Lecture 7 - PHP - Part 1
Lecture 7 - PHP - Part 1
os
Internet
Network Core
Server: responds
• Webserver supports
HTTP.
Server
Web
server My codes
PHP
HTTP HTML MySQL
interpreter
Client
Operating System
Web
TCP/IP
browser
Internet
What’s happening?
Client (browser) Web server
Find hello.php
Parse the file
Run php parts through PHP interpreter
Deliver resulting document to port 80
HTTP/1.1 200 OK
(document body)
Display resulting
document on the
screen
PHP Designer
Configure your PHP editor to access the PHP interpreter
From the menu bar, select Tools/Preferences
1st PHP program
Enter this text in a file called “hello.php”
<html>
<head><title>Hello</title></head>
<body>
<?php
print "<h1>Hi there!</h1>";
?>
</body>
</html>
PHP code is executed on the server, and the plain HTML result is
sent to the browser.
Basic Syntax
• a PHP scripting block always starts with <?php and ends with ?>.
• a PHP scripting block can be placed anywhere in the document.
• contains HTML tags, just like an HTML file, and some PHP scripting code.
• The file must have a .php extension. If the file has a .html extension, the PHP
code will not be executed.
<?php
?>
Try the following
<html><head><title>1st PHP</title></head><body>
<?php
print "This line was PHP generated";
?>
<p> This line was not... </p>
<?php
print "This line was also PHP generated";
?>
</body></html>
Comments
C-style:
// this is a comment
Multi-line comments:
/* this is a comment
this is the continuation of the comment */
Boolean boo
Integer int
Float flo
String str
Array arr
Object obj
PHP Variables
Convention:
$strName1='some name';
$intNumber1=2000000000;
$floNumber2=2.456;
$floNumber3=1.8e308; //notice scientific notation
PHP Constants
By default, case-sensitive as are variables
define(“DEFAULT_SCRIPT”, “php”);
define(“MIN_SPEED”, 2);
define(“DEFAULT_SCRIPT”, “php”,TRUE);
You can turn a constant variable case-insensitve
using a third argument set to TRUE.
PHP Operators
=, ==, +, -, /, *, &&, ||, !, ++, --, %, /=, *=, >, <, >=, <=, &, |, ^, ~
– All similar to C
Additionally,
String concatenation
. $fullName = $firstName . “ “ . $lastName;
Identical test operator:
operator same type as well as same
===, !=== value
Error suppression command
@ When placed in front of an expression in PHP, any
error messages that might be generated by that
expression will be ignored.
Back tick to execute shell commands (be careful for
`` variations on different platforms)
Execution Operator (` `)
Note that these are not single-quotes!
<?php
$output = `dir *.php`;
echo "<pre>$output</pre>";
?>
<html>
<head><title>Date of Birth</title></head>
Output: Array
( [0] =>
1 [1] =>
2 [2] =>
3 [3] =>
4 [4] =>
5 )
Creating & printing Arrays
<?php
$arr = array(“candy" => "bar", 12 => true);
Note: Attempting to access an array key which has not been defined is the same as
accessing any other undefined variable: an E_NOTICE-level error message will be
issued, and the result will be NULL.
Auto-indexing
<?php
// This array is the same as ...
array(5 => 43, 32, 56, "b" => 12);
// ...this array
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Printing Array Elements
<?php
$array = array(5=>43, 32, 56, "b"=> 12);
print("\$array[\"b\"]=");
echo $array["b"];
print "<br>";
print("\$array[\"b\"]= $array['b']");
print("\$array[\"b\"]= {$array['b']}");
print("\$array[\"b\"]= {$array[“b”]}");
print("\$array[\"b\"]= $array["b"]");
?>
<?php
$arr = array(5 => 1, 12 => 2);
Array (
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
int array_push()
Push one or more elements onto the end of array.
Returns the new number of elements
<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
mixed array_pop()
pops and returns the last value of the array, shortening the array by one
element. If array is empty (or is not an array), NULL will be returned. Will
additionally produce a Warning when called on a non-array.
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
Array
(
[0] => orange
[1] => banana
[2] => apple
)
Iterating through Arrays
foreach (array_expression as $value) {
statement
}
Example: • Loops over the array given
<html> by array_expression.
<body> • On each loop, the value of the
<?php current element is assigned
to $value and the internal array
$x=array(“red",“green",“blue"); pointer is advanced by one (so on the
foreach ($x as $value) { next loop, you'll be looking at the next
element).
echo $value . "<br />"; • Note: acts on a copy of the array
}
Output:
?>
red
</body> green
</html> blue
Iterating through Arrays
foreach (array_expression as $key => $value)
statement
Example: • Loops over the array given
<html> by array_expression.
<body>
<?php • On each loop, the current element’s
$numbers = array("one"=>"une", "two"=>"deux", key is assigned to the variable $key.
"three"=>"trois", "four"=>"quatre", "five"=>"cinq");
• Note: acts on a copy of the array
foreach($numbers as $key=>$val) {
print "English: " . $key . ", French " . $val . "<br/>";
}
Output:
?> English: one, French une
</body>
English: two, French deux
</html> English: three, French trois
English: four, French quatre
English: five, French cinq
PHP Control Structures
• if, else, switch … case …, break, continue
– Similar to C, but must use brackets {} always
• elseif
– Different syntax to C, same semantics
• While loop …, do … while loop, for loop…
– All similar to C
PHP Control Structures
switch(expression){
case $Example: //variable name
statements;
break;
case “text”:
//…
case 75:
//…
case -123.45:
//…
default:
//…
}
Summary
• PHP runs in the web server
• The server creates HTML dynamically
• It is more efficient than using CGIs
• Variables
• Arrays