0% found this document useful (0 votes)
179 views2 pages

PHP Shorthand If - Else Examples PDF

The document provides examples of PHP shorthand if/else statements (ternary operators). It includes basic true/false declarations, conditional welcome messages, conditional item counts, conditional error reporting levels, conditional basepaths, nested conditions, leap year checks, and conditional redirects.

Uploaded by

Kambing Tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views2 pages

PHP Shorthand If - Else Examples PDF

The document provides examples of PHP shorthand if/else statements (ternary operators). It includes basic true/false declarations, conditional welcome messages, conditional item counts, conditional error reporting levels, conditional basepaths, nested conditions, leap year checks, and conditional redirects.

Uploaded by

Kambing Tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1/17/2017 PHP Shorthand If / Else Examples

PHP Shorthand If / Else Examples

 Design Conf, San Francisco, CA • March 19-22 • Save 20% PC20DWALSH

By David Walsh on December 18, 2007    41     

In looking at my Google Analytics statistics, I see a lot of visitors searching for PHP shorthand
if/else (ternary) information. I've gone through my code library and picked out some examples
of ternary operator usage.

Basic True / False Declaration


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

Conditional Welcome Message


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

Conditional Items Message


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

Conditional Error Reporting Level


error_reporting($WEBSITE_IS_LIVE ? 0 : E_STRICT);

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

Nested PHP Shorthand


https://davidwalsh.name/php­ternary­examples 1/14
1/17/2017 PHP Shorthand If / Else Examples

Nested PHP Shorthand


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

Leap Year Check


$is_leap_year = ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0)));

Conditional PHP Redirect


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

Recent Features 
An Interview with Eric Meyer
Your early CSS books were instrumental in pushing my love for front end technologies. What
was it about CSS that you fell in love with and drove you to write about it? At first blush, it was
the simplicity of it as compared to the table-and-spacer...

Serving Fonts from CDN


For maximum performance, we all know we must put our assets on CDN (another domain).
 Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or
any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by
spec) though...

Incredible Demos 
JavaScript Speech Recognition
Speech recognition software is becoming more and more important; it started (for me) with
Siri on iOS, then Amazon's Echo, then my new Apple TV, and so on.  Speech recognition is
https://davidwalsh.name/php­ternary­examples 2/14

You might also like