PHP Shorthand if Statements

0
2K

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.

Site içinde arama yapın
Kategoriler
Read More
Other
PHP Magic Constants
PHP Predefined Constants PHP has nine predefined constants that change value depending on where...
By PHP Tutorial 2024-05-17 07:40:16 0 2K
Other
PHP Break
The break statement can be used to jump out of different kind of loops. Break in For...
By PHP Tutorial 2024-05-17 07:50:37 0 2K
Other
PHP echo and print Statements
With PHP, there are two basic ways to get output: echo and print. In this...
By PHP Tutorial 2024-05-17 07:20:06 0 3K
Other
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings. PHP Numbers...
By PHP Tutorial 2024-05-17 07:37:05 0 3K
Other
PHP Global Variables - Superglobals
Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available...
By PHP Tutorial 2024-05-17 08:08:10 0 4K