You are on page 1of 35

CENG420

Web Programming & Technologies

Fall 2019 - 2020

Introduction to PHP

Dr. Ismail El Sayad (www.ismailelsayad.info)

[Adapted from Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition, by
Robin Nixon. Published by O'reilly , © 2012
&
Dr. Charles Severance Lectures]

1 El Sayad, LIU, Fall 2019 - 2020


About the PHP Language
 Syntax is inspired by C
 Curly braces, semicolons, no signficant whitespace
 Syntax inspired by perl
 Dollar signs to start variable names, associative arrays
 Extends HTML to add segments of PHP within an
HTML file.

2 El Sayad, LIU, Fall 2019 - 2020


Philosphy of PHP
 You are a responsible and intelligent programmer
 You know what you want to do
 Some flexibility in syntax is OK - style choices are OK
 Lets make this as convenient as possible
 Sometimes errors fail silently

3 El Sayad, LIU, Fall 2019 - 2020


PhP can be written within the HTML code Or can be a standing alone File

<h1>First Php lesson</h1>


<p>
<?php
echo "Hi there.";
$answer = 6 * 7;
echo "The answer is $answer, what ";
echo "was the question again?";?>
</p>
<p>Yes another paragraph.</p>

4 El Sayad, LIU, Fall 2019 - 2020


Variable Names
 Start with a dollar sign ($) followed by a letter or
underscore, followed by any number of letters,
numbers, or underscores
 Case matters

$abc = 12;
$total = 0;
$largest_so_far = 0; Not correct

$2php = 0;
$bad-punc = 0;

http://php.net/manual/en/language.variables.basics.php
5 El Sayad, LIU, Fall 2019 - 2020
Variable Name Weirdness
 Things that look like variables but are missing a
dollar sign can be confusing
$x = 2;
$y = x + 5;
print $y; 5

$x = 2;
y = $x + 5;
print $x; Parse error

6 El Sayad, LIU, Fall 2019 - 2020


Expressions
 Completely normal like other languages ( + - / * )
 More aggressive implicit type conversion
<?php
$x = "15" + 27;
echo($x); 42
?>

7 El Sayad, LIU, Fall 2019 - 2020


Output
 echo is a language construct - can be treated like a
function with one parameter. Without parenthesis, it
accepts multiple parameters.
 print is a function - only one parameter but parenthesis
are optional so it can look like a language construct
<?php $x = "15" + 27;
echo $x;
42
echo("<br/>"); 42
echo $x."<br/>"; 42
42
print $x;
print "<br/>";
print($x);
print("<br/>");?>

8 El Sayad, LIU, Fall 2019 - 2020


Conditional - if
 Logical operators ( == != < > <= >= and
or ! )
 Curly braces
 <?php
$ans = 42;
if ( $ans == 42 )
{print "Hello world!"; }
else
{print "Wrong answer"; }?>
Hello World!

9 El Sayad, LIU, Fall 2019 - 2020


White space does not matter
<?php $ans = 42;
if ( $ans == 42 ) { print
"Hello world!"; }
else { print "Wrong answer"; }?>

<?php $ans = 42; Hello world! Hello world!


if ( $ans == 42 )
{ print "Hello world!"; }
else {print "Wrong answer\n";}?>

10 El Sayad, LIU, Fall 2019 - 2020


Increment / Decrement
 These operators allow you to both retrieve and increment
/decrement a variable
 They are generally avoided in civilized code.
$x = 12;
$y = 15 + $x++; Post increment
echo "x is $x and y is $y";

x is 13 and y is 27

12 El Sayad, LIU, Fall 2019 - 2020


String Concatenation
 PHP uses the period character for concatenation
because the plus character would instructor PHP to do
the best it could do to add the two things together,
converting if necessary.

$a = 'Hello ' . 'World!';


echo $a;

Hello World!

13 El Sayad, LIU, Fall 2019 - 2020


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 use it carefully.
$www = 123;
$msg = ($www > 100) ? "Large" : "Small" ;
echo "First: $msg" . "<br />";
$msg = ( $www % 2 == 0 ) ? "Even" : "Odd";
echo "Second: $msg ". "<br />";
$msg = ( $www % 2 ) ? "Odd" : "Even";
First: Large
echo "Third: $msg ". "<br />"; Second: Odd
Third: Odd

14 El Sayad, LIU, Fall 2019 - 2020


Control Structures

15 El Sayad, LIU, Fall 2019 - 2020


Two-way using else :
$x = 4;
if ($x > 2) {
print "Bigger";
} else {
print "Smaller";
}
print "<br />";
print "All done";

Bigger
All done

16 El Sayad, LIU, Fall 2019 - 2020


Multi-way
$x = 7;
if ( $x < 2 ) {
print "Small";
} elseif ( $x < 10 ) {
print "Medium";
} else {
print "LARGE";
}
print "All done";

MediumAll done

17 El Sayad, LIU, Fall 2019 - 2020


switch
switch ($page)
{
case "Home":
echo "You selected Home";
break;
case "About":
echo "You selected About";
break;
case "News": echo "You selected News";
break;
default:
echo "different";
}
18 El Sayad, LIU, Fall 2019 - 2020
Looping Structures

19 El Sayad, LIU, Fall 2019 - 2020


While loop
$fuel = 10;
while ($fuel > 1) {
print "Vroom vroom";
}
A while loop is a "zero-trip" loop with the test at the top.
before the first iteration starts.

20 El Sayad, LIU, Fall 2019 - 2020


Do-while loop
$count = 1;
do {
echo "$count times 5 is " . $count * 5;
echo "<br />";
} while (++$count <= 5);

