MOX
Products
Learn about our additional services
Resources & Elements
Return

MOXNicolás Aravena
22-09-2021

Short statements in PHP / Tiny conditions if - else

In software development, creating statements is a daily occurrence. Below you'll discover some methods that will allow you to do everything in one line, saving you time and debugging your code:

Basic TRUE / FALSE statement


$is_admin = ($user['permissions'] == 'admin') ? true : false;


Welcome condition


echo 'Welcome '.($user['is_logged_in'] ? $user['name'] : 'Guest').'!';


Item condition


echo 'The shopping cart contains '.$num_items.' item'.($num_items != 1 ? 's' : '');


Subconditions


echo 'The average is: '.($score > 10 ? ($age > 10 ? 'a' : 'b') : ($age > 10 ? 'c' : 'd') );


A condition for redirection


header('Location: '.($valid_login ? '/members/index.php' : 'login.php?errors=1')); exit();




Other articles that might interest you

Servicios

Fast, secure web hosting designed to grow with you.

Your sites load faster, are always available, and have expert support 24/7. SSL certificates, automatic backups, and an intuitive dashboard are included. Ideal for shops, blogs, or businesses that can't stop.

Get to know Hosting

Browse without limits with a fast, private VPN.

Protect your identity with military-grade encryption, bypass geoblocks, and access all the world's content. No logs, optional dedicated IP, and stable connections across all your devices.

More about Chilean VPN

Total power and control with dedicated VPS servers.

Get root access, high performance with NVMe drives, and complete isolation. Easily scale your resources to host applications, systems, or web projects with constant traffic. Advanced security and dedicated support whenever you need it.

Get to know Cloud VPS