三元运算符和if条件有区别吗?
php中的三元运算符和if条件有区别吗?
如果是,请提供。
Is there difference between ternary operator and if condition in php ?
if yes, kindly provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
三元运算符是一个运算符,因此它形成一个表达式。因此它会有一个值,您可以将其分配给变量或根据需要使用。它用于简单的情况,其中变量可以根据条件采用两个可能的值。
例如:
$status = $age > 18? 'adult' : 'child';
尽管可能,你不应该嵌套三元运算符。
控制结构
if
是完全不同的东西。它们唯一的共同点是都评估条件(真/假)。if
用于根据该条件的结果执行代码片段。您会发现自己大多数时候都在使用if
(事实上,没有三元也可以)。在某些情况下,如果将某些类型的if
替换为三元,则代码会更少且更具可读性,如下所示:The ternary operator is an operator, so it forms an expression. So it will have a value that you can assign to a variable or use however you want. It is used for simple situations where a variable can take two possible values depending on a condition.
For example:
$status = $age > 18 ? 'adult' : 'child';
Although possible, you should not nest the ternary operator.
The control structure
if
is something absolutely different. The only thing they have in common that both evaluate a condition (true/false).if
is used to execute code fragments based on the result of that condition. You will find yourself usingif
most of the time (in fact, you can live without the ternary). In certain situations, it is less code and more readable if you replace certain types ofif
s with the ternary, like this one:就我个人而言,我仅在适合一行的情况下使用三元运算符。如果它需要跨越,那么是时候讲老话了,
你也可以看到一个有趣的问题多个三元如何工作:
unusual三元运算
Personally, I only use the ternary operator if it fits on one line. If it need to span, then it's time for the good old
also you can see one interesting question how multiple ternary works :
unusual ternary operation
if 语句比三元语句更快,但在大多数情况下这并不重要。
这里是一篇关于性能的帖子If 语句与三元语句。
摘要:除非您正在评估大型对象,否则它基本上是相同的,因为三元复制了正在评估的对象。这是使用 PHP 5.3 我不确定它在当前版本中是否已更改。
If statements are faster than ternary, but in most cases it doesn't matter.
Here is a post on the performance of If statements vs ternary.
Summary: It's basically the same unless you are evaluating large objects because ternary copies the objects being evaluated. This was using PHP 5.3 I'm not sure if it has been changed in current versions.
三元运算符可以执行 if/else 语句可以执行的任何操作。在某些情况下,它可以提供简洁性和表达力,但要注意:它很容易被滥用。
我喜欢它的一件事是检查 null:
如果我的 PHP 内存正确地为我服务,那么它也可以用于左值:
但它很容易做得过头,例如在这个 C++ 示例中,它被用来代替循环和 if/else 语句:
谨慎使用。
The ternary operator can do anything that an if/else statement can. In some cases it can provide brevity and expressiveness, but beware: it is easily abused.
One thing I like it for is checking for null:
If my PHP memory serves me correctly, then it can also be used on an lvalue:
It can be easily overdone though, such as in this C++ example, where it is used in place of loops and if/else statements:
Use with caution.
也许还有另一个观点:性能。
不久前我注意到三元运算符比 if() 更快。我不知道这是否仍然适用于最新版本的 PHP。
<疯狂猜测>
原因可能是 if() 提供了更大的灵活性,例如使用代码块 (
{ }
)。三元运算符基本上只是测试一个表达式并根据测试结果获取一个值。回到我的汇编时代,我会说 if() 必须在内存中跳转,因此需要更多的时间来完成任务。
< /疯狂猜测>
然而,只有当代码执行相当多的次数(1k+)时,这一点才可能变得明显。
Maybe another point of view: Performance.
A while ago I noticed that the ternary operator is faster than an if(). I don't know if this still applies to the latest versions of PHP.
< wild guess >
The reason may be that an if() allows more flexibility like using code blocks (
{ }
). A ternary operator basically is just testing an expression and grabbing a value depending on the result of the test.Going back into my Assembler-days I'd say that an if() has to jump in memory and, therefore, takes a bit more time to do things.
< /wild guess >
However, this may be become noticeable only if the code is executed a decent number (1k+) of times.
如果您不使用任何琐碎的逻辑,那么维护会有很大的不同。
如果你遇到一个难题,在某些情况下你需要更多的时间来解决它,就像你的代码中有一个“if ... then”一样。并且确信:它已经发生了!
时间不是三元运算符的朋友。写得快,但理解不快,如果岁月流逝!
There is a big difference in maintenance, if you use none trivial logic.
If you have a difficult problem you need under circumstances much more time, to sove it, as if you have an 'if ... then' in your code. And be sure: it's happen!
The time is not a friend of ternary operator. Fast to write, but not fast to understand, if the years gone!