Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an...

74
php Official site at http://www.php.net/
  • date post

    24-Jan-2016
  • Category

    Documents

  • view

    223
  • download

    0

Transcript of Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an...

Page 1: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

php

Official site at

http://www.php.net/

Page 2: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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.

Page 3: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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

Page 4: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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

Page 5: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Xampp Control panel running

Page 6: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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

Page 7: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Control panel running with both apache and mysql started.

Page 8: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Running a php – notice the URL

Page 9: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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

Page 10: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

In instant rails, put php scripts in www dir

Page 11: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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.

Page 12: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Test1.php – note simple variable definition… type is dynamically allocated

<html>

<?php $myvar="message string"; echo $myvar;

?>

other stuff

<html>

Page 13: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Php running in instant rails: navigate to http://localhost/appname.php (even

though it has html tags)

Page 14: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Calling predefined Phpinfo()

Page 15: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Script to call phpinfo()

<html>

<?php

phpinfo()

?>

<html>

Page 16: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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>

Page 17: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Trivial.php

Page 18: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Control structures… note variables get $

<html><?php

$myvar="message string"; $times = 5; $x = 0; while ($x < $times) {echo "Hello World<br/>"; ++$x; }

?><html>

Page 19: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

browser

Page 20: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Create a long long webpage

<html><?php$number = 1000;$current = 0;while ($current < $number) {++$current;echo "$current<br/>";}?>

Page 21: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Numbers 1..1000

