PHP Shorthand if Statements

0
2χλμ.

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.

Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
άλλο
PHP Add Array Items
Add Array Item To add items to an existing array, you can use the bracket [] syntax....
από PHP Tutorial 2024-05-17 08:06:05 0 4χλμ.
άλλο
PHP Strings
A string is a sequence of characters, like "Hello world!". Strings Strings in PHP are...
από PHP Tutorial 2024-05-17 07:28:17 0 3χλμ.
άλλο
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
από PHP Tutorial 2024-05-17 08:06:46 0 4χλμ.
άλλο
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
από PHP Tutorial 2024-05-17 07:47:03 0 2χλμ.
άλλο
PHP Multidimensional Arrays
In the previous pages, we have described arrays that are a single list of key/value pairs....
από PHP Tutorial 2024-05-17 08:07:20 0 3χλμ.