PHP – Conditionals

1 0
Read Time:2 Minute, 27 Second

PHP – Conditionals

php conditionals Construct

Here are the most common conditional types:

Conditional statements allow your program to make choices

A computer can typically make two choices, true or false. You can “chain” them together to allow more choices

PHP v.5.3 adds support for jump labels which provides a limited goto functionality.

  • Simple if Statement
  • if .. else Statement
  • elseif Statement
  • switch Statement

PHP- The Simple if Statement

Statement – executes some code if one condition is true

Example:

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
}
//Output -Have a good day! if the current time (HOUR) is less than 20
?>

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.

<?php
$t = date("H");
 
if ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
//Output -Have a good day! if the current time is less than 20, and -Have a good night! otherwise
?>

PHP- The elseif Statement

The elseif statement executes different codes for more than two conditions

<?php
$t = date("H");

if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
//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!"
?>

PHP- The switch Statement

Use the switch statement to select one of many blocks of code to be executed.

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.

<?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!";
  break;
}
//Output "Your favorite color is red!"
?>

PHP- Shorthand Condition

Here is how to assign a variable based on a condition.

  • if ($a > 10 ) $a=10;
  • Useful to make sure $a doesn’t become greater than 10

There is a shorthand operation called the ternary operator which makes this faster to type and read:

  • (<condition>) ? <value of true> : <value of false>;

The above if conditional could be re-written using the ternary operator as..

<?php
$a=5;
$a = ($a > 10) ? 10 : $a;
echo $a;
//Output:5
?>
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

16 thoughts on “PHP – Conditionals

  1. Cool, I’ve been looking for this one for a long time
    _________________

  2. Cool, I’ve been looking for this one for a long time
    _________________

  3. I like this website because I can always find something new for myself, moreover, I like to read other people’s comments.
    I like this website too:

  4. Good point, makes a sence especially when one is experienced in the topic. So keep righting and share your thoughts and experiences.

  5. There is clearly a lot to realize about this. I consider you made various good points in features also.

  6. Thanks for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such fantastic info being shared freely out there.

  7. I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…

  8. We are a group of volunteers and starting a new scheme in our community.
    Your site provided us with valuable info to work on. You’ve done
    an impressive job and our entire community will be thankful to you.

  9. This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your website in my social networks!

Comments are closed.