The
(condition) ? /* value to return if condition is true */
: /* value to return if condition is false */ ;
syntax is not a “shorthand if” operator (the ?
is called the conditional operator) because you cannot execute code in the same manner as if you did:
if (condition) {
/* condition is true, do something like echo */
}
else {
/* condition is false, do something else */
}
In your example, you are executing the echo
statement when the $address
is not empty. You can’t do this the same way with the conditional operator. What you can do however, is echo
the result of the conditional operator:
echo empty($address) ? "is empty!" : $address;
Hello Friends, I’m Mohit Sharma founder of tutrialwala.com, Full Stack Web Developer. Expertise in WordPress, PHP, Laravel, Angular, and Frontend Development. In this publication, I share everything I know about PHP frameworks and Javascript frameworks, packages, and tools.
Have a project in mind? Get in touch