PHP Shorthand if Statements

0
4KB

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.

Suche
Kategorien
Mehr lesen
Andere
PHP Data Types
PHP Data Types Variables can store data of different types, and different data types can do...
Von PHP Tutorial 2024-05-17 07:27:24 0 5KB
Andere
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
Von PHP Tutorial 2024-05-17 07:45:23 0 4KB
Andere
PHP Functions
The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and...
Von PHP Tutorial 2024-05-17 07:53:06 0 5KB
Andere
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 4KB
Andere
PHP Comments
Comments in PHP A comment in PHP code is a line that is not executed as a part of the program....
Von PHP Tutorial 2024-05-17 07:14:49 0 6KB
Sociallez https://sociallez.com