这两个字符串在PHP数组中的区别是什么?


What's the difference between these two strings in a PHP array?

这是我在drupal主题中遇到的一个问题,但这个问题似乎是一些深奥的PHP问题,所以我想我应该在这里找到一个答案。

$vars['theme_hook_suggestions']数组用于查找模板文件。现在,如果我硬编码的值与看起来已经存在的值相同,它会找到模板。

从逻辑上讲,相同的输入应该==相同的输出,所以我觉得如果一个找到模板而另一个没有,那么两者之间的输入一定有某种不同。

那么我附加到这个数组的两个'page__gallery'字符串之间可能有什么区别?我试过通过拆分和重组来重建字符串。我试过在整个字符串和字符串中的每个字符之间进行编码比较。从各方面看,它们看起来都是一样的,但只有当我手动输入时才有效。

$str2 = 'page__gallery';
$vars['theme_hook_suggestions'][] = $str2; // if I comment this line out, it doesn't find the template.
print $str2; //output is page__gallery
$str1 = $vars['theme_hook_suggestions'][3];
$vars['theme_hook_suggestions'][] = $str1; // if I comment this line out, it finds the template still.
print $str1; //output is page__gallery
print $str1 == $str2; // 1
print $str1 === $str2; // 1
var_dump($str1) // Outputs: string(13) "page__gallery"
var_dump($str2) // Outputs: string(13) "page__gallery"

$vars['theme_hook_suggestions']的var_dump,其硬编码字符串值为:

array(6) { [0]=> string(10) "page__node" [1]=> string(13) "page__node__%" [2]=> string(14) "page__node__96" [3]=> string(13) "page__gallery" [4]=> string(20) "page__drisco_gallery" [5]=> string(13) "page__gallery" }

$vars['theme_hook_suggestions']的var_dump与已经存在的字符串相同,重新添加:

array(6) { [0]=> string(10) "page__node" [1]=> string(13) "page__node__%" [2]=> string(14) "page__node__96" [3]=> string(13) "page__gallery" [4]=> string(20) "page__drisco_gallery" [5]=> string(13) "page__gallery" }

第一个可以,第二个不行。

帮助!

print总是返回1http://php.net/manual/en/function.print.php

所以这一行不能正确测试

print $str1 === $str2; // 1

建议var_dump()是更好的选择