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.

Pesquisar
Categorias
Leia mais
Outro
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
Por PHP Tutorial 2024-05-17 07:45:23 0 2KB
Outro
PHP Delete Array Items
Remove Array Item To remove an existing item from an array, you can use...
Por PHP Tutorial 2024-05-17 08:06:22 0 3KB
Outro
PHP Arrays
An array stores multiple values in one single variable: ExampleGet your own PHP Server $cars...
Por PHP Tutorial 2024-05-17 07:53:31 0 2KB
Outro
PHP Introduction
PHP code is executed on the server. What You Should Already Know Before you continue you...
Por PHP Tutorial 2024-05-17 07:08:40 0 2KB
Outro
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
Por PHP Tutorial 2024-05-17 08:06:46 0 4KB