PHP Shorthand if Statements

0
4K

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.

Zoeken
Categorieën
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 4K
Other
PHP Math
PHP has a set of math functions that allows you to perform mathematical tasks on numbers. PHP...
By PHP Tutorial 2024-05-17 07:39:09 0 6K
Other
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
By PHP Tutorial 2024-05-17 07:45:23 0 4K
Other
PHP Functions
The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and...
By PHP Tutorial 2024-05-17 07:53:06 0 5K
Other
PHP Casting
Sometimes you need to change a variable from one data type into another, and sometimes you want a...
By PHP Tutorial 2024-05-17 07:38:02 0 4K
Sociallez https://sociallez.com