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.

Suche
Kategorien
Mehr lesen
Andere
PHP - Modify Strings
PHP has a set of built-in functions that you can use to modify strings. Upper Case...
Von PHP Tutorial 2024-05-17 07:29:19 0 9KB
Andere
PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is...
Von PHP Tutorial 2024-05-17 07:48:58 0 5KB
Andere
PHP for Loop
The for loop - Loops through a block of code a specified number of times. The PHP...
Von PHP Tutorial 2024-05-17 07:49:43 0 4KB
Andere
PHP Global Variables - Superglobals
Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available...
Von PHP Tutorial 2024-05-17 08:08:10 0 11KB
Andere
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
Von PHP Tutorial 2024-05-17 08:06:46 0 8KB
Sociallez https://sociallez.com