740 likes | 974 Views
php. Official site at http://www.php.net/. About php. Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as a script. The server executes the php code as it delivers the html to the client.
E N D
php Official site at http://www.php.net/
About php • Php is server-side code. • Php typically sits in a php tag inside an html doc, so we refer to it as a script. • The server executes the php code as it delivers the html to the client. • Php provides a way for the serv er to dynamically render an html page.
PHP available at instant rails or xampp • http://www.apachefriends.org/en/xampp.html Xampp site has downloads for exe, zip and install formats of apache (server), php, mysql, perl and filezilla. Instant Rails comes with mysql, php, and ruby. Either way, remember to start your apache server to serve php documents. This semester we will use InstantRails, available at http://instantrails.rubyforge.org/wiki/wiki.pl
After download of xampp/install- run control panel:C:\xampp\xampp\xampp-control.exe
Instant Rails also has a control panel to start apache and mysql servers – the I thingy
The extension: .php • .php tells the (server) to run the php interpreter for the embedded script. • You can use the usual script tag but the <?php ?> notation is less typing.
Test1.php – note simple variable definition… type is dynamically allocated <html> <?php $myvar="message string"; echo $myvar; ?> other stuff <html>
Php running in instant rails: navigate to http://localhost/appname.php (even though it has html tags)
Script to call phpinfo() <html> <?php phpinfo() ?> <html>
Trivial.php <html> <head> <title> today.php </title> </head> <body> <p> <?php print "<b>Welcome to my home page <br /> <br />"; print "Today is:</b> "; print date("l, F jS"); print "<br />"; ?> </p> </body> </html>
Control structures… note variables get $ <html> <?php $myvar="message string"; $times = 5; $x = 0; while ($x < $times) { echo "Hello World<br/>"; ++$x; } ?> <html>
Create a long long webpage <html> <?php $number = 1000; $current = 0; while ($current < $number) { ++$current; echo "$current<br/>"; } ?>
Powers.php text example <html> <head> <title> powers.php </title> </head> <body> <table border = "border"> <caption> Powers table </caption> <tr><th> Number </th> <th> Square Root </th> <th> Square </th> <th> Cube </th> <th> Quad </th></tr> <?php for ($number = 1; $number <=10; $number++) { $root = sqrt($number); $square = pow($number, 2); $cube = pow($number, 3); $quad = pow($number, 4); print("<tr align = 'center'> <td> $number </td>"); print("<td> $root </td> <td> $square </td>"); print("<td> $cube </td> <td> $quad </td> </tr>"); } ?> </table> </body> </html>
Just the php part <?php for ($number = 1; $number <=10; $number++) { $root = sqrt($number); $square = pow($number, 2); $cube = pow($number, 3); $quad = pow($number, 4); print("<tr align = 'center'> <td> $number </td>"); print("<td> $root </td> <td> $square </td>"); print("<td> $cube </td> <td> $quad </td> </tr>"); } ?>
arrays • Arrays have similarities to arrays and hashes (from perl)
Using an array <html> <?php $names[0] = 'John'; $names[1] = 'Paul'; $names[2] = 'Steven'; $names[3] = 'George'; $names[4] = 'David'; $number = 5; $x = 0; while ($x < $number) { $namenumber = $x + 1; echo "Name $namenumber is $names[$x]<br>"; ++$x; } ?> <html>
The script <html> <body> The list <ul> <?php $family=array('keely','shanny','tisha','mom','dad'); foreach($family as $member) { echo "<li>$member"; } ?> </ul> </body> </html>
Script from previous slide <?php $family=array('keely','shanny','tisha','mom','dad'); print_r($family); ?>
The script for previous slide <html> <body> <ul> <?php $family=array('Keely'=>'daughter','Patricia'=>'daughter','Siobhan'=>'daughter','Katie'=>'mom','Dennis'=>'dad'); foreach($family as $key=>$person) { echo "<li>$key is a $person"; } ?> </ul> </body> </html>
Sorting an array: text example <?php $original = array("Fred" => 31, "Al" => 27, "Gandalf" => "wizzard", "Betty" => 42, "Frodo" => "hobbit"); ?> <h4> Original Array </h4> <?php foreach ($original as $key => $value) print("[$key] => $value <br />"); $new = $original; sort($new); ?> <h4> Array sorted with sort </h4> <?php foreach ($new as $key => $value) print("[$key] = $value <br />"); $new = $original; sort($new, SORT_NUMERIC); ?>
Sorting an array: text example <h4> Array sorted with sort and SORT_NUMERIC </h4> <?php foreach ($new as $key => $value) print("[$key] = $value <br />"); $new = $original; rsort($new); ?> <h4> Array sorted with rsort </h4> <?php foreach ($new as $key => $value) print("[$key] = $value <br />"); $new = $original; asort($new); ?> <h4> Array sorted with asort </h4> <?php foreach ($new as $key => $value) print("[$key] = $value <br />"); $new = $original; arsort($new); ?> <h4> Array sorted with arsort </h4> <?php foreach ($new as $key => $value) print("[$key] = $value <br />"); ?>
Function example 1: local var $sum <?php function summer ($list){ $sum=0; foreach($list as $value) $sum +=$value; return $sum; } $sum=10; $nums=array(1,3,5,7,9); $ans=summer($nums); print("sum of values in \$nums is $ans<br/>"); print("value of var \$sum is still $sum<br/>"); ?>
Function example 2: global var $xsum <?php $xsum=0; function summer ($list){ global $xsum;//access the global var foreach($list as $value) $sum +=$value; $xsum +=$sum; return $sum; } $nums=array(1,3,5,7,9); $others=array(2,4,6,8,10); $ans=summer($nums); print("sum of values in \$nums is $ans<br/>"); $ans=summer($others); print("sum of values in \$others is $ans<br/>"); print("value of var \$xsum is $xsum<br/>"); ?>
Static function variables <?php function summer ($list){ static $xsum=0;//static var static $callcount=0;//static var $callcount++; print("call count is $callcount<br/>"); print("function start...current values of \$xsum is $xsum<br/>"); foreach($list as $value) $sum +=$value; $xsum +=$sum; print("function ending...current values of \$xsum is $xsum<br/>"); return $sum; } $nums=array(1,3,5,7,9); $others=array(2,4,6,8,10); $more=array(1,1,1,1,1,1); $ans=summer($nums); $ans=summer($others); summer($more); ?>
Counting word frequencies <?php function splitter($str){ $freq=array(); $words=preg_split("/[ .;:!,?]\s*/",$str); foreach($words as $word){ $keys=array_keys($freq); if(in_array($word,$keys)) $freq[$word]++; else $freq[$word]=1; } return $freq; }//end fn $str="here is some long sentence... a a a a punctuation ... other words,,, apples pears peaches oranges it if x x x"; $tbl=splitter($str); print "<br/>frequencies<br/>"; $sorted_keys=array_keys($tbl); sort($sorted_keys); foreach($sorted_keys as $word) print "$word $tbl[$word] <br/>"; ?>
An html form whose post goes to action.php <form action="action.php" method="post"> <p>Your name: <input type="text" name="name" /></p> <p>Your age: <input type="text" name="age" /></p> <p><input type="submit" /></p> </form>
Action.php Hi <?php echo $_POST['name']; ?>. You are <?php echo $_POST['age']; ?> years old.