Week 2 Lecture
Week 2 Lecture
02
SADAM HUSSAIN
Exercise
1. Answer
for($x=1;$x<=5;$x++)
for ($y=1;$y<=$x;$y++)
echo "*";
if($y< $x)
echo "\n";
}
While Loop statement
Example practice:
Fac of 3: 3*2*1
Fac of 5: 5*4*3*2*1
Fac of 6: 6*5*4*3*2*1
While Loop statement
Example practice:
$num = 3;
$factorial = 1;
The while statement will execute a block of code if and as long as a test
expression is true.
If the test expression is true, then the code block will be executed. After the
code has executed the test expression will again be evaluated and the loop
will continue until the test expression is found to be false
While Loop statement
Syntax
while (condition is true) {
code to be executed;
}
Example:
<?php
$x = 1;
Syntax
do {
code to be executed;
} while (condition is true);
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 20);
?>
Do…While Loop statement
<?php
$i = 0
do
{
print "hi";
$i++;
}
while ($i != 3);
?>
Do…While Loop statement
<?php
$i = 0
while ($i < 3)
{
print "hi";
$i--;
}
print "hello"
?>
Do…While Loop statement
<?php
$i = "";
while($i)
{
print "hi";
}
print "hello";
?>
PHP Functions
Syntax
function functionName() {
code to be executed;
}
PHP Functions
<?php
function abc() {
echo "Hello world!";
}
Ex1:
function abc($fname) {
echo "$fname.<br>";
}
abc(“ali");
abc(“hamza");
Ex2:
abcd(“ali", “2001");
abcd(“hamza", “2002");
PHP Function
PHP is a Loosely Typed Language. PHP automatically associates a data type to the variable, depending on its value
Ex1:
function numbersAdd($a,$b) {
return $a + $b;
}
echo numbersAdd(5, "5 days");
Ex1:
declare(strict_types=1); // strict requirement
// since strict is enabled and "5 days" is not an integer, an error will
be thrown
PHP Function Arguments
Default Argument Value
Ex1:
declare(strict_types=1); // strict requirement
setHeight(50);
setHeight(100);
setHeight(30);
setHeight(); // will use the default value of 60
PHP FUNCTION Returning values
Returning values
Ex1:
declare(strict_types=1); // strict requirement
function add(int $x, int $y) {
$z = $x + $y;
return $z;
}
Ex1:
Write a PHP script to print all even numbers between 1 to 20
Output: 2 3 5 8 10 12 14 16 18 20
PHP FUNCTION Returning values
Ex1: Solution
<?php
$num=0;
while ($num < 20) {
$num = $num +2;
echo $num, "\n";
}
?>
PHP example
Ex2:
Write a PHP script to print all odd numbers between 1 to 20
Output: 1 3 5 7 9 11 13 15 17 19
PHP FUNCTION Returning values
Ex2: Solution
<?php
$num = 1;
while ( $num <= 20 ) {
print "$num";
$num += 2;
}
?>
PHP FUNCTION
Ex3:
In PHP, the array() function is used to create an array: but we can directly
create array like below:
<?php
$var[0] = "a";
$var[1] = "y";
$var[2] = "h";
$var[3] = "e";
$var[4] = "u";
echo $var[2];
// outout h
?>
PHP ARRAYS
An array stores multiple values in one single variable: An array is a special variable, which
can hold more than one value at a time.
Ex1:
There are two ways to create indexed arrays:
$color = array(“red", “green", “blue");
$color[0] = “red";
$color[1] = "green";
$color[2] = "blue";
echo “value at index 0 and 1 " . $color[0] . ", " . $color[1] . " and at 2 " . $color[2] . ".";
or:
$age['ali'] = "35";
$age[‘taha'] = "37";
$age[‘hamza'] = "43";
$marks = array (
array(“ali",75,90),
array(“hamza",88,81),
array(“taha",79,80),
array(“sana",83,77)
);
ow the two-dimensional $marks array contains four arrays, and it has two indices:
row and column.
To get access to the elements of the $marks array we must point to the two indices
(row and column):
ARRAYS- PHP Multidimensional
Arrays