SlideShare a Scribd company logo
1 of 8
Download to read offline
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
1
Chapter 5
Part II
PHP
PHP if...else...elseif Statements:
Conditional statements are used to perform different actions based on
different conditions.
In PHP we have the following conditional statements:
 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
 if...elseif....else statement - executes different codes for more
than two conditions
 switch statement - selects one of many blocks of code to be
executed
1. PHP - The if Statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
2
The example below will output "Have a good day!" if the current time
(HOUR) is less than 20:
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
2. PHP - The if...else Statement
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 {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is
less than 20, and "Have a good night!" otherwise:
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
3
} else {
echo "Have a good night!";
}
?>
3. PHP - The if...elseif....else Statement
The if....elseif...else statement executes different codes for more than
two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
The example below will output "Have a good morning!" if the current
time is less than 10, and "Have a good day!" if the current time is less
than 20. Otherwise it will output "Have a good night!":
Example
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
4
echo "Have a good night!";
}
?>
4. The PHP switch Statement
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:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all
labels;
}
This is how it works: First we have a single expression n (most often a variable),
that is evaluated once. The value of the expression is then compared with the
values for each case in the structure. If there is a match, the block of code
associated with that case is executed. Use break to prevent the code from running
into the next case automatically. The default statement is used if no match is
found.
Example
<?php
$favcolor = "red";
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
5
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!";
}
?>
5.PHP while Loops
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;
}
The example below first sets a variable $x to 1 ($x = 1). Then, the while
loop will continue to run as long as $x is less than, or equal to 5 ($x <=
5). $x will increase by 1 each time the loop runs ($x++):
Example
<?php
$x = 1;
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
6
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
6. The PHP do...while Loop
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);
The example below first sets a variable $x to 1 ($x = 1). Then, the do
while loop will write some output, and then increment the variable $x
with 1. Then the condition is checked (is $x less than, or equal to 5?),
and the loop will continue to run as long as $x is less than, or equal to 5:
Example
<?php
$x = 1;
do {
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
7
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
7. The PHP for Loop
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;
}
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
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
8
}
?>
8. The PHP foreach Loop
The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}
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 demonstrates a loop that will output the values
of the given array ($colors):
Example
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>

More Related Content

What's hot

Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 

What's hot (20)

Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Php string function
Php string function Php string function
Php string function
 
php basics
php basicsphp basics
php basics
 
Php basics
Php basicsPhp basics
Php basics
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
PHP-Part1
PHP-Part1PHP-Part1
PHP-Part1
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 

Viewers also liked

CV Bernard Maselis - 2015 (EN)
CV Bernard Maselis - 2015 (EN)CV Bernard Maselis - 2015 (EN)
CV Bernard Maselis - 2015 (EN)
Bernard Maselis
 
Facebook Insights Reports
Facebook Insights ReportsFacebook Insights Reports
Facebook Insights Reports
ReportGarden
 

Viewers also liked (20)

Psa Business Model
Psa Business ModelPsa Business Model
Psa Business Model
 
Performance d'un site Internet
Performance d'un site InternetPerformance d'un site Internet
Performance d'un site Internet
 
Case Study - Performance Assessment and Testing Helps Government Department I...
Case Study - Performance Assessment and Testing Helps Government Department I...Case Study - Performance Assessment and Testing Helps Government Department I...
Case Study - Performance Assessment and Testing Helps Government Department I...
 
CV Bernard Maselis - 2015 (EN)
CV Bernard Maselis - 2015 (EN)CV Bernard Maselis - 2015 (EN)
CV Bernard Maselis - 2015 (EN)
 
Personality Profile Training
Personality Profile TrainingPersonality Profile Training
Personality Profile Training
 
Case Study - Functional Testing Helps Leading Movie Service Company Minimize ...
Case Study - Functional Testing Helps Leading Movie Service Company Minimize ...Case Study - Functional Testing Helps Leading Movie Service Company Minimize ...
Case Study - Functional Testing Helps Leading Movie Service Company Minimize ...
 
21 avril 2015 : la compatibilité mobile, critère SEO officiel chez Google
21 avril 2015 : la compatibilité mobile, critère SEO officiel chez Google21 avril 2015 : la compatibilité mobile, critère SEO officiel chez Google
21 avril 2015 : la compatibilité mobile, critère SEO officiel chez Google
 
المادة العلمية محاضرة 3 تبويب المراجع العلمية باستخدام برنامج endnote
المادة العلمية محاضرة 3 تبويب المراجع العلمية باستخدام برنامج endnoteالمادة العلمية محاضرة 3 تبويب المراجع العلمية باستخدام برنامج endnote
المادة العلمية محاضرة 3 تبويب المراجع العلمية باستخدام برنامج endnote
 
20160907 myposeo weloveseo
20160907 myposeo weloveseo20160907 myposeo weloveseo
20160907 myposeo weloveseo
 
توظيف وسائل الإعلام الاجتماعي في العمل الخيري - تجربة عالمية
توظيف وسائل الإعلام الاجتماعي في العمل الخيري - تجربة عالميةتوظيف وسائل الإعلام الاجتماعي في العمل الخيري - تجربة عالمية
توظيف وسائل الإعلام الاجتماعي في العمل الخيري - تجربة عالمية
 
