php 测试树枝中的变量相等性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4303113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing variable equality in twig
提问by Sam
In twig, is there an easy way to test the equality of 2 variables?
在twig 中,是否有一种简单的方法来测试 2 个变量的相等性?
{% if var1 = var2 %}
isn't valid, {% if var1 is sameas(var2) %}
only works if both are a strings...
{% if var1 = var2 %}
无效,{% if var1 is sameas(var2) %}
仅当两者都是字符串时才有效...
(from docs) "sameas checks if a variable points to the same memory address than another variable", like thats useful.
(来自文档)“sameas 检查一个变量是否指向与另一个变量相同的内存地址”,这样很有用。
So the only way I've found of comparing integers is to convert them both to strings:{% if var1|lower is sameas(var2|lower) %}
所以我发现比较整数的唯一方法是将它们都转换为字符串:{% if var1|lower is sameas(var2|lower) %}
回答by Russell Dias
As far as I'm aware Twig supports all of the standard logical operators ==, !=, <, >, >=, and <=.
Also, your first example {% if var1 = var2 %}
does not check for equality, it assigns var2
to var1
, you might want to change it to the comparison operator ==
.
据我所知,Twig 支持所有标准逻辑运算符==, !=, <, >, >=, and <=.
此外,您的第一个示例{% if var1 = var2 %}
不检查相等性,而是分配var2
给var1
,您可能希望将其更改为比较运算符==
。
The Twig sameas
built in test, is essentially a strict type comparison operator ===
, hence why they both need to be strings in your example.
Twigsameas
内置测试本质上是一个严格的类型比较运算符===
,因此在您的示例中它们都需要是字符串。
回答by webdeveloper
If you are comparing value which have a numeric value you can use:
如果您正在比较具有数值的值,您可以使用:
{% if (psong.songid) ==(song.id) %}