Web Extension
PHP
● Control Structures
Content ● Functions
Control Structures
● if statement - executes some code if one condition is true
● if...else statement - executes some code if a condition is true and
another code if that condition is false
Conditional
Statements ● if...elseif...else statement - executes different codes for more than two
conditions
● switch statement - selects one of many blocks of code to be executed
The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
Example:
if Output "Have a good day!" if the current time (HOUR) is less than 20:
<!DOCTYPE html>
Statements <html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
</body>
</html>
The if...else statement executes some code if a condition is true and
another code if that condition is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else { <!DOCTYPE html>
if … else code to be executed if condition is false;
<html>
<body>
Statements } <?php
$t = date("H");
if ($t < "20") {
Example: echo "Have a good day!";
} else {
Output "Have a good day!" echo "Have a good night!";
}
if the current time is less than 20, ?>
and "Have a good night!" otherwise: </body>
</html>
The if...elseif...else statement executes different codes for more than two
conditions.
Syntax
if (condition) {
if … elseif …
else code to be executed if this condition is true;
Statements } elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
<!DOCTYPE html>
Example: <html>
<body>
Output "Have a good morning!"
<?php
if the current time is less than 10, $t = date("H");
echo "<p>The hour (of the server) is " .
and $t;
echo ", and will give the following
message:</p>";
if … elseif … "Have a good day!"
if ($t < "10") {
else if the current time is less than 20.
echo "Have a good morning!";
Statements Otherwise it will output "Have a
} elseif ($t < "20") {
echo "Have a good day!";
good night!": } else {
echo "Have a good night!";
}
?>
</body>
</html>
Use the switch statement to select one of many blocks of code to be executed.
Syntax:
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
switch code to be executed if n=label2;
Statements break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
<!DOCTYPE html>
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
switch case "blue":
echo "Your favorite color is blue!";
Statements break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red,
blue, nor green!";
}
?>
</body>
</html>
● while - loops through a block of code as long as the specified
condition is true
● do...while - loops through a block of code once, and then repeats
Loops the loop as long as the specified condition is true
● for - loops through a block of code a specified number of times
● foreach - loops through a block of code for each element in an
array
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}
Example
The example below displays the numbers from 1 to 5:
<!DOCTYPE html>
<html>
while loop <body>
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
The do...while loop will always execute the block of code once, it will then check
the condition, and repeat the loop while the specified condition is true.
Syntax:
<!DOCTYPE html>
<html>
do { <body>
code to be executed;
<?php
} while (condition is true);
$x = 1;
Example
do {
do..while loop echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
● The for loop - Loops through a block of code a specified number of times.
● The for loop is used when you know in advance how many times the script
should run.
Syntax:
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
for loop }
Parameters:
● init counter: Initialize the loop counter value
● test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop
continues. If it evaluates to FALSE, the loop ends.
● increment counter: Increases the loop counter value
The example below displays the numbers from 0 to 10
Example:
<!DOCTYPE html>
<html>
<body>
<?php
for loop for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
● The foreach loop works only on arrays
● It is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
foreach loop
}
● For every loop iteration, the value of the current array element is
assigned to $value and the array pointer is moved by one, until it
reaches the last array element.
The following example will output the values of the given array ($colors):
<!DOCTYPE html>
<html>
<body>
<?php
foreach loop $colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
</body>
</html>
The following example will output both the keys and the values of the
given array ($age):
<!DOCTYPE html>
<html>
<body>
<?php
foreach loop $age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
foreach($age as $x => $val) {
echo "$x = $val<br>";
}
?>
</body>
</html>
Functions
Functions
● A function is a block of statements that can be used repeatedly in a program.
● A function will not execute automatically when a page loads.
● A function will be executed by a call to the function
Built-in
Functions ● The real power of PHP comes from its functions.
● PHP has more than 1000 built-in functions
● Those functions can be called directly, from within a script, to perform a
specific task.
● you can create your own custom functions
A user-defined function declaration starts with the word function:
A function name must start with a letter or an underscore. Function names are
NOT case-sensitive.
<!DOCTYPE html>
<html>
Syntax <body>
User defined
function functionName() { <?php
Functions function writeMsg() {
code to be executed; echo "Hello world!";
}
}
writeMsg();
?>
</body>
</html>
<!DOCTYPE html>
● Information can be passed to <html>
<body>
functions through arguments.
<?php
● An argument is just like a function familyName($fname) {
echo "$fname morgan.<br>";
variable. }
● Arguments are specified after familyName("Jani");
familyName("Hege");
Function the function name, inside the familyName("Stale");
familyName("Kai Jim");
Arguments parentheses. familyName("Borge");
?>
● You can add as many arguments
</body>
as you want, just separate them </html>
with a comma.
• PHP automatically associates a data type to the variable, depending on its value.
• Since the data types are not set in a strict sense, you can do things like adding a
string to an integer without causing an error.
• In PHP 7, type declarations were added.
• This gives us an option to specify the expected data type when declaring a function,
and by adding the strict declaration, it will throw a "Fatal Error" if the data type
Loosely Typed
Language mismatches.
• In the following example we try to send both a number and a string to the function
without using strict
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will
return 10
?>
• To specify strict we need to set declare(strict_types=1);. This must be on
the very first line of the PHP file.
• In the following example we try to send both a number and a string to
the function, but here we have added the strict declaration
Loosely Typed <?php declare(strict_types=1); // strict requirement
Language function addNumbers (int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer, an error will
be thrown
?>
<?php declare(strict_types=1); // strict
requirement ?>
<!DOCTYPE html>
<html>
<body>
<?php
function setHeight(int $minheight = 50) {
echo "The height is : $minheight <br>";
Default values }
setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>
</body>
</html>
<?php declare(strict_types=1); // strict
requirement ?>
<!DOCTYPE html>
<html>
<body>
<?php
function sum(int $x, int $y) {
$z = $x + $y;
Returning return $z;
values }
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>
</body>
</html>
• PHP 7 also supports Type Declarations for the return statement.
• Like with the type declaration for function arguments, by enabling the strict
requirement, it will throw a "Fatal Error" on a type mismatch.
• To declare a type for the function return, add a colon ( : ) and the type right
before the opening curly ( { )bracket when declaring the function.
Return Type
Declaration <?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : float {
return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>
• In PHP, arguments are usually passed by value, which means that a copy of the
value is used in the function and the variable that was passed into the function
cannot be changed.
• When a function argument is passed by reference, changes to the argument also
change the variable that was passed in.
Passing • To turn a function argument into a reference, the & operator is used:
arguments by <?php
function add_five(&$value) {
reference $value += 5;
}
$num = 2;
add_five($num);
echo $num;
?>
Title Lorem
Ipsum Dolor
LOREM IPSUM LOREM IPSUM LOREM IPSUM
DOLOR DOLOR DOLOR