PHP – Conditionals

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
?>
Ciao, volevo sapere il tuo prezzo.
15 US dollar/Hour
Hola, quería saber tu precio..
Cool, I’ve been looking for this one for a long time
_________________
Cool, I’ve been looking for this one for a long time
_________________
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:
Good point, makes a sence especially when one is experienced in the topic. So keep righting and share your thoughts and experiences.
There is clearly a lot to realize about this. I consider you made various good points in features also.
Kudos. I like it!
Hello. And Bye.
Cool + for the post
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.