PHP Shorthand if Statements

0
2KB

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.

Rechercher
Catégories
Lire la suite
Autre
PHP Loops
In the following chapters you will learn how to repeat code by using loops in PHP. PHP Loops...
Par PHP Tutorial 2024-05-17 07:48:35 0 2KB
Autre
PHP for Loop
The for loop - Loops through a block of code a specified number of times. The PHP...
Par PHP Tutorial 2024-05-17 07:49:43 0 2KB
Autre
PHP Operators
PHP Operators Operators are used to perform operations on variables and values. PHP divides the...
Par PHP Tutorial 2024-05-17 07:41:39 0 3KB
Autre
PHP Indexed Arrays
PHP Indexed Arrays In indexed arrays each item has an index number. By default, the first item...
Par PHP Tutorial 2024-05-17 08:00:38 0 2KB
Autre
PHP Casting
Sometimes you need to change a variable from one data type into another, and sometimes you want a...
Par PHP Tutorial 2024-05-17 07:38:02 0 2KB