A do-while loop is a "one trip“


loop with the test at the 1 times 5 is 5
2 times 5 is 10
bottom after the first 3 times 5 is 15
iteration completes. 4 times 5 is 20
5 times 5 is 25
21 El Sayad, LIU, Fall 2019 - 2020
For loop
for($count=1; $count<=6; $count++ ) {
echo "$count times 6 is " . $count * 6;
echo " <br /> ";
}
 A for loop is the simplest way to construct a counted
loop.

1 times 6 is 6
2 times 6 is 12
3 times 6 is 18
4 times 6 is 24
5 times 6 is 30
6 times 6 is 36

22 El Sayad, LIU, Fall 2019 - 2020


Loop Controls
 Like many C-inspired languages, PHP has two control
structures that work within a loop

 break - exit the loop immediately


 continue - finish the current iteration and jump to the next
iteration, starting at the top of the loop

23 El Sayad, LIU, Fall 2019 - 2020


Breaking Out of a Loop
 The break statement ends the current loop and jumps to
the statement immediately following the loop
 It is like a loop test that can happen anywhere in the
body of the loop

for($count=1; $count<=600; $count++ ) {


if ( $count == 5 ) break;
echo "Count: $count ". "<br />";;
Count: 1
} Count: 2
Count: 3
echo "Done\n"; Count: 4
Done

24 El Sayad, LIU, Fall 2019 - 2020


Finishing an Iteration with continue
 The continue statement ends the current iteration and
jumps to the top of the loop and starts the next iteration

for($count=1; $count<=10; $count++ ) {


if ( $count == 5 ) continue;
echo "Count: $count ". "<br />"; Count: 1
Count: 2
} Count: 3
Count: 4
echo "Done"; Count: 6
Count: 7
Count: 8
Count: 9
Count:10

Done
25 El Sayad, LIU, Fall 2019 - 2020
Conversion / Casting
 As PHP evaluates expressions, at times values in the
expression need to be converted from one type to
another as the computations are done.
 PHP does aggressive implicit type conversion (casting)
 You can also make type conversion (casting) explicit
with casting operators.

26 El Sayad, LIU, Fall 2019 - 2020


Implicit Casting:
$a = 56; $b = 12;
$c = $a / $b;
echo "C: $c". "<br /> ";
$d = "100" + 36.25 + TRUE;
C: 4.66666666667
D: 137.25
echo "D: ". $d . "<br /> "; D2: 137.25
echo "D2: ". (string) $d . "<br /> "; E: 8
$e = (int) 9.9 - 1; F: 25 with warning
echo "E: $e" . "<br /> "; G: sam25
$f = "sam" + 25;
echo "F: $f" . "<br /> ";
$g = "sam" . 25; echo "G: $g" . "<br /> ";
27 El Sayad, LIU, Fall 2019 - 2020
Explicit Casting

$var = "10";
$value = 10 + $var;
$value = (string)$value;
echo “Result is: $value"; // Result is:20

28 El Sayad, LIU, Fall 2019 - 2020


Associative Arrays
 Like Python Dictonaries+Lists - but more powerful
 Can be key => value or simply indexed by numbers
 Ignore two-dimensional arrays for now...

29 El Sayad, LIU, Fall 2019 - 2020


Array-Integer Indices
<?php
$stuff = array("Hi", "There"); There
echo $stuff[1];
?>

<?php
$stuff = Array();
$stuff[] = "Hello";
$stuff[] = "World"; World
echo $stuff[1];
?>

30 El Sayad, LIU, Fall 2019 - 2020


Array-Key / Value
<?php
$stuff = array("name" => “Ismail",
"course" => “CENG420");
echo $stuff["course"] ;?>

CENG420

31 El Sayad, LIU, Fall 2019 - 2020


Dumping an Array
 The function print_r() dumps out PHP data - it is used
mostly for debugging. It prints the information about
some variables in a more human-readable way:
<?php
$stuff = array("name" => "Ismail",
"course" => "CENG420");
print_r($stuff);
?> Array ( [name] => Ismail [course] => CENG420 )

32 El Sayad, LIU, Fall 2019 - 2020


Dumping an Array
 The function print_r() dumps out PHP data - it is used
mostly for debugging
<?php
$stuff = Array();
$stuff[2] = "Hello";
$stuff[9] = "World";
print_r($stuff);
?>

Array ( [2] => Hello [9] => World )

33 El Sayad, LIU, Fall 2019 - 2020


var_dump .vs. print_r
The var_dump() function dumps information about one or more
variables. The information holds type and value of the variable(s).
<?php
$stuff = array("name" => “Ismail",
"course" => “CENG450");
print_r($stuff);
echo "</br>";
var_dump($stuff);
?>

Array ( [name] => Ismail [course] => CENG450 )


array(2) { ["name"]=> string(6) "Ismail" ["course"]=> string(7)
"CENG450" }

34 El Sayad, LIU, Fall 2019 - 2020


var_dump() is more verbose
<?php $thing = FALSE;
echo("One\n");
print_r($thing);
echo("Two\n");
var_dump($thing);?>

One Two bool(false)

35 El Sayad, LIU, Fall 2019 - 2020


Looping Through an Array
<?php
$stuff = array("name" => “Ismail", "course" => “CENG420");
foreach($stuff as $k => $v )
{ echo "key=", $k, " val=", $v, "</br>";}?>

Key=name Val=Ismail
Key=course Val=CENG420

36 El Sayad, LIU, Fall 2019 - 2020

You might also like