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

28
Introduction to PHP

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

Page 1: 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.

Introduction to PHP

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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)

Page 5: 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.

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

Page 6: 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.

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” ?>

Page 7: 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.

• 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

Page 8: 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.

• 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

Page 9: 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.

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

Page 10: 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.

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”

Page 11: 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.

• 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.

Page 12: 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.

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

Page 13: 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.

Basic PHP Syntax

<?php ?>

Page 14: 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.

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"]

Page 15: 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.

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>

Page 16: 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.

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)

Page 17: 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.

Comments in PHP

<html><body><?php

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

?></body> </html>

Page 18: 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.

The If...Else Statement

Syntax

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

else

code to be executed if condition is false;

Page 19: 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.

Example

<html><body><?php

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

echo "Have a nice weekend!";else

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

Page 20: 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.

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;

}

Page 21: 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.

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

Page 22: 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.

The while Statement

Syntax

while (condition) code to be executed;

Page 23: 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.

The do...while Statement

Syntax

Do {

code to be executed;}

while (condition);

Page 24: 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.

The for Statement

Syntax

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

Page 25: 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.

The foreach Statement

Syntax

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

Page 26: 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.

PHP Function

<html><body><?php

function writeMyName() {

echo "Kai Jim Refsnes"; }

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

Page 27: 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.

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>

Page 28: 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.

welcome.php

<html><body>

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

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