PHP Introduction
PHP Introduction
PHP Introduction
Wednesday, Ju 2
Example
<h1>Hello from Dr. Chuck's HTML Page</h1>
<p>
<?php
echo "Hi there.\n";
$answer = 6 * 7;
echo "The answer is $answer, what ";
echo "was the question again?\n";
?>
</p>
<p>Yes another paragraph.</p>
Wednesday, Ju 3
Example
<h1>Hello from Dr. Chuck's HTML Page</h1>
<p>
<?php
echo "Hi there.\n";
$answer = 6 * 7;
echo "The answer is $answer, what ";
echo "was the question again?\n";
?>
</p>
<p>Yes another paragraph.</p>
Wednesday, Ju 4
PHP from the Command Line
Wednesday, Ju 5
Keywords
Wednesday, Ju 6
Variable Names
http://php.net/manual/en/language.variables.basics.php
Wednesday, Ju 7
Variable Name Weirdness
$x = 2; $x = 2;
$y = x + 5; y = $x + 5;
print $y; print $x;
5 Parse error
Wednesday, Ju 8
Variable Name Weirdness
• Things that look like variables but are missing a dollar sign as
an array index are unpredictable....
$x = 5;
$y = array("x" => "Hello");
print $y[x];
Hello
Wednesday, Ju 9
Strings / Different + Awesome
Wednesday, Ju 10
<?php Double Quote
echo "this is a simple string\n";
echo "You can also have embedded newlines in
strings this way as it is
okay to do";
// Outputs: This will expand:
// a newline
echo "This will expand: \na newline";
// Outputs: Variables do 12
$expand = 12;
echo "Variables do $expand\n";
Wednesday, Ju 11
<?php Single Quote
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
Wednesday, Ju 12
Comments in PHP
echo 'This is a test'; // This is a c+
+ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a shell-
style comment
Wednesday, Ju 13
Output
Wednesday, Ju 14
Expressions
<?php
$x = "15" + 27;
echo($x); 42
echo("\n");
?>
Wednesday, Ju 15
Expressions
Wednesday, Ju 16
Increment / Decrement
$x = 12;
$y = 15 + $x++; x is 13 and y is 27
echo "x is $x and y is $y \n";
Wednesday, Ju 17
Operators of Note
• Increment / Decrement ( ++ -- )
• String concatenation ( . )
• Equality ( == != )
• Identity ( === !== )
• Ternary ( ? : )
• Side-effect Assignment ( += -= .= etc.)
• Ignore the rarely-used bitwise operators ( >> << ^ |
&)
Wednesday, Ju 18
Increment / Decrement
$x = 12;
$y = 15 + $x; x is 13 and y is 27
$x = $x + 1;
echo "x is $x and y is $y \n";
Wednesday, Ju 19
String Concatenation
Wednesday, Ju 20
Ternary
• The ternary operator comes from C. It allows
conditional expressions. It is like a one-line if-then-
else. Like all “contraction” syntaxes, we must use it
carefully.
$www = 123;
$msg = $www > 100 ? "Large" : "Small" ;
echo "First: $msg \n";
$msg = ( $www % 2 == 0 ) ? "Even" : "Odd";
echo "Second: $msg \n";
First: Large
$msg = ( $www % 2 ) ? "Odd" : "Even";
Second: Odd
echo "Third: $msg \n"; Third: Odd
Wednesday, Ju 21
Side-Effect Assignment
echo "\n";
$out = "Hello";
$out = $out . " ";
$out .= "World!";
$out .= "\n"; Hello World!
echo $out; Count: 1
$count = 0;
$count += 1;
echo "Count: $count\n";
Wednesday, Ju 22
Conversion / Casting
Wednesday, Ju 23
Casting In PHP, division forces
operands to be floating point.
PHP converts expression
$a = 56; $b = 12; values silently and
$c = $a / $b; aggressively.
echo "C: $c\n";
$d = "100" + 36.25 + TRUE;
echo "D: ". $d . "\n";
echo "D2: ". (string) $d . "\n";
C: 4.66666666667
$e = (int) 9.9 - 1;
D: 137.25
echo "E: $e\n"; D2: 137.25
$f = "sam" + 25; E: 8
echo "F: $f\n"; F: 25
$g = "sam" . 25; G: sam25
echo "G: $g\n";
Wednesday, Ju 24
PHP vs. Python
$x = "100" + 25; x = int("100") + 25
echo "X: $x\n"; print "X:", x
$y = "100" . 25; y = "100" + str(25)
echo "Y: $y\n"; print "Y:", y
$z = "sam" + 25; z = int("sam") + 25
echo "Z: $z\n"; print "Z:", z
X: 125 X: 125
Y: 10025 Y: 10025
Z: 25 Traceback:"cast.py", line 5
z = int("sam") + 25;
ValueError: invalid literal
Wednesday, Ju 25
Casting
AB
X1Y
Wednesday, Ju 26
Equality versus Identity
Wednesday, Ju 27
$vv = "Hello World!";
echo "First:" . strpos($vv, "Wo") . "\n";
echo "Second: " . strpos($vv, "He") . "\n";
echo "Third: " . strpos($vv, "ZZ") . "\n";
if (strpos($vv, "He") == FALSE ) echo "Wrong A\n";
if (strpos($vv, "ZZ") == FALSE ) echo "Right B\n";
if (strpos($vv, "He") !== FALSE ) echo "Right C\n";
if (strpos($vv, "ZZ") === FALSE ) echo "Right D\n";
print_r(FALSE); print FALSE;
echo "Where were they?\n"; First:6
Second: 0
Third:
Wrong A
Right B
Right C
Beware FALSE variables. They are detectable but not visible... Right D
Where were they?
Wednesday, Ju 28
Conditional - if
• Logical operators ( == != < > <= >= && || ! )
• Curly braces
<?php
$ans = 42;
if ( $ans == 42 ) {
print "Hello world!\n";
} else {
print "Wrong answer\n"; Hello World!
}
?>
Wednesday, Ju 29
Whitespace Does Not Matter
<?php
$ans = 42;
if ( $ans == 42 ) {
print "Hello world!\n";
} else {
print "Wrong answer\n";
}
?>
<?php $ans = 42; if ( $ans == 42 ) { print
"Hello world!\n"; } else { print "Wrong answer\n"; }
?>
Wednesday, Ju 30
Which Style do You Prefer?
<?php
$ans = 42;
<?php if ( $ans == 42 )
$ans = 42; {
if ( $ans == 42 ) { print "Hello world!\n";
print "Hello world!\n"; }
} else { else
print "Wrong answer\n"; {
} print "Wrong answer\n";
?> }
Aesthetics ?>
Wednesday, Ju 31
Multi-way x<2
yes
print 'Small'
no
$x = 7;
yes
if ( $x < 2 ) { x<10 print 'Medium'
print "Small\n";
} elseif ( $x < 10 ) { no
print "Medium\n";
} else { print 'LARGE'
print "LARGE\n";
}
Wednesday, Ju 32
Curly Braces are Not Required
if ($page == "Home") echo "You selected Home";
elseif ($page == "About") echo "You selected About";
elseif ($page == "News") echo "You selected News";
elseif ($page == "Login") echo "You selected Login";
elseif ($page == "Links") echo "You selected Links";
Wednesday, Ju 33
$fuel = 10;
while ($fuel > 1) {
print "Vroom vroom\n";
}
Wednesday, Ju 34
$count = 1;
do {
echo "$count times 5 is " . $count * 5;
echo "\n";
} while (++$count <= 5);
Wednesday, Ju 35
for($count=1; $count<=6; $count++ ) {
echo "$count times 6 is " . $count * 6;
echo "\n";
}
1 times 6 is 6
2 times 6 is 12
3 times 6 is 18
A for loop is the simplest 4 times 6 is 24
way to construct a 5 times 6 is 30
counted loop. 6 times 6 is 36
Wednesday, Ju 36
Loop runs while TRUE (top-test)
Run after each iteration.
Before loop starts
Wednesday, Ju 37
Breaking Out of a Loop
• The break statement ends the current loop and jumps to
the statement immediately following the loop.
Count: 1
for($count=1; $count<=10; $count++ ) { Count: 3
if ( ($count % 2) == 0 ) continue; Count: 5
echo "Count: $count\n"; Count: 7
} Count: 9
echo "Done\n"; Done
Wednesday, Ju 39
Summary
Wednesday, Ju 40