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
Options ● Perl ● Microsoft ASP.NET ● JSP ● ColdFusion ● XSLT ● others
PHP Strengths ● High Performance ● Interfaces with many database systems ● Built-in libraries ● Low cost ● Open source ● Support ● Easy to learn and use
What is PHP? ● Server-side scripting language ● Conceived in 1994 ● Open Source ● Current version is 5 ● Reference page:
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:
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
Hello World!
Hello World!
Output Hello World!
echo 'Hello World';
Output 1 Hello World
Output 2 Hello World
Hello World! Hello World " ?>
Output 1 Hello World
Output 2 Hello World
PHP Basics
PHP Syntax basics ● PHP code embedded in ● 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 …
Data Types
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
<?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo $first_name; Output: John
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo '$first_name'; Output: $first_name
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo "$first_name"; Output: John
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo "Your name is: $first_name $last_name"; Output: Your name is: John Doe
Strings <?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”
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo "... And you are worth \$2M"; Output:... And you are worth $2M
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo $first_name[0]; Output: J
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo substr('abcdef', 1, 4); Output: bcde
Strings <?php ?> Strings $first_name = 'John'; $last_name = 'Doe'; echo substr($first_name, 0, 6); Output: ? (guess and write it down)
Strings ● No / Single / Double quote ● String type: ● Functions available:
Integers and Floats <?php $a = 1234; $b = 12.02; ?> ● Integer type: ● Float type:
Boolean ● Logical operators: – And: && – Or: ¦¦ – Not: ! ● Boolean type:
Array idFirst NameLast NameJob 1JoeSmithSurgeon 2JohnJohnsonNurse 3JackJacksonConsultant 4JimJonesIT
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 arrays 2. Non-numerically indexed arrays 3. Does it make any difference?
1. Numerically indexed array <?php $names = array( ); ?> 'Joe', 'John', 'Jack','Jim' echo $names[1]; Output: John
2. Non-numerically indexed array <?php $record = array( ?> firstName => 'John', lastName => 'Smith'); echo $record[lastName]; Output: Smith
3. Key-value pairs <?php $arr = array( ); echo $arr[1]; ?> Output: … 0 => 'Joe', 1 => 'John', 2 => 'Jack', 3 => 'Jim'
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")
Multidimensional arrays... echo $record[1][lastName]; echo ' '; echo $record[1][1]; ?> Output: …
Operators Directed study
Control structures
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!”;
switch <?php switch ($value){ }?> case 24: echo "Correct!"; break; case 25: echo "Almost correct!"; break; default: echo "Too much!";
for <?php for ( ; ; ) { } ?> ($i = 1$i <= 10 $i++ echo $i; Output:
foreach <?php ?> $arr = array("one", "two", "three"); echo "Using a for loop: \n"; for ($i = 0; $i < sizeof($arr); $i++) { echo "Value: $arr[$i] \n"; } echo "Using a foreach loop: \n"; foreach ($arr as $value) { echo "Value: $value \n"; }
while <?php $i = 1; while ($i <= 10) { echo $i; i++; } Output:
Summary ● Basics ● Data types ● Control structures ● Operators - Directed study: – Arithmetic operators – Comparison operators – Logical operators – String operators