1 / 28

PHP

PHP: Hypertext Preprocesor Personal Home Page Tools. PHP. Features. scripting to generate dynamic content is embedded inside an XHTML page similar in syntax to Perl (sort of) ‏ similar to server side inculdes: web server parses .php file executes scripts sends result to client

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: Hypertext Preprocesor Personal Home Page Tools PHP

  2. Features • scripting to generate dynamic content is embedded inside an XHTML page • similar in syntax to Perl (sort of)‏ • similar to server side inculdes: • web server parses .php file • executes scripts • sends result to client • Built-in DB support

  3. Example ... <input type="submit" /> </form> <br /> <?php echo $a + $b ?> </body> </html> ...

  4. <?php if ( $a ) { ?> <table border="1"> <?php for( $i=0; $i<$a; $i++ ) { ?> <tr><td> <?php echo $i ?> </td></tr> <?php } ?> </table> <?php } ?>

  5. Basic Syntax • statements terminated with ; • comments: • shell script style: # . . . (to end of line)‏ • C++ style: // . . . (to end of line)‏ • C style: /* . . . */

  6. Scalar Types • boolean: true, false (case insensitive)‏ • integer: usual decimal, octal and hex notations • floating point: usual notations • string: • single quote: 'xyz $abc': no variable interpolation • double quotes: “$abc xyz”: variable interpolation • heredoc: <<<END: multiline strings with varialbe interpolation

  7. Arrays • indexed: $a1 = array( “dog”, “cat”, “ant”, “mouse” ); $a1[2] = “snake”; • associative: $a2 = array( 'bob'=>24, 'alice'=>33, 'ted'=>12 ); $a2['alice'] = 41;

  8. Variables • begin with a $ followed by the variable name • variable variables: $xyz = 'abc'; $$xyz = 123; // actualy sets the value of $abc ! echo $abc; // prints out 123

  9. Built-in Variables • $_SERVER: • associative array containing web server environment variables • e.g. • $_SERVER['GATEWAY_INTERFACE'] • $_SERVER['REMOTE_ADDR'] • $_SERVER['HTTP_ACCEPT']

  10. Built-in Variables • $_GET: • form fields submitted with GET • $_POST: • form fields submitted with POST

  11. Built-in Variables • $_FILES: • form fields of the file type (uploaded files)‏ • $_FILES['x']['name'] – name of the file on the client machine • $_FILES['x']['type'] – mime type e.g. image/jpeg • $_FILES['x']['size'] – file size in bytes • $_FILES['x']['tmp_name'] – path specification to a local file containing the contents of the uploaded file

  12. Built-in Variables • $_REQUEST: • everything from the $_GET, $_POST, and $_FILES variables

  13. Variable Scope • unlike most languages global variables are not visible inside a funciton unless explicitly declared so $a = “hello”; $a = “hello”; function foo() { funtion foo() { print $a; // nothing ! global $a; } print $a; // “hello” }

  14. Operators • pretty much as in C/C++ • no separate set of comparison operators for strings • . operator for concatenating strings • + operator for concatenating arrays

  15. Control Flow – if if ( $a > 2 ) { . . . } elseif ( $b < 10 ) { . . . } else { . . . }

  16. Control Flow – for, while, do ... while • for ( expr1; expr2; expr3 ) { . . . } • while ( expr ) { . . . } • do { . . . } while ( expr ); • continue; • break;

  17. foreach • used with indexed or associative arrays • foreach ( $array_var as $value ) { . . . } • foreach ( $assoc_array as $key => $value ) { . . .}

  18. Funcitons funciton foo( $arg1, $arg2, ..., $argn )‏ { . . . return $arg1 + $arg3; } . . . $a = foo( 1, 2, 3, ... ); // $a gets 4

  19. Functions – default args funciton foo( $name, $weight = 27 )‏ { . . . echo $weight; } . . . foo( 'Bob', 33 ); // outputs 33 foo( 'Ted' ); // outputs 27 • all default args should be rightmost in the args list

  20. Functions – reference args funciton foo( &$xyz )‏ { . . . $xyz = 'Carol'; } . . . $name = 'Alice'; foo( $name ); echo $name; // outputs 'Carol'

  21. Functions – reference returns $a = 7; $b = 3; funciton &foo( $x )‏ { global $a, $b; if ( $x ) return $a; else return $b; } $c =& foo( 1 ); // $c and $a the same $d =& foo( 0 ); // $d and $b the same

  22. Functions

  23. require • textual inclusion • require 'vars.php'; • drops out of php mode into XHTML mode, so included file must have <?php ... ?> around any code • require_once 'vars.php' • ensures a file is included only once per page

  24. mysql_connect $link = mysql_connect( $host, $user, $password ); • returns a link identifier on success, FALSE on failure

  25. mysql_select_db mysql_select_db( $dbname, [ $link ] ); • returns TRUE on success, FALSE on failure

  26. mysql_query $result = mysql_query( $query, [ $link ] ); • returns TRUE or a result set on success, FALSE on failure • mysql_affected_rows( [ $link ] ); • returns number of affected rows for non-SELECT queries • mysql_num_rows( [ $link ] ); • returns number of rows in result set for SELECT queries

  27. mysql_fetch_* $iarray = mysql_fetch_row( $result ); • fetches the next row in the result set as a regular indexed array $aarray = mysql_fetch_assoc( $result ); • fetches the next row in the result set as an associative array

  28. 3 Tier Architectures

More Related