PHP Shorthand if Statements

This is used to simplify simple if...else conditions. It condenses the if...else logic into a single line of code, making your code more compact and sometimes easier to read.





Example of a Shorthand if
<!DOCTYPE html>
<html>
<body>
<?php
$a = 19;
if ($a > 18) $b = "You are eligible to vote.";
echo $b
?>
</body>
</html>

Try it yourself

Example of a Shorthand If...Else
<!DOCTYPE html>
<html>
<body>
<?php
$age = 18;
$message = ($age >= 18) ? "You are eligible to vote." : "You are not eligible to vote.";
echo $message;
?>
</body>
</html>

Try it yourself

Note

This technique is known as Ternary Operators, or Conditional Expressions.


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.