HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP Shorthand if Statements


• Short Hand If

PHP offers a shorthand syntax for if-else statements, also known as the ternary operator, providing a more concise way to write conditional expressions.


Example


<!DOCTYPE html>
<html>
<body>

<?php
$a = 5;

if ($a < 10) $b = "Hello";

echo $b
?>

</body>
</html>

Output

Hello

• Short Hand If...Else

shorthand "If...Else" statement, often referred to as the ternary operator, offers a concise way to write conditional expressions in a single line.

Example


<!DOCTYPE html>
<html>
<body>

<?php
$a = 13;

$b = $a < 10 ? "Hello" : "Good Bye";

echo $b;
?>

</body>
</html>

Output

Good Bye