php php: hypertext preprocesor personal home page tools

27
PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Upload: amber-davidson

Post on 04-Jan-2016

234 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

PHP

PHP: Hypertext PreprocesorPersonal Home Page Tools

Page 2: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 3: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Example

...

<input type="submit" />

</form>

<br />

<?php echo $a+ $b ?>

</body>

</html>

...

Page 4: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

<?php if ( $a ) { ?>

<table border="1">

<?php for( $i=0; $i<$a; $i++ ) { ?>

<tr><td>

<?php echo $i ?>

</td></tr>

<?php } ?>

</table>

<?php } ?>

Page 5: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Basic Syntax

statements terminated with ; comments:

shell script style: # . . . (to end of line) C++ style: // . . . (to end of line) C style: /* . . . */

Page 6: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 7: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Arrays

indexed:

$a1 = array( “dog”, “cat”, “ant”, “mouse” );

$a1[2] = “snake”; associative:

$a2 = array( 'bob'=>24, 'alice'=>33, 'ted'=>12 );

$a2['alice'] = 41;

Page 8: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 9: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Built-in Variables

$_SERVER: associative array containing web server environment

variables e.g.

$_SERVER['GATEWAY_INTERFACE'] $_SERVER['REMOTE_ADDR'] $_SERVER['HTTP_ACCEPT']

Page 10: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Built-in Variables

$_GET: form fields submitted with GET

$_POST: form fields submitted with POST

Page 11: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 12: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Built-in Variables

$_REQUEST: everything from the $_GET, $_POST, and $_FILES

variables

Page 13: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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”

}

Page 14: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Operators

pretty much as in C/C++ no separate set of comparison operators for

strings . operator for concatenating strings + operator for concatenating arrays

Page 15: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Control Flow – if

if ( $a > 2 ) {

. . .

} elseif ( $b < 10 ) {

. . .

} else {

. . .

}

Page 16: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Control Flow – for, while, do ... while

for ( expr1; expr2; expr3 ) { . . . } while ( expr ) { . . . } do { . . . } while ( expr ); continue; break;

Page 17: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

foreach

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

Page 18: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Funcitons

funciton foo( $arg1, $arg2, ..., $argn ){

. . .return $arg1 + $arg3;

}. . .$a = foo( 1, 2, 3, ... ); // $a gets 4

Page 19: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Functions – default args

funciton foo( $name, $weight = 27 ){

. . .echo $weight;

}. . .foo( 'Bob', 33 ); // outputs 33foo( 'Ted' ); // outputs 27

all default args should be rightmost in the args list

Page 20: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

Functions – reference args

funciton foo( &$xyz ){

. . .$xyz = 'Carol';

}. . .$name = 'Alice';foo( $name );echo $name; // outputs 'Carol'

Page 21: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 22: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 23: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

mysql_connect

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

failure

Page 24: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

mysql_select_db

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

Page 25: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 26: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

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

Page 27: PHP PHP: Hypertext Preprocesor Personal Home Page Tools

3 Tier Architectures