Page 22: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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><?phpfor ($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>

Page 23: Php Official site at . 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 table of powers

Page 24: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Just the php part

<?phpfor ($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>");}?>

Page 25: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

arrays

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

Page 26: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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>

Page 27: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Using an array

Page 28: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Another array as html list

Page 29: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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>

Page 30: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Different output from an array

Page 31: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Script from previous slide

<?php

$family=array('keely','shanny','tisha','mom','dad');

print_r($family);

?>

Page 32: Php Official site at . 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 hash (array): key-value pairs

Page 33: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

The script for previous slide<html><body><ul><?php$family=array('Keely'=>'daughter','Patricia'=>'daughter','Siobhan'=>'da

ughter','Katie'=>'mom','Dennis'=>'dad');foreach($family as $key=>$person){echo "<li>$key is a $person";}?></ul></body></html>

Page 34: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Sorting an array: text example

<?php$original = array("Fred" => 31, "Al" => 27, "Gandalf" => "wizzard", "Betty" => 42, "Frodo" => "hobbit");?><h4> Original Array </h4><?phpforeach ($original as $key => $value) print("[$key] => $value <br />");

$new = $original;sort($new);?><h4> Array sorted with sort </h4><?phpforeach ($new as $key => $value) print("[$key] = $value <br />");

$new = $original;sort($new, SORT_NUMERIC);?>

Page 35: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Sorting an array: text example<h4> Array sorted with sort and SORT_NUMERIC </h4><?phpforeach ($new as $key => $value) print("[$key] = $value <br />");$new = $original;rsort($new);?><h4> Array sorted with rsort </h4><?phpforeach ($new as $key => $value) print("[$key] = $value <br />");$new = $original;asort($new);?><h4> Array sorted with asort </h4><?phpforeach ($new as $key => $value) print("[$key] = $value <br />");$new = $original;arsort($new);?><h4> Array sorted with arsort </h4><?phpforeach ($new as $key => $value) print("[$key] = $value <br />");?>

Page 36: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Sorting an array: text example

Page 37: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Sorting an array: text example

Page 38: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Sorting an array: text example

Page 39: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Function example 1

Page 40: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Function example 1: local var $sum

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

Page 41: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Function example 2: global var $xsum

Page 42: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Function example 2: global var $xsum<?php$xsum=0;function summer ($list){

global $xsum;//access the global varforeach($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/>");?>

Page 43: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Static function variables

Page 44: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Static function variables<?phpfunction summer ($list){

static $xsum=0;//static varstatic $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);?>

Page 45: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Counting word frequencies<?phpfunction 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/>"; ?>

Page 46: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Output…not sure about 1st line

Page 47: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

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>

Page 48: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Html form

Page 49: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Action.php output

Page 50: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Action.php

Hi <?php echo $_POST['name']; ?>.

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

Page 51: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Processing a form

• entire html form is in your text

• Php is in text

Page 52: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Popcorn form

Page 53: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Processed via php

Page 54: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Process form values: html is in slide notes<?php$unpop=$_POST["unpop"];$caramel=$_POST["caramel"];$caramelnut=$_POST["caramelnut"];$toffeynut=$_POST["toffeynut"];$name=$_POST["name"];$street=$_POST["street"];$city=$_POST["city"];$payment=$_POST["payment"];// If any of the quantities are blank, set them to zeroif ($unpop == "") $unpop = 0;if ($caramel == "") $caramel = 0;if ($caramelnut == "") $caramelnut = 0;if (toffeynut == "") $toffeynut = 0;// Compute the item costs and total cost$unpop_cost = 3.0 * $unpop;$caramel_cost = 3.5 * $caramel;$caramelnut_cost = 4.5 * $caramelnut;$toffeynut_cost = 5.0 * $toffeynut;$total_price = $unpop_cost + $caramel_cost + $caramelnut_cost + $toffeynut_cost;$total_items = $unpop + $caramel + $caramelnut + $toffeynut;// Return the results to the browser in a table

Page 55: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Handling file data

Page 56: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Data file

23231name frm filestreet from filecity from filezip from file

Page 57: Php Official site at . 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 few modified lines in popcorn php to read from a file

//getting data via file reads$file=fopen("popcorn.txt","r") or die ("file error");$unpop=fgets($file,50);$caramel=fgets($file,50);$caramelnut=fgets($file,50);$toffeynut=fgets($file,50);$name=fgets($file,50);$street=fgets($file,50);$city=fgets($file,50);$payment=fgets($file,50);

Page 58: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Writing to a file

Page 59: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Open a file for output, too

$readfile=fopen("popcorn.txt","r") or die ("read file error");

$writefile=fopen("popcornbill.txt","w") or die ("write file error");

Page 60: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Write to the file$bytes=fwrite($writefile,$name);$bytes=fwrite($writefile,$street);$bytes=fwrite($writefile,$city);$bytes=fwrite($writefile,$payment);$bytes=fwrite($writefile,$unpop);$bytes=fwrite($writefile,$unpop_cost);$bytes=fwrite($writefile,$caramel);$bytes=fwrite($writefile,$caramel_cost);$bytes=fwrite($writefile,$caramelnut);$bytes=fwrite($writefile,$caramelnut_cost);$bytes=fwrite($writefile,$toffeynut);$bytes=fwrite($writefile,$toffeynut_cost);$bytes=fwrite($writefile,$total_price);$bytes=fwrite($writefile,$total_items);print ("popcornbill.txt has been written<br/>");

Page 61: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

File contents after execution (would need to be prettied up)

name frm file

street from file

city from filezip from file236927313.515$94.529

Page 62: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Solution to 1st php lab: add new user<?php$readfile=fopen("users.txt","r+") or die ("read file error");$ctr=0;while($user[$ctr]=trim(fgets($readfile))){$pw[$ctr]=trim(fgets($readfile));//print ("while loop $user[$ctr] $pw[$ctr]<br/>");$ctr++;}$name=$_POST["name"];$pass=$_POST["pw"];$name= $name . "\n";$pass=$pass . "\n";$bytes=fwrite($readfile,$name);$bytes=fwrite($readfile,$pass);print ("new user added");?>

Page 63: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

New user html page

<html><body><h1> new user account page</h1><form action="adduser.php" method="post"> <p>Your name: <input type="text" name="name" /></p> <p>Your password: <input type="password" name="pw"

/></p> <p>enter password again</p> <p>Your password: <input type="password"

name="pw2" /></p> <p><input type="submit" /></p></form>

Page 64: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Login page

<html><body><h1> user login page</h1><form action="checkpw.php" method="post"> <p>Your name: <input type="text" name="name" /></p> <p>Your password: <input type="password" name="pw"

/></p> <p><input type="submit" /></p></form></body></html>

Page 65: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Check the pw<?php$name=$_POST["name"];$pass=$_POST["pw"];//print("$name $pass<br/>");$readfile=fopen("users.txt","r") or die ("read file error");$ctr=0;while($user[$ctr]=trim(fgets($readfile))){$pw[$ctr]=trim(fgets($readfile));//print ("while loop $user[$ctr] $pw[$ctr]<br/>");$ctr++;}$found=false;for($i=0;$i<$ctr&&!$found;$i++)if($name==$user[$i]){$j=$i;$found=true;}if($found )print("found at $j<br/>");if($found && $pass==$pw[$j])print("pw ok");else print ("pw failure");?>

Page 66: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Add new user

Page 67: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Logging in

Page 68: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Same user wrong pw

• User is found in position 8 but the pw is wrong.

• I did not complete the part with a cookie and a link to another page.

Page 69: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

regular expressions

• php supports pattern matching/regular expressions the same way javascript and perl do

Page 70: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Php site links

• http://us3.php.net/ -- the official site

• http://us3.php.net/manual/en/language.variables.external.php -- referencing http post variables as per precious slides

• http://us3.php.net/manual/en/faq.html.php --faq

Page 71: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Javascript & php

• Javascript to get an ip calling php

<html>

<body>

<script type="text/javascript" src="http:\\localhost\ip.php"></script>

</body>

</html>

Page 72: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

The php

<?//"ip.php" example- display user IP address on any pageHeader("content-type: application/x-javascript");$serverIP=$_SERVER['REMOTE_ADDR'];echo "document.write(\"Your IP address is: <b>" .

$serverIP . "</b>\")";?>

Page 73: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

Php-javascript example2

• Javascript

<html>

<body>

<script type="text/javascript" src="phpmessage.php"></script>

</body>

</html>

Page 74: Php Official site at . About php Php is server-side code. Php typically sits in a php tag inside an html doc, so we refer to it as.

The php

<?//Header("content-type: application/x-javascript");

$myvar="a var value";echo "document.write(\"Your var is: <b>" .

$myvar . "</b>\")";

?>