Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML...

Preview:

Citation preview

Introduction to PHP

PHP

• PHP is the Hypertext Pre-processor– Script language– Embedded into HTML– Runs as Apache module– Can use DB (MySQL, Oracle, Microsoft SQL,

PostgreSQL)– Rich features: XML, PDF, IMAP, LDAP

PHP Origins

Rasmus Lerdorf (born Greenland, ed Canada)

PHP originally abbreviates for ‘Personal Home Pages’, now ‘PHP Hypertext Processor’

Other key developers: Zeev Surashi and Andi Gutmans (Israel)

Open Source

PHP version 4.4.3 current at UWE Due to upgrade to PHP 5

Scripting languages

• A scripting language is:– often evolved not designed– cross-platform since interpreter is easy to port– designed to support a specific task – PHP -> Web

support– un-typed variables (but values are typed) – implicit variable declaration – implicit type conversion – stored only as script files– compiled on demand– may run on the server (PHP) or the client (Javascript)

PHP details

• Procedural language– Compare with Javascript which is event-driven

• C-like syntax - { } ;• Extensive Function Library• Good Web-server integration

– Script embedded in HTML– Easy access to form data and output of HTML pages

• Not fully object-oriented – Java is fully object oriented – all functions have to be

in a class– In PHP, classes are additional but quite simple to use

PHP and HTML

• HTML-embedded– PHP scripts are essentially HTML pages with the

occasional section of PHP script.– PHP script is enclosed in the tag pair:

<?php echo “hello” ?>

• Free format - white space is ignored • Statements are terminated by semi-colon ; • Statements grouped by { … } • Comments begin with // or a set of comments /* */ • Assignment is ‘=’ $a=6• Relational operators are ,< , > == ( not a single equal)

• Control structures include if (cond) {..} else { }, while (cond) { .. } , for(startcond; increment; endcond) { }

C-like language

• Arrays are accessed with [ ] : $x[4] is the 5th element of the array $x – indexes start at 0

• Associative Arrays (hash array in Perl, dictionary in Java) are accessed in the same way: $y[“fred”]

• Functions are called with the name followed by arguments in a fixed order enclosed in ( ) : substr(“fred”,0,2)

• Case sensitive - $fred is a different variable to $FRED

Function library

• Basic tasks– String Handling – Mathematics – random numbers, trig functions.. – Regular Expressions – Date and time handling – File Input and Output

• And more specific functions for-– Database interaction –

• MySQL, Oracle, Postgres, Sybase, MSSQL .. – Encryption – Text translation – Image creation – XML

String Handling

• String literals (constants) enclosed in double quotes “ ” or single quotes ‘ ’

• Within “”, variables are replaced by their value: – called variable interpolation. “My name is $name, I think”

• Within single quoted strings, interpolation doesn’t occur

• Strings are concatenated (joined end to end) with the dot operator “key”.”board” == “keyboard”

• Standard functions exist: strlen(), substr() etc

• Values of other types can be easily converted to and from strings – numbers implicitly converted to strings in a string context.

• Regular expressions be used for complex pattern matching.

Learning PHP

• Start with just the basics, installing a script to output an HTML page

• Understand how PHP supports interaction with the Browser or other clients

• Understand how PHP supports integration with databases – Postgress

Basic PHP Syntax

<?php ?>

Datatypes

• String– 'My name is Taro Keio.'– "My name is $name."

• Boolean– true– false

• Integer– 100– 0x1c

• Floating point• Array

– array("tokyo", "hanoi", "london")– array("japan" => "tokyo", "vietnam" => "hanoi",

"england" => "london")– $a[2]– $a["vietnam"]

Variables in PHP

• All variables in PHP start with a $ sign symbol. • Variables may contain strings, numbers, or

arrays.

<html><body><?php $txt="Hello World"; echo $txt; ?></body>

</html>

Variable Naming Rules

• A variable name must start with a letter or an underscore "_"

• A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )

• A variable name should not contain spaces. If a variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

Comments in PHP

<html><body><?php

//This is a comment/*This is a comment block*/

?></body> </html>

The If...Else Statement

Syntax

if (condition) code to be executed if condition is true;

else

code to be executed if condition is false;

Example

<html><body><?php

$d=date("D");if ($d=="Fri")

echo "Have a nice weekend!";else

echo "Have a nice day!"; ?></body> </html>

The Switch Statement

Syntaxswitch (expression){

case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break;default: code to be executed if expression is different from both label1 and label2;

}

Looping

• while - loops through a block of code if and as long as a specified condition is true

• do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true

• for - loops through a block of code a specified number of times

• foreach - loops through a block of code for each element in an array

The while Statement

Syntax

while (condition) code to be executed;

The do...while Statement

Syntax

Do {

code to be executed;}

while (condition);

The for Statement

Syntax

for (initialization; condition; increment) { code to be executed; }

The foreach Statement

Syntax

foreach (array as value) { code to be executed; }

PHP Function

<html><body><?php

function writeMyName() {

echo "Kai Jim Refsnes"; }

writeMyName();?></body> </html>

PHP Form Handling

<html>

<body><form action="welcome.php" method="post">

Name: <input type="text" name="name" />Age: <input type="text" name="age" />

<input type="submit" /></form></body> </html>

welcome.php

<html><body>

Welcome <?php echo $_POST["name"]; ?>.<br />

You are <?php echo $_POST["age"]; ?> years old.</body> </html>

Recommended