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