Тернарный оператор в PHP

Contents
Теория
Пример

Теория

Сперва пишется выражение, затем ?

После ? пишутся два значения и : между ними.

Если выражение верное (True) то выбирается левое значение, в противном случае правое.

(Выражение) ? Если_правда : Если_ложь

Пример

<?php echo (1 == 1) ? 'Правда' : 'Ложь'; // Правда echo (1 == 2) ? 'Правда' : 'Ложь'; // Ложь $r = (1 == 1) ? 'Yes' : 'No'; // $r is set to 'Yes' echo $r; // Yes

Related Articles
Development with PHP
Arrays in PHP
Date and Time in PHP - basics
How to display time of multiple timezones in PHP
How to add variable to url in PHP
json_decode
How to get screen size with PHP
Call function from another file
Premature end of chunk coded message body: closing chunk expected
Generate unique random numbers with PHP
Check your HTTP_USER_AGENT
Compare two dates
PHP 8 Classes
Comments in PHP
Cookies
PHP sessions
Authentication and Authorization in PHP 8

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedback and Questions in Telegram

@aofeedchat