PHP Shorthand if Statements
Posted 2024-05-17 07:47:03
0
3K
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
Categories
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Games
- Gardening
- Health
- Home
- Literature
- Music
- Networking
- Other
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness
Read More
PHP Continue
The continue statement can be used to jump out of the current iteration of a loop, and...
PHP - Escape Characters
Escape Character
To insert characters that are illegal in a string, use an escape character.
An...
PHP Arrays
An array stores multiple values in one single variable:
ExampleGet your own PHP Server
$cars...
PHP Loops
In the following chapters you will learn how to repeat code by using loops in PHP.
PHP Loops...
PHP Installation
What Do I Need?
To start using PHP, you can:
Find a web host with PHP and MySQL support...