PHP Shorthand if Statements

0
4KB

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 foreach Loop
The foreach loop - Loops through a block of code for each element in an array or each...
Par PHP Tutorial 2024-05-17 07:50:19 0 5KB
Autre
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
Par PHP Tutorial 2024-05-17 07:47:03 0 4KB
Autre
PHP if Operators
Comparison Operators If statements usually contain conditions that compare two values....
Par PHP Tutorial 2024-05-17 07:45:46 0 5KB
Autre
PHP - $_SERVER
$_SERVER $_SERVER is a PHP super global variable which holds information about headers,...
Par PHP Tutorial 2024-05-17 08:08:50 0 8KB
Autre
PHP switch Statement
The switch statement is used to perform different actions based on different...
Par PHP Tutorial 2024-05-17 07:47:56 0 4KB
Sociallez https://sociallez.com