PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Shorthand Conditionals

PHP has two special shorthand conditional operators.

PHP Ternary Operator

The ternary operator is a short way of performing an if conditional.

Ternary Syntax

(condition) ? (on true) : (on false)
  • If the condition is true, the result of on true expression is returned.
  • If the condition is false, the result of on false expression is returned.
  • Since PHP 5.3, you can omit the on true expression. (condition) ?: (on false) returns the result of condition if the condition is true. Otherwise, the result of on false expression is returned.

PHP Ternary Operator Example


<?php
$username = 'Hyvor';
echo 'Hello ' . ($username ?: 'Guest') . '<br>'; // hello hyvor, because $username returns true

$username = null;
echo 'Hello ' . ($username ?: 'Guest') . '<br>'; // hello guest, because $username returns null

// another example
$var = 5;
$isGreaterThan2 = $var > 2 ? true : false;
var_dump($isGreaterThan2);

Run Example ››
It is not recommended to use nested expressions with the ternary operator. PHP's behavior when using more than one ternary operator within a single statement is non-obvious. So, use ternary operator only for simple conditions.

The ternary operator is commonly used with isset() function in PHP.

PHP Ternary Operator with Isset() Example


<?php
$username = (isset($fisrtname, $lastname)) ? $fisrtname . ' ' . $lastname : "Guest";
echo $username . '<br>';

$fisrtname = 'Hyvor';
$lastname = 'Developer';
$username = (isset($fisrtname, $lastname)) ? $fisrtname . ' ' . $lastname : "Guest";
echo $username;

Run Example ››

Tip: The isset() function returns true if the variables (which are sent to the function in parameters) are set and not null.

Wrapping the expressions in the ternary operator with parentheses is optional.

PHP Null Coalescing Operator

PHP's null coalescing operator is a useful new feature which was introduced in PHP 7.

The null coalescing operator can be used to assign default values to a variable.

In the example below, the null coalescing expression returns 2 because $a is not defined.

PHP Null Coalescing Operator Example


<?php
$x = $a ?? 2;
var_dump($x);

// $a ?? 2 is equal to the following code
if (isset($a)) {
	return $a;
} else {
	return 2;
}

Run Example ››

Important: Null coalescing operator allows for nesting.

PHP Null Coalescing Operator Nesting Example


<?php
$a = null;
$b = null;
$c = 5;
$d = 3;

var_dump($a ?? $b ?? $c ?? $d); // outputs 5

Run Example ››
Need help with PHP? Post your question on our forum's PHP Group.
Facebook Twitter