0% found this document useful (0 votes)
7 views

Week 2 Lecture

week 2 lecture

Uploaded by

Aftab Jamali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Week 2 Lecture

week 2 lecture

Uploaded by

Aftab Jamali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Web Development week

02
SADAM HUSSAIN
Exercise

A program to check 1233456 is odd or even is shown ?


Exercise
A program to check 1233456 is odd or even is shown ?
Answer:
$number=1233456;
if($number%2==0)
{
echo "$number is Even Number";
}
else
{
echo "$number is Odd Number";
}
While Loop statement
Example practice:

1 write program yo output following


While Loop statement
Example practice:

1. Answer

for($x=1;$x<=5;$x++)

for ($y=1;$y<=$x;$y++)

echo "*";

if($y< $x)

{ echo " ";

echo "\n";

}
While Loop statement
Example practice:

2 : Write a factorial program using for loop in php ….. ??

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:

2 : Write a factorial program using for loop in php …. Answer

$num = 3;

$factorial = 1;

for ($x=$num; $x>=1; $x--)

$factorial = $factorial * $x;

echo "The factorial of $num is $factorial";


While Loop statement

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;

while($x <= 15) {


echo "The number is: $x <br>";
$x++;
}
?>
While Loop statement
Do…While Loop statement
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
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

What will be the output of the following PHP code?

<?php
$i = 0
do
{
print "hi";
$i++;
}
while ($i != 3);
?>
Do…While Loop statement

What will be the output of the following PHP code?

<?php
$i = 0
while ($i < 3)
{
print "hi";
$i--;
}
print "hello"
?>
Do…While Loop statement

What will be the output of the following PHP code?

<?php
$i = "";
while($i)
{
print "hi";
}
print "hello";
?>
PHP 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.

Syntax
function functionName() {
code to be executed;
}
PHP Functions

<?php
function abc() {
echo "Hello world!";
}

abc(); // call the function


?>
PHP Function Arguments
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as
you want, just separate them with a comma.

Ex1:
function abc($fname) {
echo "$fname.<br>";
}

abc(“ali");
abc(“hamza");

Ex2:

function abcd($fname, $year) {


echo "$fname. Born in $year <br>";
}

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");

// strict is NOT enabled "5 days" is changed to int(5), and it will


return 10
PHP Function
To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.

Ex1:
declare(strict_types=1); // strict requirement

function numbersAdd(int $a, int $b) {


return $a + $b;
}
echo numbersAdd(5, "5 days");

// 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

function setHeight(int $minheight = 60) {


echo "The height is : $minheight <br>";
}

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;
}

echo “15 + 8 = " . add(15, 8) . "<br>";


echo “5 + 3 = " . add(5, 3) . "<br>";
echo "20 + 14 = " . add(20, 14);
PHP example

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:

Write a PHP program to check if a person is eligible to vote


How to do it.. ?

• Minimum age required for vote is 18.


• You can use PHP Functions.
• You can use Decision Making Statements
PHP FUNCTION Returning values
Ex3: Solution
function check_vote() //function has been declared {
$name = "Rakesh"; $age = 19;
if ($age >= 18) {
echo $name . ", you Are Eligible For Vote";
} else {
echo $name . ", you are not eligible for vote. ";
}
}

check_vote(); //function has been called


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.
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.

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.

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
ARRAYS- PHP Indexed Arrays
The index can be assigned automatically (index always starts at 0), like this:

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] . ".";

The count() Function


The count() function is used to return the length (the number of elements) of an array:
$color = array("red", "green", "blue");
echo count($color);
ARRAYS- PHP Indexed Arrays
Loop Through an Indexed Array

$color = array(“red", “green", “blue");


$arrlength = count($color);

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


echo $color[$x];
echo "<br>";
}

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


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
ARRAYS- PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them

There are two ways to create an associative array:

$age = array(“ali"=>"35", "taha"=>"37", "hamza"=>"43");

or:
$age['ali'] = "35";
$age[‘taha'] = "37";
$age[‘hamza'] = "43";

$age = array(“ali"=>"35", “taha"=>"37", “hamza"=>"43");


echo “ali is " . $age[‘ali'] . " years old.";
ARRAYS- PHP Associative Arrays
Loop Through an Associative Array

$age = array(“ali"=>"35", “taha"=>"37", “hamza"=>"43");

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


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
ARRAYS- PHP Multidimensional
It is the simplest form of a multidimensional array. It can be created using nested array. These type of arrays can be
Arrays
used to store any type of elements, but the index is always a number. By default, the index starts with zero
ARRAYS- PHP Multidimensional
A multidimensional array is an array containing one or more arrays.
Arrays

A two-dimensional array is an array of arrays (a three-dimensional array is an


array of arrays of arrays).

First, take a look at the following table:

Name math english


ali 75 90
Hamza 88 81
Taha 79 80
sana 83 77
ARRAYS- PHP Multidimensional
We can store the data from the table above in a two-dimensional array, like this:
Arrays

$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

echo $marks[0][0].": In stock: ".$marks[0][1].", sold: ".$marks[0][2].".<br>";

echo $marks[1][0].": In stock: ".$marks[1][1].", sold: ".$marks[1][2].".<br>";

echo $marks[2][0].": In stock: ".$marks[2][1].", sold: ".$marks[2][2].".<br>";

echo $marks[3][0].": In stock: ".$marks[3][1].", sold: ".$marks[3][2].".<br>";

How to print all values in loop ??

You might also like