World Wide Web Programming

$n=11; $a=11; if ($n>10) if ($a >10) echo "1"; ?>

Here is some HTML in your. PHP

. .
246KB taille 2 téléchargements 318 vues
World Wide Web Programming 6 – Introduction to PHP Part II

Arrays Functions ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹

$success=array_walk($arr,’func’); $nbr=count($arr); $res=current($arr); $arr1=each($arr); $res=end($arr); $res=key($arr); next($arr) $res=pos($arr); prev($arr) $res=$reset($arr); $res=$sizeof($arr);

Sorting an array arsort, asort, krsort, ksort, rsort, sort ‹ You can also specify which comparison you want to use to sort, by adding the function name as a second parameter ‹

• uasort, uksort, usort ‹

Look at the following code example • Download http://cs465.free.fr/files/sortings.zip

Other interesting functions ‹

list ($a, $b, $c) = $arr; • Copies the elements of the array into the variables

‹

range($min, $max); • Creates an array with the integer numbers between $min and $max

‹

shuffle($arr); • D’you really need an explanation?

Some PHP4 array functions ‹ ‹ ‹ ‹

$arr1=array_count_values($arr); $arr2=array_keys($arr); $arr3=array_merge($arr1, $arr2); $arr1=array_pad($arr, $n, $val);

• Adds $n times $val to the $arr • If $n < 0 it adds at the beginning of table

‹ ‹ ‹ ‹

$elem=array_pop($arr); $arr1=array_push($arr, $elem, …); $arr1=array_reverse($arr); $elem=array_shift($arr);

Some PHP4 array functions cont’d ‹

$arr2=array_slice($arr,$pos,$num);

• Return $num elements from $arr, starting at $pos

‹

$arr1=array_splice($arr,$pos,$num,$arre ); • Takes out of $arr $num elements starting at $pos and replaces them by the elements of $arre • $arr1 holds the elements replaced

‹ ‹ ‹

$arr1=array_unshift($arr,x,y,z); $arr1=array_values($arr); in_array(3, $arr); // TRUE or FALSE

PHP Programs Structure A program in PHP is basically a set of instructions ‹ You can create blocks using { } ‹ The whole block will have its own instruction and will be considered as an instruction ‹

Conditions ‹

Basic if structure • if (condition1) Instruction1; elseif (condition2) Instruction2; else Instruction3;

‹

Remember to use comparisons not affectations!! •

‹

An else refers to the last if created which hasn’t got an else yet

Problems of inserting HTML ‹

Here is some HTML in your PHP

This code would output

$n=11; $a=11; if ($n>10) if ($a >10) echo "1";

• Parse error: parse error, unexpected T_ELSE in D:\Stevens\WEB\cs465\samples\ifproblem.php3 on line 9

How to solve this? ‹

‹

Here is some HTML in your PHP

This will allow the server to understand that your if/else is not over yet

Doing Loops - While ‹

While: • while (Expression) Instruction

‹

Ex: •

$i=32; while ($i Don’t forget the BREAKS!!!!

Using functions ‹

‹

‹

‹

Functions no longer need to be defined before they’re called Usually you would write your functions at the end of your file $res = average(25, 12); function average($a, $b) { return ($a + $b)/2; } You can use recursive calls

Different ways of calling a function ‹

By value • It’s the normal call, variables in the function won’t affect the main ones

‹

By reference • average(&$a, &$b); • In this case, any change done to the variables within the function will also affect them in the main program

Linking with external files ‹

include (filename) • Inserts an external file in the script • The called file could have a return value that could be used by the calling script • Can be used in loops

‹

require (filename) • Does basically the same thing, but executes the file before inserting it • Cannot use a return • Cannot be used in loops

Using files It is possible to open and manipulate files with PHP ‹ Remember it’s a server-side script language, therefore the files used are in the server! ‹ Next slides holds a big list of functions, we’ll use the variable $fp (File Pointer) that is created by doing an fopen() ‹

Files Manipulation Functions ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹

copy($src,$dest) fclose($fp) feof($fp) fgetc($fp) $arr=fgetcsv($fp,$len,”,”) fgets($fp,$maxlength) fgetss($fp,$maxlength) file(“filename”) flock($fp,$op) $fp=fopen(“file”,$mode)

‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹ ‹

fpassthru($fp) fputs($fp,$str) fread($fp,$length) fseek($fp,$offset) ftell($fp) fwrite($fp,$str) readfile(“filename”) rename(“old”,”new”) rewind($fp) unlink(“filename”)

Some notes on flock and fopen ‹

The $op in flock corresponds to the lock option • • • •

‹

1: 2: 3: 4:

Shared read only Write only Unlock access authorized when locked

The $mode can be • • • • •

“r” read only “r+” read and write “w” write only “w+” read and write erasing or creating the file “a” write only. places pointer at the end of file or creates the file • “a+” read write placing pointer at end or creating file • In cany case you can add a b to indicate it’s a binary file (“ba”, “br+”, “bw+”,…)

An example ‹