PHP QuickRef

March 19, 2018 | Author: narendramahajantalen | Category: Php, Text File, Email, Computer Programming, Software


Comments



Description

Prepared by Ravi Sambangi (http://www.sravisankar.com) PHP (Hypertext Preprocessor) PHP code should start with <?php and end with ?> Eg: <?php …………………. ?> To output the text, we have two functions: echo and print Eg: echo “Hello World”; Each line should be separated with ; (semicolon) Comments in PHP: o // single line comment o /* block • comment */ Variables: variables should start with $ no need to tell php about the data type of the variable no need to declare a variable before being set variable names are case sensitive Syntax: $variableName=value; Eg: <?php $name = "My Name"; $regNo = 1220; echo “Name=” ?> Note: . $name . “Number=” . $regNo; . (Dot) is used to concatenate the strings Example echo strlen("Hello World"); echo strpos("Hello World", "World"); echo ltrim(“ Hello”); ”); Hello Hello String Functions Function Description Name strlen strpos ltrim rtrim find the length of a string search for a string or character within a string Removes whitespaces from the left/right side of a string Removes spaces from both sides Output 11 6 echo rtrim(“Hello trim print slower than echo str_replace replaces some characters with some other characters(2nd argurement) in a string. echo trim(“ Hello World ”); <?php $name = "Ravi”; $number =12; print "Name:$color"; print "<br />"; print 'No:$number”; ?> <?php echo str_replace("World","Ravi","Hello World"); ?> 1. $arr=str_split("Ravi Sankar",3); Hello World Name:Ravi No:12 Hello Ravi 1. Rav i S ank ar 2. A B C str_split splits a string into an array of strings(each string length is by default 1) 2. $arr=str_split(“ABC”); Page 1 of 8 Source: http://www.w3schools.com and Internet com) strcasecmp strcmp compares two strings. strcasecmp(“A”.").5).”B”). strcasecmp(“A”. 2. $token = strtok(" . 1. 2. 2.com and Internet .w3schools. if ($d=="Fri"){ echo "Have a nice weekend!". strcasecmp(“ravi”. <?php $string = "Ravi. 1.6. substr("Hello World!". strcasecmp(“ravi”. substr("Hello World".”Ravi”). echo strtoupper("Hello").”B”). you are good boy".6).(casesensitive) 1.World! 2. 0 <0 >0 <0 strtok() splits a string into smaller strings depends on some criteria Ravi you are good boy strtolower() strtoupper() substr() Converts a string to lowercase/uppercase.else Eg: <?php $d=date("D"). $token = strtok($string.").”Ravi”).else if . 1. 2.(caseinsensitive) compares two strings. 2.start. 1. " . }//while ?> echo strtolower("Hello"). }else if($d==”Sun”){ Page 2 of 8 Source: http://www. World Operators • Arithmetic Operators + * / % ++ and -- • Assignment Operators Operator = += -= *= /= %= Example x=y x+=y x-=y x*=y x/=y x%=y Is equal to x=y x=x+y x=x-y x=x*y x=x/y x=x%y • Comparison Operators == != > < >= <= • Logical Operators && || ! Conditional Statements if .length) Hello HELLO 1. returns a part of a string Syntax: substr(string.Prepared by Ravi Sambangi (http://www.sravisankar. while ($token != false){ echo "$token<br />". $value . foreach ($arr as $value){ echo "Value: " . Eg: <?php $arr=array("one". $i++){ echo "The number is " . break. }//switch ?> Output: Number 2 for loop Eg: <?php for ($i=1. $i<=3. case 3: echo "Number 3". "two". }//foreach ?> Output: Value:one Value:two Value:three while loop Eg: <?php Page 3 of 8 Source: http://www.w3schools. $i . break.com) echo "Have a nice Sunday!". switch ($x){ case 1: echo "Number 1". break. }//else ?> switch Statement Eg: <?php $x=2.sravisankar. case 2: echo "Number 2".com and Internet .is used to loop through arrays. "three"). }else{ echo "Have a nice day!". "<br />" }//for ?> Output: The number is 1 The number is 2 The number is 3 foreach loop . "<br />". default: echo "No number between 1 and 3".Prepared by Ravi Sambangi (http://www. echo "The number is " . Multidimensional array . do{ $i++. ?> Output: The number The number The number The number is is is is 1 2 3 4 Arrays -There are three types of arrays in PHP 1.stores each element with a numeric ID key. $regNumbers [‘Sankar’] ="1221i".w3schools. Numeric array . $regNumbers[‘Ravi’] 3. "<br />". $empNames[1] ="Sankari". while($i<=3){ echo "The number is " . empNames[1] . echo “Ravi Reg Number: “ . $i++. And each element in the sub-array can be an array. Eg: $myCompany = array ( "Group1"=>array ( "Ravi". }//while ?> Output: The number is 1 The number is 2 The number is 3 do while loop Eg: <?php $i=0.sravisankar. "Sankar"=>1221. empNames[2] 2. each element in the main array can also be an array. Eg1: $empNames = array("Ravi". and so on.In a multidimensional array. $i . echo empNames[0] . Eg2: $regNumbers [‘Ravi’] ="1220". "Sankar" Page 4 of 8 Source: http://www. $empNames[2] ="Kumar". }while ($i<=3)."Sankar".com and Internet . "<br />". "Kumar"=>1222). Associative array . Eg2: $empNames[0] ="Ravi"."Kumar").An array where each ID key is associated with a value (key-value pairs) Eg1: $regNumbers = array("Ravi"=>1220. $regNumbers [‘Kumar’] ="1222r".Prepared by Ravi Sambangi (http://www. $i .com) $i=1. Seconds.20).Minutes with leading zeros (00 to 59) s . } echo "sum of 10 and 20 is = " . MDT) timestamp: is the no. "<br />". echo myCompany[‘Group1’][0] .…) Y . “John” ) ). echo "Your name is ". Syntax: mktime(hour. echo “************************ <br/>”.timestamp) where format: specifies how to format the date/time d .Prepared by Ravi Sambangi (http://www. Eg: echo date("Y/m/d").Timezone setting of the PHP machine (Examples: EST.w3schools. }//function writeData echo "My name is ".minute. “is in Group1” Functions Eg1: <?php function writeData($myName){ echo “************************ <br/>”.is_dst) Page 5 of 8 Source: http://www.12-hour format of an hour (01 to 12) H .24-hour format of an hour (00 to 23) i .1970 at 00:00:00 GMT.com) ). ?> Date() in PHP Syntax: date(format.com and Internet .second. myAddition(10. return $total. "Group2"=>array ( "Kumar". Output: 2008/02/21 Adding a timestamp to the existing date.year.$second){ $total = $first + $second. ?> Eg2: <?php function myAddition($first.day.sravisankar.The day of the month (01-31) D – Day name (Mon. writeData("Sankar"). as a number (01-12) M – Short month name(Jan) F – Full month name (January. with leading zeros (00 to 59) T .Jan.of seconds since 1st.Uppercase AM or PM h . Default is the current date and time.The current month. echo $myName .The current year in four digits y – two digit representation of year (08) a .month.Lowercase am or pm A . Tue …) l – A full textual representation of a day(Lower case L) m . writeData("Ravi"). Windows note: If a full stop is found on the beginning of a line in the message.0. Data sent by GET method will be visible in address bar and it has limitation in size of data.date("Y")). $tomorrow). it might be removed. Note: When sending an email.w3schools.".date("Y/m/d".php" method="post"> Name: <input type="text" name="txtName"> RegNo: <input type="text" name="txtRegNo"> <input type="submit”> </form> </body> </html> FirstWeb.message. ?> </body> </html> mail function in PHP Syntax: mail(to.headers.com) Eg: <?php $tomorrow = mktime(0. Lines should not exceed 70 characters. Defines the message to be sent.sravisankar.php <html> <body> Entered Name: <?php echo $_POST["txtName"].php <html> <body> <form action="FirstWeb.subject.<br /> Entered Reg No: <?php echo $_POST["txtRegNo"].parameters) Parameter to subject message Description Required. Specifies additional headers. We can receive the form data either through $_GET[“variableName”] or $_POST[“variableName”] Note: We can use $_REQUEST[“variableName”] to get the result from form data sent with both the GET and POST methods and also COOKIE method.. parameters Optional. it must contain a From header. echo "Tomorrow is ". The additional headers should be separated with a CRLF (\r\n). "\n. Specifies the receiver / receivers of the email Required. ?> Output: Tomorrow is 2008/02/22 PHP in Web applications We can send the form data either in get or post method form. ?> headers Optional. Note: This parameter cannot contain any newline characters Required.". Eg1: index.0. Cc. and Bcc. This can be set with this parameter or in the php.Prepared by Ravi Sambangi (http://www. Specifies an additional parameter to the sendmail program (the one Page 6 of 8 Source: http://www.com and Internet . To solve this problem.date("d")+1. replace the full stop with a double dot: <?php $txt = str_replace("\n. Each line should be separated with a LF (\n).date("m"). ?>. POST method doesn’t have any limitations. $txt). Specifies the subject of the email.ini file. like From. ’Ravi’).$txtContent).$user. ?> Eg2: email with extra headers: <?php $to = "[email protected] VARCHAR(20)). you can get this name from tnsnames.$headers). die('Could not connect: ' . Page 7 of 8 Source: http://www.com and Internet .ename) VALUES(1220.com".com". Eg: CREATE TABLE EMP(seqno INTEGER AUTO_INCREMENT PRIMARY KEY. $ename=$row['ename']. "CC:[email protected]. <html> <body> <table border="1"> <tr> <td>Eno</td> <td>EName</td> </tr> <?php $host="1. $db_name="MySID". mail($to.$pass).w3schools. "\r\n" . (i.com) defined in the sendmail_path configuration setting). INSERT INTO EMP(eno.e. $txt = "Hello world!". ?> PHP with Database (MySQL) Assume you have a table called EMP having two fields.$txt. $subject = "My subject".3. if(!$con){ printf("error while connecting to database"). it can be localhost also depends on ur configuration $user="scott". } mysql_select_db($db_name.Prepared by Ravi Sambangi (http://www.$con)){ $eno=$row['eno']. // Use wordwrap() if lines are longer than 70 characters $txt = wordwrap($txt. $sqlstr=mysql_query("SELECT * FROM emp".$subject.ename) VALUES(1221.com" .com".’Sankar’). $headers = "From: webmaster@example. INSERT INTO EMP(eno."My subject".4". while($row=mysql_fetch_array($sqlstr.//database location. // Send email mail("somebody@example. echo "<td>$eno</td>".eno INT(4).$con). mysql_error()).ora //database connection $con=mysql_connect($host. //sid. echo "<tr>". for oracle. this can be used to set the envelope sender address when using sendmail with the -f sendmail option) Eg1: Simple Mail <?php $txtContent = This is first line of text\n This is second line of text".$con).70). $pass="tiger". com and Internet .com) echo "<td>$ename</td>". }//while( mysql_close($con).w3schools.Prepared by Ravi Sambangi (http://www. echo "</tr>".sravisankar. ?> </body> </html> Page 8 of 8 Source: http://www.
Copyright © 2024 DOKUMEN.SITE Inc.