PHP Shorthand if Statements

0
4χλμ.

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 Functions
The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and...
από PHP Tutorial 2024-05-17 07:53:06 0 4χλμ.
άλλο
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to the browser....
από PHP Tutorial 2024-05-17 07:10:53 0 8χλμ.
άλλο
PHP Global Variables - Superglobals
Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available...
από PHP Tutorial 2024-05-17 08:08:10 0 10χλμ.
άλλο
PHP switch Statement
The switch statement is used to perform different actions based on different...
από PHP Tutorial 2024-05-17 07:47:56 0 4χλμ.
άλλο
PHP - Slicing Strings
Slicing You can return a range of characters by using the substr() function. Specify...
από PHP Tutorial 2024-05-17 07:31:23 1 8χλμ.
Sociallez https://sociallez.com