Shortest if/else using ternary operators

I have this piece of code: <?=!empty($options["placeholder"]) ? $options["placeholder"]:'search...'?> I was under the impression I could do like: <?=!empty($options["placeholder"]) ?:'search...'?> But when $options["placeholder"] is not empty then it returns 1, as it is a ternary operator. Do I have to always issue the variable 2 times? If you can be sure that $optio

最短的if / else使用三元运算符

我有这段代码: <?=!empty($options["placeholder"]) ? $options["placeholder"]:'search...'?> 我的印象是我可以做的: <?=!empty($options["placeholder"]) ?:'search...'?> 但是当$options["placeholder"]不为空时,它返回1,因为它是一个三元运算符。 我必须总是发出2次变量吗? 如果你可以确定$options['placeholder']将被设置 - 如果没有,你可以用@作为前缀来抑制警告 - 你可以删

Short version of $x ? $x : $y expression

My intention is to find a short version of: $x ? $x : $y where $x gets evaluated only once. From the PHP manual entry on Comparison Operators : Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. So, you'll need to be using PHP 5.3 or greater to get this "shor

短版$ x? $ x:$ y表达式

我的意图是找到一个简短的版本: $x ? $x : $y 其中$ x只被评估一次。 从比较运算符上的PHP手册条目: 从PHP 5.3开始,可以忽略三元运算符的中间部分。 表达式expr1 ?: expr3 expr1如果expr1计算结果为TRUE,则expr1 ?: expr3返回expr1 ,否则返回expr3 。 因此,您需要使用PHP 5.3或更高版本才能获得此“快捷方式”功能。 虽然,为什么你想要一个已经是捷径的运营商的捷径是另一个问题...... 是的, 它是有效的 。

Ternary statement without middle expression

How to replace this : if( $this->getInfo() ){ $value = $this->getInfo(); }else{ $value = $this->getAnotherInfo(); } This would be nicer solution : $value = $this->getInfo() ? $this->getInfo() : $this->getAnotherInfo(); But we repeat $this->getInfo() . 这是乐趣: $value = $this->getInfo() ? : $this->getAnotherInfo(); 如果它困扰你不得不重复表达式,你可以编写一

没有中间表达的三元陈述

如何取代这一点: if( $this->getInfo() ){ $value = $this->getInfo(); }else{ $value = $this->getAnotherInfo(); } 这将是更好的解决方案: $value = $this->getInfo() ? $this->getInfo() : $this->getAnotherInfo(); 但是我们重复$this->getInfo() 。 这是乐趣: $value = $this->getInfo() ? : $this->getAnotherInfo(); 如果它困扰你不得不重复表达式,你可以编写一个返回第一个真

Is there a shorter syntax for the ternary operator in php?

How can I create a shorter expression of: $variable = @$array["property"] ? $array["property"] : DEFAULT_VALUE_CONSTANT; To something like this: $variable = @$array["property"] || DEFAULT_VALUE_CONSTANT; Now I get true / false Yes it's possible in PHP7 with Null coalescing operator ( ?? ) $variable = $array["property"] ?? DEFAULT_VALUE_CONSTANT; If you are using PHP version < 7 one

在php中有三字运算符的较短语法吗?

我如何创建一个更短的表达式: $variable = @$array["property"] ? $array["property"] : DEFAULT_VALUE_CONSTANT; 像这样的东西: $variable = @$array["property"] || DEFAULT_VALUE_CONSTANT; 现在我得到true / false 是的,它可能在PHP7中与空合并运算符( ?? ) $variable = $array["property"] ?? DEFAULT_VALUE_CONSTANT; 如果您使用的是PHP版本<7,则使用elvis操作符 $variable = $array["property"] ?: DEFA

what does ? sign in this statement?

Possible Duplicates: quick php syntax question Reference - What does this symbol mean in PHP? $row_color = ($row_count % 2) ? $color1 : $color2; This is called Ternary operator. Basically it is checking if row_count is odd number then assign row_color to color1 or else color2 it is extended IF syntax it equals to if ($row_count % 2) $row_color = $color1; else $row_color = $colo

