Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.
Menu
PHP TUTORIALS


PHP If Else

If Else statements allow you to check if a certain condition is true, and do something, and if it's not true, do something else instead.

You can use the if statement on it's own: <?PHP
$condition=true;

if($condition){
echo 'Condition is True';
}
?>

Or you can use the if statement with an else statement: <?PHP
$condition=true;

if($condition){
echo 'Condition is True';
}
else{
echo 'Condition is False';
}
?>

You can even combine if & else statements together: <?PHP
$condition='c';

if($condition=='a'){
echo 'Condition is A';
}
else if($condition=='b'){
echo 'Condition is B';
}
else{
echo 'Condition is C';
}
?>


There's not a lot else to discuss regarding if else... it's that simply, and you'll use them alot!

But wait... there is a shorthand version of if else which is great for making quick yes/no decisions inline: <?PHP
$condition=true;
echo 'Condition is '.(($condition)?'True':'False');
?>
You simply place the thing you want to check in brackets ($condition==true), followed by a question mark (?). This basically asks the question 'is the condition in the brakets true?', and if so executes the code in front of the colon (:), and if not it executes the code after the colon (:). I tend to wrap the whole question in another set of brackets just to make sure it's separated from any other code, but you don't have to!


Title