PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
-
Upload
amber-davidson -
Category
Documents
-
view
226 -
download
2
Transcript of PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
PHP
PHP: Hypertext PreprocesorPersonal 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
Example
...
<input type="submit" />
</form>
<br />
<?php echo $a+ $b ?>
</body>
</html>
...
<?php if ( $a ) { ?>
<table border="1">
<?php for( $i=0; $i<$a; $i++ ) { ?>
<tr><td>
<?php echo $i ?>
</td></tr>
<?php } ?>
</table>
<?php } ?>
Basic Syntax
statements terminated with ; comments:
shell script style: # . . . (to end of line) C++ style: // . . . (to end of line) C style: /* . . . */
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
Arrays
indexed:
$a1 = array( “dog”, “cat”, “ant”, “mouse” );
$a1[2] = “snake”; associative:
$a2 = array( 'bob'=>24, 'alice'=>33, 'ted'=>12 );
$a2['alice'] = 41;
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
Built-in Variables
$_SERVER: associative array containing web server environment
variables e.g.
$_SERVER['GATEWAY_INTERFACE'] $_SERVER['REMOTE_ADDR'] $_SERVER['HTTP_ACCEPT']
Built-in Variables
$_GET: form fields submitted with GET
$_POST: form fields submitted with POST
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
Built-in Variables
$_REQUEST: everything from the $_GET, $_POST, and $_FILES
variables
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”
}
Operators
pretty much as in C/C++ no separate set of comparison operators for
strings . operator for concatenating strings + operator for concatenating arrays
Control Flow – if
if ( $a > 2 ) {
. . .
} elseif ( $b < 10 ) {
. . .
} else {
. . .
}
Control Flow – for, while, do ... while
for ( expr1; expr2; expr3 ) { . . . } while ( expr ) { . . . } do { . . . } while ( expr ); continue; break;
foreach
used with indexed or associative arrays foreach ( $array_var as $value ) { . . . } foreach ( $assoc_array as $key => $value ) { . . .}
Funcitons
funciton foo( $arg1, $arg2, ..., $argn ){
. . .return $arg1 + $arg3;
}. . .$a = foo( 1, 2, 3, ... ); // $a gets 4
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
Functions – reference args
funciton foo( &$xyz ){
. . .$xyz = 'Carol';
}. . .$name = 'Alice';foo( $name );echo $name; // outputs 'Carol'
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
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
mysql_connect
$link = mysql_connect( $host, $user, $password ); returns a link identifier on success, FALSE on
failure
mysql_select_db
mysql_select_db( $dbname, [ $link ] ); returns TRUE on success, FALSE on failure
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
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
3 Tier Architectures