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.

Search
Nach Verein filtern
Read More
Other
PHP Introduction
PHP code is executed on the server. What You Should Already Know Before you continue you...
Von PHP Tutorial 2024-05-17 07:08:40 0 2KB
Other
PHP foreach Loop
The foreach loop - Loops through a block of code for each element in an array or each...
Von PHP Tutorial 2024-05-17 07:50:19 0 2KB
Other
PHP Continue
The continue statement can be used to jump out of the current iteration of a loop, and...
Von PHP Tutorial 2024-05-17 07:52:32 0 2KB
Other
PHP Operators
PHP Operators Operators are used to perform operations on variables and values. PHP divides the...
Von PHP Tutorial 2024-05-17 07:41:39 0 3KB
Other
PHP Multiline Comments
Multi-line Comments Multi-line comments start with /* and end with */. Any text...
Von PHP Tutorial 2024-05-17 07:15:48 0 2KB