PHP Shorthand if Statements

0
2K

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.

Căutare
Categorii
Citeste mai mult
Alte
PHP if...else Statements
PHP - The if...else Statement The if...else statement executes some code if a...
By PHP Tutorial 2024-05-17 07:46:12 0 2K
Alte
PHP Array Functions
PHP Array Functions PHP has a set of built-in functions that you can use on arrays....
By PHP Tutorial 2024-05-17 08:07:42 1 4K
Alte
PHP Delete Array Items
Remove Array Item To remove an existing item from an array, you can use...
By PHP Tutorial 2024-05-17 08:06:22 0 3K
Alte
PHP - Concatenate Strings
String Concatenation To concatenate, or combine, two strings you can use...
By PHP Tutorial 2024-05-17 07:30:24 0 3K
Alte
PHP if Operators
Comparison Operators If statements usually contain conditions that compare two values....
By PHP Tutorial 2024-05-17 07:45:46 0 2K