Le Content Marketing stratégique, la méthodologie SCOM (& scompler / Scribble...
Le Content Marketing stratégique, la méthodologie SCOM (& scompler / Scribble...Le Content Marketing stratégique, la méthodologie SCOM (& scompler / Scribble...
Le Content Marketing stratégique, la méthodologie SCOM (& scompler / Scribble...
 
Symantec Data Insight 3.0
Symantec Data Insight 3.0Symantec Data Insight 3.0
Symantec Data Insight 3.0
 
Interview Métier : Web Designer / Intégrateur
Interview Métier : Web Designer / Intégrateur Interview Métier : Web Designer / Intégrateur
Interview Métier : Web Designer / Intégrateur
 
Renault nissan
Renault nissanRenault nissan
Renault nissan
 
Facebook Insights Reports
Facebook Insights ReportsFacebook Insights Reports
Facebook Insights Reports
 
MÉMOIRE DE FIN D'ÉTUDE - LA COMMUNICATION DES CONCEPT-STORES
MÉMOIRE DE FIN D'ÉTUDE - LA COMMUNICATION DES CONCEPT-STORES MÉMOIRE DE FIN D'ÉTUDE - LA COMMUNICATION DES CONCEPT-STORES
MÉMOIRE DE FIN D'ÉTUDE - LA COMMUNICATION DES CONCEPT-STORES
 
KPI & acquisition de trafic - 2016
KPI & acquisition de trafic - 2016KPI & acquisition de trafic - 2016
KPI & acquisition de trafic - 2016
 
Etes vous prêts pour la révolution omnicanale imposée par le consommateur ?
Etes vous prêts pour la révolution omnicanale imposée par le consommateur ?Etes vous prêts pour la révolution omnicanale imposée par le consommateur ?
Etes vous prêts pour la révolution omnicanale imposée par le consommateur ?
 
Les origines du brand content
Les origines du brand contentLes origines du brand content
Les origines du brand content
 
Carte de voeux QualiQuanti 2016
Carte de voeux QualiQuanti 2016 Carte de voeux QualiQuanti 2016
Carte de voeux QualiQuanti 2016
 

Similar to PHP-Part2

Similar to PHP-Part2 (20)

Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Intro to php
Intro to phpIntro to php
Intro to php
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 
PHP
PHPPHP
PHP
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
php
phpphp
php
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides (2) (1)
Php i-slides (2) (1)Php i-slides (2) (1)
Php i-slides (2) (1)
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
php41.ppt
php41.pptphp41.ppt
php41.ppt
 
PHP InterLevel.ppt
PHP InterLevel.pptPHP InterLevel.ppt
PHP InterLevel.ppt
 
php-I-slides.ppt
php-I-slides.pptphp-I-slides.ppt
php-I-slides.ppt
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 

More from Ahmed Saihood (8)

Sessions &Cookies
Sessions &CookiesSessions &Cookies
Sessions &Cookies
 
HTTP & HTTPs
HTTP & HTTPsHTTP & HTTPs
HTTP & HTTPs
 
CSS
CSSCSS
CSS
 
XHTML
XHTMLXHTML
XHTML
 
HTML-Forms
HTML-FormsHTML-Forms
HTML-Forms
 
HTML-Part2
HTML-Part2HTML-Part2
HTML-Part2
 
HTML-Part1
HTML-Part1HTML-Part1
HTML-Part1
 
internet basics
internet basics internet basics
internet basics
 

Recently uploaded

Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
nirzagarg
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
JOHNBEBONYAP1
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
nirzagarg
 

Recently uploaded (20)

Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Thalassery Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call G...
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 

PHP-Part2

  • 1. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 1 Chapter 5 Part II PHP PHP if...else...elseif Statements: Conditional statements are used to perform different actions based on different conditions. In PHP we have the following conditional statements:  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  if...elseif....else statement - executes different codes for more than two conditions  switch statement - selects one of many blocks of code to be executed 1. PHP - The if Statement The if statement executes some code if one condition is true. Syntax if (condition) { code to be executed if condition is true; }
  • 2. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 2 The example below will output "Have a good day!" if the current time (HOUR) is less than 20: Example <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } ?> 2. PHP - The if...else Statement 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 { code to be executed if condition is false; } The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise: Example <?php $t = date("H"); if ($t < "20") { echo "Have a good day!";
  • 3. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 3 } else { echo "Have a good night!"; } ?> 3. PHP - The if...elseif....else Statement The if....elseif...else statement executes different codes for more than two conditions. Syntax if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; } The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!": Example <?php $t = date("H"); if ($t < "10") { echo "Have a good morning!"; } elseif ($t < "20") { echo "Have a good day!"; } else {
  • 4. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 4 echo "Have a good night!"; } ?> 4. The PHP switch Statement 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: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; } This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found. Example <?php $favcolor = "red";
  • 5. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 5 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!"; } ?> 5.PHP while Loops 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; } The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++): Example <?php $x = 1;
  • 6. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 6 while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> 6. The PHP do...while Loop 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); The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5: Example <?php $x = 1; do {
  • 7. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 7 echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> 7. The PHP for Loop 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; } 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 <?php for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>";
  • 8. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 8 } ?> 8. The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax foreach ($array as $value) { code to be executed; } 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 demonstrates a loop that will output the values of the given array ($colors): Example <?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>