16
week 4 PHP PHP Hypertext Preprocessor Reference : http://www.w3schools.com/php/php_intro.asp Official Site : http://www.php.net

Week 4 PHP H ypertext P reprocessor Reference : Official Site :

Embed Size (px)

Citation preview

week 4

PHP

PHP Hypertext Preprocessor

Reference : http://www.w3schools.com/php/php_intro.asp

Official Site : http://www.php.net

PHP Hypertext Preprocessor

What?

♦ Open source free to downloadfrom www.php.net

♦ Server-side script (similar to asp)executed at the server end (unlike javascript)

♦ Common uses

♦ Form-handling

♦ E-commerce

♦ Shopping-carts store information on the server

PHP Hypertext Preprocessor

PHP files

• Have file extension .phpe.g. index.php

• File contains text, html tags & scripts

• PHP codes enclosed in <?php ?> tags

• File MUST be parsed by web-server (cannot open directly in browser)

PHP Hypertext Preprocessor

PHP syntax

Simiar to C++• Statements end with semi-colon;• Case-sensitive• Same rules about variable names

1. No spaces

2. Must not begin with numbers

• Same logic & loop structureif ( ) { }

PHP Hypertext Preprocessor

PHPExample:

<html><head><script> window.status = 'hello';</script><body><?php echo "hello\n";?></body></html>

PHP Hypertext Preprocessor

PHPVariables

1. Prefixed with ‘$’

2. Begins with letters or underscore

3. Contains only a-z A-Z 0-9 _

4. No need to define type

Examples: $a1, $_x, $myVariable

$1a, $my name , $x.y

PHP Hypertext Preprocessor

PHPSuperglobal Variables

• $_GET

• $_POST

• $_REQUEST

• $_SERVER

PHP Hypertext Preprocessor

PHPSimple PHP commands

• echo

• print

• print_r

• array( )

• count( $x)

PHP Hypertext Preprocessor

PHPArrays

A variable that stores multiple values

e.g. $scores

80 90 95 100 60

PHP Hypertext Preprocessor

PHPArrays

Two types of arrays

• indexed

• associative

PHP Hypertext Preprocessor

PHPArrays

Normal (indexed) array

- Each value is referenced by index numbers, starting from zero.

0 1 2 3 4

80 90 95 100 60Index numbers

values

<?php echo $scores[2]; ?>

PHP Hypertext Preprocessor

PHPArrays

Associative array

- Each value is referenced by text known as key.

CL EL HU MA SC

80 90 95 100 60keys

values

<?php echo $scores[" HU"]; ?>

PHP Hypertext Preprocessor

PHPArray defintion

<?php$scores1 = array(80, 90, 95, 100, 60);

$scores2 = array("CL"=>80, "EL"=>90, "HU"=>95, "MA"=>100, "SC"=>60);

?>

PHP Hypertext Preprocessor

PHPArrays

Debugging arrays

- The function “print_r” is used to display contents in an array.

<?php print_r $scores; ?>

PHP Hypertext Preprocessor

PHPArrays

The following system variables are associative arrays:

$_GET$_POST$_REQUEST$_SERVER$_SESSION

PHP Hypertext Preprocessor

☺PHP