ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
PHP Tutorial - Index
PHP Tutorial - Introduction to PHP
PHP Tutorial - Your First PHP Script
PHP Tutorial - Operators
PHP Tutorial - Conditional and Looping
PHP Tutorial - Functions
PHP Tutorial - Classes and Objects
PHP Tutorial - Working with Forms
PHP Tutorial - Advanced PHP Topics
PHP Tutorial - Advanced Class Concepts
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > PHP Tutorial >Conditionals and Looping
 

PHP Tutorial - Conditionals and Looping

 
Page 2 of 5
 

 

Example

<html>

<body>

<?php

$x=10;

if ($x>=10)

{

echo "Hello<br/>";

echo "The day is over for you.<br/>";

}

else

{

echo "Hello<br/>";

echo "You still have to work.<br/>";

}

?>

</body>

</html>

 

Output

Switch Statement

The “switch” statement is another useful conditional statement. It is used when one of the many blocks of code has to be executed. When the switch statement is used then it checks for all the conditions at once and it is also a good programming practice.

In the switch statements break is used to prevent the code from running into the next case automatically.

Syntax

switch (expression)

{

case label1:

code to be executed if the expression = label1;

break;

case label2:

code to be executed if the expression = label2;

break;

default:

code to be executed if the expression is different from both label1 and label2;

break;

}

 

In the following example there is a single expression that is evaluated once. After this the value of the expression is compared with the value of each case in the structure. If the value in any of the cases gives a match, then the block of code associated with that structure is associated. If the value does not match with any of the cases then the default case is executed.

Example:

 

<html>

<body>

 

<?php

 

$input = 10;

echo "Working for $input hours<br />";

 

switch ($input)

{

case 6:

echo "Your salary is $600";

break;

case 8:

echo "Your salary is $800";

break;

case 10:

echo "Your salary is $1000";

break;

case 12:

echo "Your salary is $1200";

break;

default:

echo "How much do you work?";

break;

}

?>

 

</body>

</html>



 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.