PHP三元语句
您好,我最近正在查看一个购物车分页器类,试图理解他们的代码,以便当我遇到以下代码行时我可以构建自己的分页器。它类似于三元语句,但以我以前从未见过的方式编写。我会用谷歌搜索,但我不知道该用谷歌搜索什么。有人可以告诉我它的工作原理是什么以及它的名称吗,以便我可以搜索它并了解更多信息。
return ($output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '')
. '<div class="' . $this->style_results . '">' . sprintf($this->text, ($total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit), $total, $num_pages) . '</div>';
请告诉我这是否足以继续下去 谢谢 安德鲁
Hi i was recently looking over a shopping cart paginator class, trying to understand their code so i could build my own paginator when i came across the following line of code. It resembles a ternary statement but is written in a way that i have never seen before. I would google it but i wouldn't know what to google. Could someone please tell me what this is how it works and what it is called so i can do a search for it and learn more.
return ($output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '')
. '<div class="' . $this->style_results . '">' . sprintf($this->text, ($total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit), $total, $num_pages) . '</div>';
Just let me know if this is enough code to go on
Thanks
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很好...它只是一个常规条件运算符(好吧,其中 3 个,以及一些串联)。
如果你重新格式化它,它会变得更清晰一些:
Nice... It is just a regular conditional operator (well, 3 of them, along with some concatenation).
If you reformat it, it gets a bit clearer:
它被称为条件运算符,我认为这是对它的滥用。条件运算符可用于将简短的 if-else 结构减少为一条语句,而不影响代码的可读性。
给定的代码可以写为:
并且相当于:
It is called conditional operator and I would consider this to be an abuse of it. Conditional operators can be useful in reducing short if-else constructs into one statement without effecting the readability of the code.
The given code can be written as:
And is equivalent to:
更多信息
在您的情况下:
如果 $output 变量不为空,则返回以下内容,否则返回空 '' 字符串。
代码中使用的其他三级运算符的情况也是如此。
More here
In your case:
If $output variable is not empty then following is return otherwise empty '' string is returned.
Same is the case with other tertiary operators used in your code.