Acceder

Short statements in PHP / Tiny conditions if - else

How to replace long statements or conditions in PHP?


In software development, the creation of sentences is something daily. Next you will discover some methods that will allow you to do everything in one line, benefiting you in time and in debugging your code:

Basic declaration of TRUE /FALSE

$ 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': '');


Safety condition

echo '<base href =" http'. ($ PAGE_IS_SECURE? 's': ''). ': //mydomain.com "/>';


Subconditions

echo 'Your score is:'. ($ score> 10? ($ age> 10? 'Average': 'Exceptional'): ($ age> 10? 'Horrible': 'Average'));


A condition for redirection

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