什么? 签署此声明?

可能重复: 快速PHP语法问题 参考 - 这个符号在PHP中的含义是什么? $ row_color =($ row_count%2)? $ color1:$ color2; 这被称为三元运算符。 基本上,它是检查是否row_count是奇数然后分配row_color到color1要不然color2 它是IF语法的扩展 它等于 if ($row_count % 2) $row_color = $color1; else $row_color = $color2; 这是三元运营商

Assign if variable is set

In PHP I find myself writing code like this frequently: $a = isset($the->very->long->variable[$index]) ? $the->very->long->variable[$index] : null; Is there a simpler way to do this? Preferably one that doesn't require me to write $the->very->long->variable[$index] twice. Sadly no, because the RFC has been declined. And because isset is

如果设置了变量,则分配

在PHP中,我发现自己经常编写这样的代码: $a = isset($the->very->long->variable[$index]) ? $the->very->long->variable[$index] : null; 有没有更简单的方法来做到这一点? 最好不要求我写$the->very->long->variable[$index]两次。 可悲的是,因为RFC已被拒绝。 由于isset不是函数,而是语言结构,所以不能为这种情况编写自己的函数。 注意:因为这是一种语言

Order of evaluation and @ sign in the PHP code segment?

This question already has an answer here: What is the use of the @ symbol in PHP? 10 answers Reference — What does this symbol mean in PHP? 18 answers

评估顺序和@登录PHP代码段?

这个问题在这里已经有了答案: PHP中@符号的用法是什么? 10个答案 参考 - 这个符号在PHP中的含义是什么? 18个答案

@ symbol in PHP

What are the advantages of using $exists = @$userID; statement, compared to the longer notation of if(isset($userID)){$exists = $userID; }else{$exists = false;} if(isset($userID)){$exists = $userID; }else{$exists = false;} Is the "@" notation a lot faster, because I find it less readable ? 显然,在谷歌搜索5秒后,它抑制了错误报告。

PHP中的@符号

使用$exists = @$userID;的优点是什么? 语句,与if(isset($userID)){$exists = $userID; }else{$exists = false;}的较长表示if(isset($userID)){$exists = $userID; }else{$exists = false;} if(isset($userID)){$exists = $userID; }else{$exists = false;} "@"符号快得多,因为我觉得它的可读性较差? 显然,在谷歌搜索5秒后,它抑制了错误报告。

Why a function starts with @ in php ? like @unpack(xxx)?

This question already has an answer here: What is the use of the @ symbol in PHP? 10 answers PHP Error Control Operators. it avoids showing the error returned by the function. more info in the link: http://www.php.net/manual/en/language.operators.errorcontrol.php

为什么函数在php中以@开头? 像@unpack(xxx)?

这个问题在这里已经有了答案: PHP中@符号的用法是什么? 10个答案 PHP错误控制操作符。 它避免了显示函数返回的错误。 链接中的更多信息: http://www.php.net/manual/en/language.operators.errorcontrol.php

what is the @'s purpose in PHP

Possible Duplicate: What is the use of @ symbol in php? I been working with PHP right now but a question pops in my mind what the @ sign means? I saw it always before method or functions calls. and I try to remove them there is no changes. Can anyone explain me what is the purpose of this @ sign?? @imagecreatefromjpeg($file); 简而言之, @允许您抑制由于调用函数而产生的任何错误。 它可以抑

@在PHP中的目的是什么?

可能重复: 在PHP中使用@符号有什么用? 我一直在使用PHP,但是我的脑海里浮现出一个问题@符号的含义是什么? 我在方法或函数调用之前总是看到它。 我试图删除它们没有任何变化。 任何人都可以解释我这个@符号的目的是什么? @imagecreatefromjpeg($file); 简而言之, @允许您抑制由于调用函数而产生的任何错误。 它可以抑制错误信息 - 请参阅PHP手册中的http://us3.php.net/manual/en/language.operators.errorcontrol.