Lab4 PHP

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Hypertext

Preprocessor
Eng. Nada Bahaa Ibrahim
What is PHP?

PHP (recursive acronym It is a widely-used open-


for PHP: Hypertext source general-purpose
Preprocessor). scripting language.

It is especially suited for


PHP supports a wide
web development and can
range of databases.
be embedded into html.
Why PHP?

Fast, flexible and PHP is mainly focused on


pragmatic. server-side scripting.

PHP can be used on all


PHP supports a wide
major operating systems
range of databases.
and web servers.
PHP switch Statement

• The switch statement is used to select one of many blocks of code to be executed.
<?php

$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}

?>
PHP Loops
• The loops are used to repeat code a certain number of times.
• In PHP, we have the following loop types:

• while - loops through a block of code if the specified condition is true.


• do...while - loops through a block of code once, and then repeats the loop if
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.
PHP while Loop

• The while loop executes a block of code if the specified condition is true.
<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}

?>
PHP do…..while Loop

• The do...while loop - Loops through a block of code once, and then
repeats the loop if the specified condition is true.
<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
PHP for Loop

• The for loop is used when you know in advance how many times the
script should run.
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
PHP foreach Loop

• The foreach loop works only on arrays and is used to loop through each
key/value pair in an array.
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";}
?>
PHP foreach Loop

• The foreach loop works only on arrays and is used to loop through each
key/value pair in an array.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {


echo "$x = $val<br>";
}
?>
PHP Functions

• The real power of PHP comes from its functions.


• PHP has more than 1000 built-in functions, and in addition you can
create your own custom functions (PHP built-in functions).
• A function is a block of statements that can be used repeatedly in a
program that can not be executed automatically when a page loads but
by a call to the function.
• A function name must start with a letter or an underscore.
• Function names are NOT case-sensitive.
PHP Functions

<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
PHP Functions

<?php declare(strict_types=1); // strict requirement


function setHeight(int $minheight = 50) {
echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
PHP Functions

<?php declare(strict_types=1); // strict requirement


function sum(int $x, int $y) {
$z = $x + $y;
return $z;
}

echo "5 + 10 = " . sum(5, 10) . "<br>";


echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
PHP Functions

<?php declare(strict_types=1); // strict requirement


function addNumbers(float $a, float $b) : float {
return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>
PHP Arrays

• An array stores multiple values in one single variable.


• In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index.
• Associative arrays - Arrays with named keys.
• Multidimensional arrays - Arrays containing one or more arrays.
• For a complete reference of all array functions, go to complete
PHP Array Reference.
PHP Indexed Arrays

<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {


echo $cars[$x];
echo "<br>";
}
?>
PHP Associative Arrays

• Associative arrays are arrays that use named keys that you assign to
them.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
PHP Multidimensional Arrays

• A multidimensional array is an array containing one or more arrays.


• PHP supports multidimensional arrays that are two, three, four, five, or
more levels deep. However, arrays more than three levels deep are hard
to manage for most people.
• A two-dimensional array is an array of arrays.
PHP Multidimensional Arrays

<?php for ($row = 0; $row < 4; $row++) {


$cars = array ( echo "<p><b>Row number
$row</b></p>";
array("Volvo",22,18),
echo "<ul>";
array("BMW",15,13),
for ($col = 0; $col < 3; $col++) {
array("Saab",5,2),
echo
array("Land Rover",17,15) "<li>".$cars[$row][$col]."</li>";
); }
echo "</ul>";
}
?>
PHP Sorting Arrays

• The elements in an array can be sorted in alphabetical or numerical


order, descending or ascending.
• In PHP, there are PHP array sort functions:
• sort() - sort arrays in ascending order
• rsort() - sort arrays in descending order
• asort() - sort associative arrays in ascending order, according to the value
• ksort() - sort associative arrays in ascending order, according to the key
• arsort() - sort associative arrays in descending order, according to the value
• krsort() - sort associative arrays in descending order, according to the key
PHP Sorting Arrays

• EX1:
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>
• EX2:
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Reference

• https://www.php.net/
• https://www.w3schools.com/php/default.asp

You might also like