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.

Căutare
Categorii
Citeste mai mult
Alte
PHP for Loop
The for loop - Loops through a block of code a specified number of times. The PHP...
By PHP Tutorial 2024-05-17 07:49:43 0 4K
Alte
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
By PHP Tutorial 2024-05-17 07:47:03 0 4K
Alte
PHP $GLOBALS
$GLOBALS is an array that contains all global variables. Global Variables Global...
By PHP Tutorial 2024-05-17 08:08:27 0 7K
Alte
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 5K
Alte
PHP Nested if Statement
Nested If You can have if statements inside if statements, this is...
By PHP Tutorial 2024-05-17 07:47:26 0 4K
Sociallez https://sociallez.com