1 / 74

php

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.

jules
Télécharger la présentation

php

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. php Official site at http://www.php.net/

  2. 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.

  3. 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

  4. After download of xampp/install- run control panel:C:\xampp\xampp\xampp-control.exe

  5. Xampp Control panel running

  6. Instant Rails also has a control panel to start apache and mysql servers – the I thingy

  7. Control panel running with both apache and mysql started.

  8. Running a php – notice the URL

  9. For xampp, put php here: xampp\apache\htdocs

  10. In instant rails, put php scripts in www dir

  11. 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.

  12. Test1.php – note simple variable definition… type is dynamically allocated <html> <?php $myvar="message string"; echo $myvar; ?> other stuff <html>

  13. Php running in instant rails: navigate to http://localhost/appname.php (even though it has html tags)

  14. Calling predefined Phpinfo()

  15. Script to call phpinfo() <html> <?php phpinfo() ?> <html>

  16. 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>

  17. Trivial.php

  18. Control structures… note variables get $ <html> <?php $myvar="message string"; $times = 5; $x = 0; while ($x < $times) { echo "Hello World<br/>"; ++$x; } ?> <html>

  19. browser

  20. Create a long long webpage <html> <?php $number = 1000; $current = 0; while ($current < $number) { ++$current; echo "$current<br/>"; } ?>

  21. Numbers 1..1000

  22. 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>

  23. A table of powers

  24. 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>"); } ?>

  25. arrays • Arrays have similarities to arrays and hashes (from perl)

  26. 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>

  27. Using an array

  28. Another array as html list

  29. 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>

  30. Different output from an array

  31. Script from previous slide <?php $family=array('keely','shanny','tisha','mom','dad'); print_r($family); ?>

  32. A hash (array): key-value pairs

  33. 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>

  34. 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); ?>

  35. 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 />"); ?>

  36. Sorting an array: text example

  37. Sorting an array: text example

  38. Sorting an array: text example

  39. Function example 1

  40. 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/>"); ?>

  41. Function example 2: global var $xsum

  42. 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/>"); ?>

  43. Static function variables

  44. 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); ?>

  45. 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/>"; ?>

  46. Output…not sure about 1st line

  47. 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>

  48. Html form

  49. Action.php output

  50. Action.php Hi <?php echo $_POST['name']; ?>. You are <?php echo $_POST['age']; ?> years old.

More Related