What control structures can one use instead of multiple nested IF statements.
eg:
function change_password($email, $password, $new_password, $confirm_new_password) { if($email && $password && $new_password && $confirm_new_password) { if($new_password == $confirm_new_password) { if(login($email, $password)) { if(set_password($email, $new_password)) { return TRUE; } } } } }This function is used like this:
if(!change_password($email, $password, $new_password, $confirm_new_password) { echo 'The form was not filled in correctly!'; exit; }I call all my functions like this, and I'm wondering if there's something wrong with my coding style. I'm having my doubts because if I follow this design then that means every single function I write will just be with nested with IF's, checking if there are errors at every stage. Is this what other people do?
I don't see many other scripts written like this, with the nested IF's making a triangle shape and only having the desired result in the very middle. If the middle isn't reached, then something screwed up.
Is this a good function structure?
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
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
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;
}
Example
Output "Have a good day!" if the current time (HOUR) is less than 20:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
Try it Yourself »
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;
}
Example
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Try it Yourself »
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 first condition is false and this condition is true;
}
else {
code to be executed if all conditions are false;
}
Example
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
$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!";
}
?>
Try it Yourself »
PHP - The switch Statement
The switch statement will be explained in the next chapter.
PHP Exercises
(PHP 4, PHP 5, PHP 7, PHP 8)
The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:
As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to true, PHP will execute statement, and if it evaluates to false - it'll ignore it. More information about what values evaluate to false can be found in the 'Converting to boolean' section.
The following example would display a is bigger than b if $a is bigger than $b:
<?php
if ($a > $b)
echo "a is bigger than b";
?>
Often you'd want to have more than one statement to be executed conditionally. Of course, there's no need to wrap each statement with an if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:
<?php
if ($a > $b) {
echo "a is bigger than b";
$b = $a;
}
?>
If statements can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.
robk ¶
9 years ago
easy way to execute conditional html / javascript / css / other language code with php if else:
<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>
techguy14 at gmail dot com ¶
11 years ago
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:
<?php
if( $a == 1 || $a == 2 ) {
if( $b == 3 || $b == 4 ) {
if( $c == 5 || $ d == 6 ) {
//Do something here.
}
}
}
?>
You could just simply do this:
<?php
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) {
//do that something here.
}
?>
Hope this helps!
Christian L. ¶
11 years ago
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:
<?php
$v = 1;$r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes'
$r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No'echo (1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed
// and since PHP 5.3
$v = 'My Value';$r = ($v) ?: 'No Value'; // $r is set to 'My Value' because $v is evaluated to TRUE$v = '';
echo ($v) ?: 'No Value'; // 'No Value' will be printed because $v is evaluated to FALSE
?>
Parentheses can be left out in all examples above.
grawity at gmail dot com ¶
14 years ago
re: #80305
Again useful for newbies:
if you need to compare a variable with a value, instead of doing
<?php
if ($foo == 3) bar();
?>
do
<?php
if (3 == $foo) bar();
?>
this way, if you forget a =, it will become
<?php
if (3 = $foo) bar();
?>
and PHP will report an error.
cole dot trumbo at nospamthnx dot gmail dot com ¶
5 years ago
Any variables defined inside the if block will be available outside the block. Remember that the if doesn't have its own scope.
<?php
$bool = true;
if ($bool) {
$hi = 'Hello to all people!';
}
echo $hi;
?>
It will print 'Hello to all people!'
On the other hand, this will have no output:
<?php
if (false) {
$hi = 'Hello to all people!';
}
echo $hi;
?>
Donny Nyamweya ¶
11 years ago
In addition to the traditional syntax for if (condition) action;
I am fond of the ternary operator that does the same thing, but with fewer words and code to type:
(condition ? action_if_true: action_if_false;)
example
(x > y? 'Passed the test' : 'Failed the test')