php h ypertext p re-processor. unit 6 - php - hello world! - data types - control structures -...

48
PHP PHP Hypertext Pre-processor

Upload: leslie-simmons

Post on 26-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

PHPPHP

Hypertext

Pre-processor

Page 2: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Unit 6 - PHP

- Hello World!- Data types- Control structures- Operators

Page 3: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Why do we need PHP?

● Dynamic data-driven web sites– Display dynamic information to users– Collect information from users

● Examples:– Online shopping site– Web forum– Newspaper site– etc

Page 4: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Options

● Perl● Microsoft ASP.NET● JSP● ColdFusion● XSLT● others

Page 5: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

PHP Strengths

● High Performance● Interfaces with many database systems● Built-in libraries● Low cost● Open source● Support● Easy to learn and use

Page 6: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

What is PHP?

● Server-side scripting language● Conceived in 1994● Open Source● Current version is 5● Reference page: http://www.php.net

Page 7: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

How to use this lecture

● Just listen to it● You won’t remember everything – that’s

ok. That’s normal.● Ask questions if they occur to you.● Refer back to this presentation if you

need to.● Or refer to: http://www.php.net

Page 8: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

How it works

● Client requests document● Server loads document in memory● Server processes document with

relevant module (PHP)● Server sends HTML document to client● Client displays document

Page 9: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Hello World!

Page 10: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Hello World!<html><body>

</body></html>

Hello World!

Page 11: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Output

Hello World!

Page 12: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Hello World!<html><body>

</body></html>

<?php ?> echo 'Hello World';

Page 13: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Output 1<html><body>

Hello World</body></html>

Page 14: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Output 2

Hello World

Page 15: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Hello World!<html><body> <?php echo "<p><i>Hello</i> <b>World</b></p>" ?>

</body></html>

Page 16: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Output 1<html><body>

<p><i>Hello</i> <b>World</b></p></body></html>

Page 17: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Output 2

Hello World

Page 18: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

PHP Basics

Page 19: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

PHP Syntax basics

● PHP code embedded in <?php … ?>● Files containing PHP code must have a .php extension (or

php3, etc)● Syntax very similar to C and Java

– Statements delimited by ;– Comments as in C/Java:

● /* comment */● // comment

– But also: # comment● Variables preceded by $, for example:

– $x = 2;– $first_name = joe;

● More on variables later …

Page 20: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Data Types

Page 21: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Data types

● Integer – for whole numbers● Float – for real numbers● String – for strings of characters● Boolean – for true or false values● Array – For collections of data● Object – For OO programming

Page 22: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo $first_name;

Output:John

Page 23: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Strings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo '$first_name';

Output:$first_name

Page 24: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Strings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo "$first_name";

Output:John

Page 25: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Strings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo "Your name is: $first_name $last_name";

Output:Your name is: John Doe

Page 26: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

StringsStrings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo "You said: \"my name is $first_name $last_name\"";

Output:Your said: “my name is John Doe”

Page 27: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

StringsStringsStrings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo "... And you are worth \$2M";

Output:... And you are worth $2M

Page 28: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

StringsStringsStringsStrings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo $first_name[0];

Output:J

Page 29: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

StringsStringsStringsStringsStrings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo substr('abcdef', 1, 4);

Output:bcde

Page 30: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

StringsStringsStringsStringsStringsStrings

<?php

?>

Strings

$first_name = 'John';$last_name = 'Doe';

echo substr($first_name, 0, 6);

Output:? (guess and write it down)

Page 31: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Strings

● No / Single / Double quote● String type:

http://uk.php.net/manual/en/language.types.string.php

● Functions available: http://uk.php.net/strings

Page 32: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Integers and Floats

<?php$a = 1234;$b = 12.02;

?>

● Integer type: http://uk.php.net/manual/en/language.types.integer.php

● Float type: http://uk.php.net/manual/en/language.types.float.php

Page 33: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Boolean

● Logical operators:– And: &&– Or: ¦¦– Not: !

● Boolean type: http://uk.php.net/manual/en/language.types.boolean.php

Page 34: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Array

id First Name Last Name Job

1 Joe Smith Surgeon

2 John Johnson Nurse

3 Jack Jackson Consultant

4 Jim Jones IT

Page 35: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Array

• Fundamental data structure in PHP• Used to store collections of values• Multidimensional array: array that

contains arrays.• Different types of arrays:

1. Numerically indexed arrays2. Non-numerically indexed arrays3. Does it make any difference?

Page 36: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

1. Numerically indexed array

<?php$names = array( );

?>

'Joe','John','Jack','Jim'

echo $names[1];

Output:John

Page 37: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

2. Non-numerically indexed array

<?php$record = array(

?>

firstName => 'John',lastName => 'Smith');echo $record[lastName];

Output:Smith

Page 38: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

3. Key-value pairs

<?php$arr = array(

);echo $arr[1];

?>Output:…

0 => 'Joe',1 => 'John',2 => 'Jack',3 => 'Jim'

Page 39: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Multidimensional arrays

<?php$record = array(

);

/* i.e. an array of arrays */

array(firstName => 'Joe', lastName => "McDonald"),

array(firstName => 'John', lastName => "Smith"),

array(firstName => 'Jack', lastName => "Black")

Page 40: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Multidimensional arrays

...echo $record[1][lastName];echo '<br/>';echo $record[1][1];?>

Output:…

Page 41: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Operators

Directed studyhttp://uk.php.net/manual/en/language.operators.php

Page 42: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Control structureshttp://uk.php.net/manual/en/language.control-structures.php

Page 43: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

if, else, elseif

<?php$value = 24;

?>

if ($value < 20) echo “not enough!”;

elseif ($value < 30) echo “reasonable.”;

elseif ($value < 40) echo “perfect!”;

else echo “Too much!”;

Page 44: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

switch

<?phpswitch ($value){

}?>

case 24: echo "Correct!"; break;case 25: echo "Almost correct!"; break;default:

echo "Too much!";

Page 45: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

for

<?php for ( ; ; ) {

}?>

($i = 1 $i <= 10 $i++

echo $i;

Output:12345678910

Page 46: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

foreach

<?php

?>

$arr = array("one", "two", "three");

echo "Using a for loop:<br />\n";

for ($i = 0; $i < sizeof($arr); $i++) {

echo "Value: $arr[$i]<br />\n";

}echo "Using a foreach loop:<br />\n";

foreach ($arr as $value) {

echo "Value: $value<br />\n";

}

Page 47: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

while

<?php

$i = 1;while ($i <= 10) {   echo $i; i++; 

} Output:12345678910

Page 48: PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators

Summary

● Basics● Data types● Control structures● Operators - Directed study:

– Arithmetic operators– Comparison operators– Logical operators– String operators

http://uk.php.net/manual/en/language.operators.php