PHP Shorthand if Statements

0
3K

Short Hand If

To write shorter code, you can write if statements on one line.

ExampleGet your own PHP Server

One-line if statement:

$a = 5;

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

echo $b
Try it Yourself »

Short Hand If...Else

if...else statements can also be written in one line, but the syntax is a bit different.

Example

One-line if...else statement:

$a = 13;

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

echo $b;
Try it Yourself »

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

Search
Categories
Read More
Other
PHP Continue
The continue statement can be used to jump out of the current iteration of a loop, and...
By PHP Tutorial 2024-05-17 07:52:32 0 3K
Other
PHP - Escape Characters
Escape Character To insert characters that are illegal in a string, use an escape character. An...
By PHP Tutorial 2024-05-17 07:32:36 0 7K
Other
PHP Arrays
An array stores multiple values in one single variable: ExampleGet your own PHP Server $cars...
By PHP Tutorial 2024-05-17 07:53:31 0 4K
Other
PHP Loops
In the following chapters you will learn how to repeat code by using loops in PHP. PHP Loops...
By PHP Tutorial 2024-05-17 07:48:35 0 3K
Other
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
By PHP Tutorial 2024-05-17 07:09:40 0 3K