php if简写
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.
在查看我的Google Analytics(分析)统计信息时,我看到很多访问者在搜索PHP简写if / else(三进制)信息。 我遍历了代码库,并选择了一些三元运算符用法的示例。
基本的对/错声明 (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" />';
嵌套PHP速记 (Nested PHP Shorthand)
echo 'Your score is: '.($score > 10 ? ($age > 10 ? 'Average' : 'Exceptional') : ($age > 10 ? 'Horrible' : 'Average') );
Year年检查 (Leap Year Check)
$is_leap_year = ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0)));
有条件PHP重定向 (Conditional PHP Redirect)
header('Location: '.($valid_login ? '/members/index.php' : 'login.php?errors=1')); exit();
php if简写