php syntax exam

March 3rd, 2009 by Dream☆Wing Leave a reply »

http://www.blueshoes.org/en/developer/syntax_exam/

在php手册中看到类型比较的时候发现的这个exam,正好测试了下基础,错了几个记录下来

21. $x = (array(’a'=>’foo’) == array(’b'=>’foo’));
what is $x?
you said: TRUE
right is: FALSE
好吧,我以为这两个数组比较时值都是ARRAY的说,其实两个数组在比较时并不是转换成字符进行比较的
array与array的比较,具有较少成员的数组较小,如果运算数 1 中的键不存在于运算数 2 中则数组无法比较,否则挨个值比较

22.$arrOne = array(”=>”);
$arrTwo = array(’9′=>’apple’, ‘15′=>’banana’, ‘20′=>’grapefruit’);
$x = array_merge($arrOne, $arrTwo);
what is $x?
1) array(”=>”, 0=>’apple’, 1=>’banana’, 2=>’grapefruit’);
2) array(”=>”, 9=>’apple’, 15=>’banana’, 20=>’grapefruit’);
you said: 2
right is: 1
具体见array_merge函数
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
如果你想完全保留原有数组并只想新的数组附加到后面,用 + 运算符。

50. $x = (bool)(”hello” == TRUE);
what is $x?
you said: FALSE
right is: TRUE
result: wrong
恩,比较运算符里有详细说明:
string,resource 或 number与string,resource 或 number比较时, 将字符串和资源转换成数字,按普通数学比较。

59. $a = “hello”;
$b = &$a;
unset($b);
$b = “world”;
what is $a?
you said: world
right is: hello

60. $a = “hello”;
$b = &$a;
unset($b);
what is $a?
you said: null
right is: hello

61. $a = “hello”;
$b = &$a;
$b = “world”;
unset($b);
what is $a?
you said: null
right is: world

好吧,引用后的变量unset不会影响到被引用的变量恩,我一直以为是$b指向$a,实际上是$b和$a指向了同一个地方(即变量内容)而已
在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。
当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。

66. $a = ‘foo’;
$b = isSet($a['bar']);
what is $b?
you said: FALSE
right is: TRUE
result: wrong
‘bar’ in $a['bar'] evaluates to int 0 (see the PHP Cheat Sheet at http://www.blueshoes.org/en/developer/php_cheat_sheet/). Then it is $a[0] and now each character in the string $a (foo) can be accessed with its char number, like an array. So that is an ‘f’. And that thing is set.
这个我以前是真的没注意。。。记下了

其实对于字符串和字符串的比较,觉得文档上的有点问题,字符串和字符串比较时并不会转换成数字进行比较,只有当其中这两个字符串都为数字字符串时才会作为证书比较。不过对于66中的转换以前还真没注意TAT

  • Share/Save/Bookmark
Advertisement

1 comment

  1. Dream☆Wing says:

    再仍个中文测试的地址http://phparch.cn/index.php/quiz

Leave a Reply

You must be logged in to post a comment.