Zend认证试题

更新时间:2024-03-28 23:11:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Zend认证试题

第一章(PHP Programming Basics)

ZendChina官方:最重要的是要您要掌握好基础知识。这些知识是需要您一直去关注和学习的,因为它们是学习PHP语言的根基。考试中您没有准备到的部分也可能是您日常程序开发中不会接触到的知识,但是本章中的问题还是需要得到您的重视,毕竟,如果您对最基本知识了解不够,对于您更深入的学习和开发也会带来诸多不便。

1、Choose the selection that best matches the following statements:

PHP is a _____ scripting language based on the ____ engine. It is primarily used to develop dynamic

_____ content, although it can be used to generate ____ documents(among others) as well.

A.Dynamic, PHP, Database, HTML B.Embedded, Zend, HTML, XML C.Perl-based, PHP, Web, Static D.Embedded, Zend, Docbook, MySQL E.Zend-based, PHP, Image, HTML

2、Which of the following tags is not a valid way to begin and end a PHP code block? A.<% %> B. C. D. E.

3、Which of the following is not valid PHP code? A.$_10 B.${“MyVar”} C.&$something D.$10_somethings E.$aVaR

4、What is displayed when the following script is executed?

A.The value is: Dog B.The value is: Cat C.The value is: Human D.The value is: 10 E.Dog

5、What is the difference between print() and echo()?

A.print() can be used as part of an expression, while echo() can’t B.echo() can be used as part of an expression, while print() can’t C.echo() can be used in the CLI version of PHP, while print() can’t D.print() can be used in the CLI version of PHP, while echo() can’t E.There’s no difference: both functions print out some text!

6、What is the output of the following script? A. 128 B. 42 C. 242.0 D. 256 E. 342

7、Which values should be assigned to the variables $a, $b and $c in order for the following script to display the string Hello, World!?

A. False, True, False B. True, True, False C. False, True, True D. False, False, True E. True, True, True

8、What will the following script output?

A. A string of 50 random characters

B. A string of 49 copies of the same character, because the random number generator has not been initialized

C. A string of 49 random characters D. Nothing, because $array is not an array E. A string of 49 ‘G’ characters

9、Which language construct can best represent the following series of if conditionals?

A. A switch statement without a default case B. A recursive function call C. A while statement

D. It is the only representation of this logic E. A switch statement using a default case

10、What is the best way to iterate through the $myarray array, assuming you want to modify the value of each element as you do?

A. Using a for loop B. Using a foreach loop C. Using a while loop D. Using a do?while loop

E. There is no way to accomplish this goal

11、Consider the following segment of code:

What should go in the marked segment to produce the following array output?

A. foreach($result as $key => $val) B. while($idx *= 2)

C. for($idx = 1; $idx < STOP_AT; $idx *= 2) D. for($idx *= 2; STOP_AT >= $idx; $idx = 0) E. while($idx < STOP_AT) do $idx *= 2

12、Choose the appropriate function declaration for the user-defined function is_leap(). Assume

that, if not otherwise defined, the is_leap function uses the year 2000 as a default value:

A. function is_leap($year = 2000) B. is_leap($year default 2000)

C. function is_leap($year default 2000) D. function is_leap($year) E. function is_leap(2000 = $year)

13、What is the value displayed when the following is executed? Assume that the code was

executed using the following URL: testscript.php?c=25 A. 25 B. -5 C. 10 D. 5 E. 0

14、Consider the following script:

Each ? in the above script represents an integer index against the $s array. In order to

display the Hello, World! string when executed, what must the missing integer indexes be?

A. Group A: 4,3,0,4,9,9 Group B: 7,8 B. Group A: 1,3,0,4,9,9 Group B: 7,6 C. Group A: 1,3,2,3,0,4 Group B: 5,8 D. Group A: 0,4,9,9,9,9 Group B: 7,8 E. Group A: 4,3,0,4,9,9 Group B: 7,8

15、Run-time inclusion of a PHP script is performed using the ________ construct, while compile-time

inclusion of PHP scripts is performed using the _______ construct. A. include_once, include B. require, include C. require_once, include D. include, require

E. All of the above are correct

16、Under what circumstance is it impossible to assign a default value to a parameter while declaring a function?

A. When the parameter is Boolean

B. When the function is being declared as a member of a class C. When the parameter is being declared as passed by reference D. When the function contains only one parameter E. Never

17、The ____ operator returns True if either of its operands can be evaluated as True, but not both.

Your Answer: ____________________________

18、How does the identity operator === compare two values?

A. It converts them to a common compatible data type and then compares the resulting values

B. It returns True only if they are both of the same type and value C. If the two values are strings, it performs a lexical comparison D. It bases its comparison on the C strcmp function exclusively E. It converts both values to strings and compares them

19、Which of the following expressions multiply the value of the integer variable $a by 4?(Choose 2) A. $a *= pow (2, 2); B. $a >>= 2; C. $a <<= 2; D. $a += $a + $a; E. None of the above

20、How can a script come to a clean termination? A. When exit() is called

B. When the execution reaches the end of the current file C. When PHP crashes

D. When Apache terminates because of a system problem

答案附在下面,方便大家参考。

答案详解

1.唯一有意义的答案是B。PHP是一种基于Zend引擎的脚本语言,它通常被嵌入在HTML代码中。它主要被用来开发HTML文档,但是也可以用它来开发其他类型的文档,比如XML。

2.PHP编程中,人们不太使用<% %>和两个标签,但它们确实是合法的PHP界定符。标签是非法的,因此正确的答案是D。记住,根据php.ini文件中的配置不同,这当中的某些标签无法在特定的情况下使用。

3.PHP变量以一个美元符号为开头,后面跟上任意数量的数字、字母和下划线。${“MyVar”}是一个合法的变量名,它使用的是较松散的命名约定。&$something是对$something的引用。然而,变量名不能以数字为开头,$10_somethings是非法的,因此答案是D。

4.注意,$myarray的键值并没有打上引号。所以,正在访问的键不是myvalue字符串,而是常量myvalue的值。最终访问的是$myarray[10],值是Dog,答案是A。

5.尽管print()和echo()在绝大多数情况下可以互换使用,但它们之间还是有一处不同。print()是函数,有返回值;echo()实际上是一个语言结构,没有返回值,并且不能在表达式中使用。因此,答案是A。

6.%运算符表示取模,它返回两个操作数相除的余数。<<是左移运算符,相当于乘以2的N次方。最后的答案乘以了一个浮点数,改变了它的数据类型。但是,由于小数点后是零,因此输出的结果不包含小数部分。答案是256(D)。

7.根据条件式的逻辑,要想得到Hello, World!字符串,必须要在第一个if结构中满足else的条件。因此$a必须为False,然后$b也必须为False。最内层的条件语句的实现要求先前的两个变量($a和$b)是False,而$c必须是True(答案是D)。

8.正确答案是C。从PHP4.2.0开始,除非已经给定了一个伪随机整数列,否则不再需要用srand()函数初始化随机数生成器。此外,即使随机数生成器没有被事先播种,脚本仍然会生成49个伪随机字符。尽管$array变量是字符串,但可以用访问数组的方式进行访问——使用数字索引访问某个位置上的字符。最后,for循环将从1开始执行到50,也就是执行了49次。

9.用一系列的if?else语句来检查一个条件的代码块,最适合用switch语句来替代。 复制PHP内容到剪贴板 PHP代码:

因为if语句中有一个捕捉所有其他条件的else,对应的,switch代码块需要一个default。正确答案是E。

10.通常情况下,foreach语句是遍历数组的首选。但是,由于foreach语句是在数组的副本上进行操作,而我们需要给数组中每个元素重新赋值,所以在这里foreach就不适用了。尽管也可以用while循环和do?while循环,但由于数组是顺序索引的,最合适的语句还是for语句。因此答案是A。

11.由于题目只允许填写一行代码,唯一合适的是for循环,因此答案只能是C或者D。要选出能生成正确结果的for循环,我们必须先复习一下for循环的构成要素。PHP中,for循环是这样声明的:

for(<初始化>;<继续执行,直到>;<重复执行>)

<初始化>在循环开始时执行一次,然后for循环开始执行大括号内的代码,直到<继续执行,直到>的值为False。每完成一次循环,执行一次<重复执行>。因此,正确的代码块应该是: for ($idx = 1; $idx < STOP_AT; $idx *= 2) 答案是C。

12.5个选项中,只有两个是合法的PHP函数声明(A和D)。在这两个选项中,只有一个设置了参数的默认值——答案是A。

13.本题考察PHP中变量作用域的相关知识。你必须明确global关键字是如何将变量引入本地域的,以及$_GET、$_POST、$_COOKIE、$_REQUEST等超级全局变量的作用域。本题中,最终的数学表达式是5+25-25-10,等于-5,答案是B。

14.函数能被以一个包含着函数名的变量后面加上括号(以及必要的参数)的形式动态调用。对于Group A来说,合适的索引组合是0,4,9,9,9,9,产生字符串myfunction。另一方面,参数将使用${}结构的可变变量。对Group B来说,合适的索引应该是7和8,等于${‘a’}$和{‘b’}——即$a和$b。因此答案是D。

15.在较新版本的PHP中,require(或requier_once())和include()(或include_once())只有一个区别——如果包含的文件不存在,前者将产生一个致命错误,同时终止脚本的执行;而后者只会产生一个警告。因此答案E正确。

16.当参数被声明为通过引用传递时,你不能给它赋默认值,此时解释器期望获得一个能在函数内部进行修改的变量。答案是C。

17.正确答案是逻辑异或(xor)运算符。

18.全等运算符比较两个操作数的数据类型和值,两者中有一个不同,都会返回False。因此答案是B。

19.答案是A和C。A选项中,pow函数计算2的平方,答案是4。C选项中,左移运算符将$a的值左移两位,相当于乘以4。

20.答案是A。一段脚本并不会在执行到文件末尾时终止,所以当前文件才能被其他脚本包含。至于PHP和Apache崩溃,这两个说法就太恶搞了。

第二章(Object-oriented Programming with PHP 4)

ZendChina官方:尽管PHP 4不是一个成功的面向对象技术的典范,但是它适用于建立一个可行的面向对象的架构——你只需要了解一个有缺陷的对象模型的缺陷在哪,并围绕着它小心处理。

即使PHP 5在PHP处理对象的方式上已经带来了许多的变化,从而你可能受到吸引而忽视了PHP 4的能力。事实是,这个面向对象的程序设计是非常受大多数程序员欢迎的,这些程序员在开始开发自己的应用软件时都是使用这个\老\版本的PHP。这导致了有大量的OOP代码,甚至在你转向PHP 5之前,你也可能会经常使用到这些代码。

考试的 OOP 部分不仅仅测试常用的面向对象开发,同时也测试PHP 4实现OOP的独特方式。

1. What is the construct used to define the blueprint of an object called? Your Answer: ____________________________

2. At the end of the execution of the following script, which values will be stored in the $a->my_value array? (Choose 3) A. c B. b C. a D. d E. e

3. How can you write a class so that some of its properties cannot be accessed from outside its methods?

A. By declaring the class as private B. By declaring the methods as private C. It cannot be done

D. By writing a property overloading method

4. Which object-oriented pattern would you use to implement a class that must be instantiated only once for the entire lifespan of a script? A. Model-view-controller B. Abstract factory C. Singleton

D. Proxy E. State

5. A class can be built as an extension of other classes using a process known as inheritance. In PHP, how many parents can a child class inherit from? A. One B. Two

C. Depends on system resources D. Three

E. As many as needed

6. What OOP construct unavailable in PHP 4 does the following script approximate?

A. Multiple inheritance B. Interfaces C. Abstract methods D. Private methods E. Function overloading

7. Assume that a class called testclass is defined. What must the name of its constructor method be? A. __construct B. initialize C. testclass D. __testclass

E. Only PHP 5 supports constructors

8. How can a class override the default serialization mechanism for its objects? A. By implementing the __shutdown and __startup methods B. By calling register_shutdown_function() C. By implementing __sleep() and __wakeup()

D. The default serialization mechanism cannot be overridden

E. By adding the class to the output buffering mechanism using ob_start()

9. In PHP 4, which object-oriented constructs from the following list are not available?

? Abstract classes ? Final classes

? Public, private, protected (PPP) methods ? Interfaces A. Abstract classes B. PPP methods

C. Neither PPP methods nor interfaces D. None of the above are available E. All of the above are available

10. How would you call the mymethod method of a class within the class itself? A. $self=>mymethod(); B. $this->mymethod(); C. $current->mymethod(); D. $this::mymethod();

E. None of the above are correct

11. What will the following script output? A. 10 B. Null C. Empty D. Nothing E. An error

12. What will the following script output? A. 10 B. 5 C. 2 D. Null E. Nothing

13. Consider the following script. What will it output? A. 5

B. 10 C. Nothing

D. The constructor will throw an error E. 510

14. Consider the following segment of PHP code. When it is executed, the string returned by the $eight_tenths->to_string method is 8 / 10 instead of the expected 4 / 5. Why?

A. The reduce_fraction function must return a value

B. The reduce_fraction function should accept integer values C. The gcd function is flawed

D. You must pass the $eight_tenths object by-reference

E. You cannot pass instances of objects to anything but methods

15. What does the following PHP code segment do? <?php

require_once(\myclass::mymethod(); ?>

A. Calls the mymethod method in the class statically.

B. Creates and instance of myclass and calls the mymethod method. C. Generates a syntax error

D. Defaults to the last-created instance of myclass and calls mymethod() E. Calls the function named myclass::mymethod()

16. Do static class variables exist in PHP? A. Yes B. No

17. What will the following script output? A. 1 B. 2

C. An error, because a::$myvar is not defined D. A warning, because a::$myvar is not defined

E. Nothing

18. How can you load classes on demand as they are required by the interpreter? A. By using the __autoload magic function B. By defining them as forward classes C. By implementing a special error handler D. It is not possible to load classes on demand E. By including them in conditional include statements

19. _____________________ are used to provide high-quality solutions to a recurrent design

problem using object-oriented programming.Your Answer: ____________________________

20. What will the following script output?

A. Parent called B. An error C. A warning D. Nothing

答案附在下面,方便大家参考。

答案详解

1.类是对象的蓝图(对象是类的实例)。

2.正确答案是B、C和D。set_value方法使用了错误的表达式$this->$my_value,因此该方法实际上是空的(这在PHP5里会导致一个错误,但在PHP4中不会。——译者注)。

3.答案是C。PHP4中无法限制对类成员的访问,而在PHP5中则可以通过private关键字实现。

4.单件模式可以限制一个类被实例化的次数。

5.尽管其他编程语言允许多重继承,但在PHP的对象模型中却不可以。因此答案是A。

6.方框中的代码表现的是抽象方法(abstrace method)的实现。如果这个类继承自其他类,而my_funct方法在子类中被调用时没有覆盖,代码将抛出一个错误。虽然只是近似的实现了抽象方法,但在PHP4有限的对象模型中,这已经做得很好了。

7.PHP5有统一的构造函数(__construct()),但在PHP4中,构造函数就是和类有相同名称的方法。对于名为testclass的类,它的构造函数就是testclass()。答案是C。

8.__sleep()和__wakeup()能被用来自定义对象的序列化过程。正确答案是C。

9.PHP4中没有题目选项里所列的任何一个概念。答案是D。

10.PHP中,在类的内部访问其成员和方法,要用$this这个特殊变量。因此答案是B。

11.正确答案是D。my_class::_my_class()不是合法的构造函数(方法名的开头多了个_),因此脚本不会输出任何东西。你可能觉得这题是在考眼力而不是知识,是的,我们就是这么打算。仔细想想你就会同意——绝大多数的bug都是由错误的拼写造成的。这题并不是在戏弄你,而是考验你的排错能力。

12.PHP4把对象视作标量进行处理,当$a赋给$b时,解释器创建对象的副本,因此对后一个对象的赋值不会影响到原先的对象。答案是B。但是要注意,PHP5里就不是这样处理的了(将会输出10)。

13.一上来,构造函数my_class通过引用,将自身存储在了变量$global_obj中。你可能会因此觉得,当我们后来吧$global_obj->my_value的值变为10时,$a也会相应改变。不幸的是,new操作符返回的不是引用,而是对象的副本。脚本输出5,答案是A。

14.PHP中,把对象传递给函数或者方法时,默认传递的是值。这意味着通过参数传递给函数的对象,其实是对象的副本。这点导致了在函数或方法里对对象进行改动时,不会影响函数外的原先的那个对象。

回到第14题中,这就说明对象$eight_tenths从来没有被reduce_fraction函数改动过,而$fraction对象(参数)则被改动了。如果要在函数内部改动对象,就必须以引用的方式传递参数:

function reduce_fraction(&$fraction) 答案是D。

15.题中所示的语法是用来进行静态调用的。当方法被静态调用时,它们就像一个独立的函数,与任何类的实例无关。答案是A。

16.没有。PHP4只允许声明静态函数变量,没有静态类变量。

17.答案是A。类b的属性$myvar将在b的父类——类a调用构造函数时被定义,此外,像PHP4中的普通变量一样,定义类变量时也不需要给它赋值。类b在其父类调用构造函数之前就给$myvar赋了值,所以不管之后如何赋值,输出都是1。

18.PHP4中无法即时装载类——它们必须在使用前就仔细声明好。PHP5中,可以使用__autoload魔术函数提醒解释器在找不到需要的类时尝试自动调用。因此答案是D。

19.为软件设计和编程中的常规问题提供良好的解决方案,这显然是在说设计模式。

20.脚本什么都不输出(答案是D)。因为子类的构造函数不会自动调用父类的构造函数。

第三章(PHP as a Web Development Language)

ZendChina官方:如果没有互联网的存在,PHP开发者的生存将肯定面临非常严峻的问题,事实上如果不是互联网有着令人难以置信的受欢迎程度,从而需要更快和更简单的应用开发,人们可能还不知道PHP是否存在。

当开发网站时,通过header和cookie来实现HTML和HTTP交互处理这样的web开发的基础知识非常重要。另外,考试的这一部分也涵盖了在多请求时通过session来保持数据。 一旦你开始学习如何使用这个语言,这很可能是你继续了解更多深层的PHP开发知识的基础。因此为了确保你能顺利通过考试,熟悉这些概念非常重要。

1. How are session variables accessed? A. Through $_GET B. Through $_POST C. Through $_REQUEST D. Through global variables E. None of the above

2. What function causes the following header to be added to your server’s output?

引用:

Set-Cookie: foo=bar;

Your Answer: ____________________________

3. Under normal circumstances—and ignoring any browser bugs—how can a cookie be accessed from a domain other than the one it was set for? A. By consulting the HTTP_REMOTE_COOKIE header B. It cannot be done

C. By setting a different domain when calling setcookie() D. By sending an additional request to the browser

E. By using Javascript to send the cookie as part of the URL

4. How can the index.php script access the email form element of the following HTML form? (Choose 2)

A. $_GET['email'] B. $_POST['email'] C. $_SESSION['text’] D. $_REQUEST['email'] E. $_POST['text']

5. What will be the net effect of running the following script on the $s string? (Choose 2)

A. The string will become longer because the angular brackets will be converted to their HTML meta character equivalents B. The string will remain unchanged

C. If the string is printed to a browser, the angular brackets will be visible D. If the string is printed to a browser, the angular brackets will not be visible and it will be interpreted as HTML

E. The string is destroyed by the call to htmlentities()

6. If no expiration time is explicitly set for a cookie, what happens to it? A. It expires right away B. It never expires

C. It is not set

D. It expires at the end of the user’s browser session

E. It expires only if the script doesn’t create a server-side session

7. Consider the following form and subsequent script. What will the script print out if the user types the word “php” and “great” in the two text boxes respectively?

<form action=\<input type=\<input type=\</form>

A. Nothing B. Array C. A notice D. phpgreat E. greatphp

8. In an HTTPS transaction, how are URLs and query strings passed from the browser to the web server?

A. They are passed in clear text, and the subsequent transaction is encrypted B. They are encrypted

C. The URL is left in clear text, while the query string is encrypted

D. The URL is encrypted, while the query string is passed in clear text

E. To ensure its encryption, the query string is converted into a header and passed along with the POST information

9. What happens when a form submitted to a PHP script contains two elements with the same name?

A. They are combined in an array and stored in the appropriate superglobal array B. The value of the second element is added to the value of the first in the appropriate superglobal array

C. The value of the second element overwrites the value of the first in the appropriate superglobal array

D. The second element is automatically renamed E. PHP outputs a warning

10. How would you store an array in a cookie?

A. By adding two square brackets ([]) to the name of the cookie B. By using the implode function

C. It is not possible to store an array in a cookie due to storage limitations D. By using the serialize function

E. By adding the keyword ARRAY to the name of the cookie

11. What will the following script output?

A. 12345678910

B. 1234567890 C. 0123456789 D. Nothing E. A notice

12. By default, PHP stores session data in ________________. A. The filesystem B. A database C. Virtual memory D. Shared memory E. None of the above

13. When you write a cookie with an expiration date in the future to a particular machine, the cookie never seem to be set. The technique usually works with other computers, and you have checked that the time on the machine corresponds to the time on the server within a reasonable margin by verifying the date reported by the operating system on the client computer’s desktop. The browser on the client machine seems to otherwise work fine on most other websites. What could be likely causes of this problem? (Choose 2)

A. The browser’s binaries are corrupt

B. The client machine’s time zone is not set properly

C. The user has a virus-scanning program that is blocking all secure cookies D. The browser is set to refuse all cookies

E. The cookie uses characters that are discarded all data from your server

14. Assuming that the client browser is never restarted, how long after the last access will a session “expire” and be subject to garbage collection? A. After exactly 1,440 seconds

B. After the number of seconds specified in the session.gc_maxlifetime INI setting C. It will never expire unless it is manually deleted D. It will only expire when the browser is restarted E. None of the above

15. The ___________ function automatically transforms newline characters into HTML <br />tags

Your Answer: ____________________________

答案附在下面,方便大家参考。

答案详解:

1.尽管在register_globals被设置为on时,可以通过全局变量来访问session,但在较新版本的PHP中,为了避免造成安全隐患,php.ini文件已经把register_globals设置为off了。因此答案是E。

2.虽然header函数在这里也能用,但这题显然是在说setcookie或setrawcookie函数。

3.答案是B。浏览器不允许来自某个域名的HTTP事务更改另一个域名下的cookie,否则这将造成严重的的安全问题。

4.既然表单采用post方式传输,那么脚本将只可能从$_POST和$_REQUEST两个超级全局变量数组中取到值。元素名称(email)是键名,因此答案是B和D。注意,由于可能导致潜在的安全问题,这里不鼓励使用$_REQUEST。

5.本题考验你对HTML编码的认识以及代码查错能力。变量$s在被函数htmlentities()处理过后,结果返回给了变量$ss,而$s自己并没有被改变。因此答案是B和D。你可能觉得自己被戏弄了,但是记住,发现这样的小错误是捉虫能力中很重要的一部分。

6.如果没有设置过期时间,cookie将在用户会话结束时自动过期。cookie不需要服务器端会话的支持,因此答案是D。

7.表单使用post方式传输,所以无论在文本框中输入什么,其值都会传给$_POST超级全局变量,这里的$_GET数组没有值。答案是A。

8.HTTPS传输发生时,浏览器与服务端立刻完成加密机制的握手,之后的数据都是加密传输而不是明文传输——包括URL,查询字串。而在HTTP传输中,它们都是明文传输的,因此答案是B。

9.对于收到的查询字串和POST信息,PHP只是简单的把元素添加进对应的超级变量数组中。结果就是如果有两个元素同名,前一个会被后一个覆盖。答案是C。

10.只有B永远正确。虽然你可以用implode函数把数组转化成字符串,然后存在数组里,但却无法保证日后一定能用serialize()把这个字符串还原成数组。浏览器对单个cookie有容量限制,因此在cookie里存储数组不是个好主意。但事情也并非永远是这样,你仍然可以存储一些比较小的数组。

11.这又是一个考验debug能力的题。注意到了吗,在脚本的末尾,echo语句中的$output变量拼错了!脚本不会输出任何东西,答案是E。

12.答案是文件系统(A)。默认情况下,PHP把所有会话信息存储在/tmp文件夹中;在没有这个文件夹的操作系统中(比如Windows),必须在php.ini中给session.save_path设置一个合适的位置(如c:\\Temp)。

13.B和D是最有可能出问题并应该深入调查的地方。由于浏览器访问其他网站都正常,所以不可能是浏览器程序出了问题。杀毒软件通常不会选择性的只阻止安全的cookie(不过有可能会阻止所有的cookie)。你首先应当检查浏览器是否被设置为阻止所有cookie,这

是最有可能导致该问题的原因。同时,错误的时区设置也可能是根源——给cookie设置有效期时用得是GMT时间。可能会出现cookie在写入时就立刻过期,从而无法被脚本接收的情况。

14.session.gc_maxlifetime设置的是用户最后一次请求到session被回收之间的时间间隔。尽管数据文件并没有被真正删除,不过一旦session被回收,你将无法对此session进行访问。巧合的是,session.gc_maxlifetime的默认设置正好是1440秒,但这个数字是可以被系统管理员调整的。所以答案应该是B。

15.函数nl2br能实现这个功能。

第四章(Working with Arrays)

ZendChina官方:数组可能是PHP中最强大的部分。这门语言允许开发者在任何时候创建和操作数组,这个自由度很强大。你不仅可以混合和匹配关键字和值的不同类型,而且可以在它们上执行各种操作,从分类到分离再到聚合。

但是,强大的能力带来了很大的责任。一个可能存在的巨大数组的另一面(不是有意的双关语)是,了解操作这个数组的最好方式不是一项简单的任务。这部分考试不仅仅从理论的角度,也同样从实践的角度集中考察你对数组如何工作的理解能力。因此,预计你面对一个主要脚本时将很多问题,让自己理解它哪里错了,或者它的最终结果将会是什么。

1. Array values are keyed by ______ values (called indexed arrays) or using ______ values (called associative arrays). Of course, these key methods can be combined as well. A. Float, string

B. Positive number, negative number C. Even number, string D. String, Boolean E. Integer, string

2. Consider the following array, called $multi_array. How would the value cat be referenced

within the $multi_array array?

A. $multi_array['yellow']['apple'][0] B. $multi_array['blue'][0]['orange'][1] C. $multi_array[3][3][2]

D. $multi_array['yellow']['orange']['cat'] E. $multi_array['yellow']['orange'][1]

3. What will the $array array contain at the end of the execution of the following script?

A. a1, a3, a5, a10, a20 B. a1, a20, a3, a5, a10 C. a10, a1, a20, a3, a5

D. a1, a10, a5, a20, a3 E. a1, a10, a20, a3, a5

7. Which function would you use to rearrange the contents of the following array so that they

are reversed (i.e.: array ('d', 'c', 'b', 'a') as the final result)? (Choose 2)

A. array_flip() B. array_reverse() C. sort() D. rsort()

E. None of the above

8. What will the following script output? A. 1 B. b C. c

D. A warning. E. a

9. What is the simplest method of computing the sum of all the elements of an array? A. By traversing the array with a for loop B. By traversing the array with a foreach loop C. By using the array_intersect function D. By using the array_sum function E. By using array_count_values()

10. What will the following script output? A. 1 B. 2 C. 0 D. Nothing E. 0.3

11. What elements will the following script output?

A. 1 => 'b'

B. True => 'a', 1 => 'b' C. 0 => 'a', 1 => 'b' D. None

E. It will output NULL

12. Absent any actual need for choosing one method over the other, does passing arrays by value to a read-only function reduce performance compared to passing them by reference?

A. Yes, because the interpreter must always create a copy of the array before passing it

to the function.

B. Yes, but only if the function modifies the contents of the array. C. Yes, but only if the array is large.

D. Yes, because PHP must monitor the execution of the function to determine if changes are made to the array. E. No.

13. What will the following script output? A. NULL

B. 0 => 1, 1 => 2, 2 => 3 C. An invalid reference error D. 2 => 1, 1 => 2, 0 => 3 E. bool(true)

14. What will be the output of the following script?

Your Answer: ____________________________

15. What will the following script output? A. 78 B. 19

C. NULL D. 5 E. 0

答案附在下面,方便大家参考。

答案详解:

1.键名是整型数字(integer)的数组叫索引数组,键名是字符串的数组叫关联数组。正确答案是E。

2.cat被包含在另外两个数组中。顺藤摸瓜,首先,键yellow必须要用到,它跟在orange后面。最内部的数组是个索引数组,字符串cat是第二个值,它的索引是1。正确答案是E。

3.答案是B。foreach操作的是$array的副本,因此对原来的值没有影响。

4.只有asort函数能在保留原有索引关系的前提下进行排序。答案是B。

5.serialize函数能把复杂的数据结构转换成字符串,并可以用unserialize函数再转换回原先的结构。还有implode函数,它可以把数组中的所有元素组成一个字符串。

6.函数natsort()使用“自然排序”法对数组进行排序。在本题中,数组元素已经“自然”排列了,因此函数根本没有改变数组。答案是A。

7.array_flip()只能把数组中每个元素的键和值进行交换。rsort()和array_reverse()则能把题目中的数组逆向排序为需要的形式(’d’,’c’,’b’,’a’)。答案是B和D。

8.给数组中的元素设置数字键时,PHP从可用的最小的数字开始,递增设置。如果没有指定从哪个数字开始,PHP将从0开始。本题中,3是第一个元素的键,因此,第三个元素的键将被设置为4,最后一个元素是5。注意,1b不是数字。因此,键是1的值不存在,答案是D。

9.array_sum函数计算数组中所有元素的总和。答案是D。

10.脚本输出1(答案是A)。因为只有整型数字和字符串可以被用来做数组的键——浮点数字会被转换成整型数字。所以0.1和0.2会被转换成0,$array中只有0=>’b’这个元素。

11.这题试图把你的注意力从真正的问题上转移开。true等同于数字1,因此$array数组将只包含一个元素。然而在var_dump()函数里出现了一个错误——$array被错拼成了

$aray,少了一个“r”。因此var_dump将输出NULL(也可能是一个提示 ,这取决于你的设置)。答案是E。

12.这题有些绕人。首先,注意两点:第一,你并非一定要使用这两种方式来传递数组。如果需要用一个函数来修改数组的内容,通过引用传递将是唯一的方法——但题中不是这种情况;第二,题目强调把数组传递个一个只读函数。如果不是这样,由于对数组进行改变将产生一个该数组的副本,答案会是B。然而常规情况下,PHP需要创建一套结构来维持一个引用,另一方面,由于PHP采用懒拷贝(lazy-copy)——又叫写时拷贝(copy-on-write)——机制,变量在被改变之前不会产生副本,所以通过引用将数组传递给一个不修改数组内容的函数比通过值传递要慢,而通过值传递是一种快速、安全的在函数间共用数组的方式。答案是E。

13.答案是E。sort函数不产生或返回数组副本,而是直接对传递给它的数组本体进行操作。该函数只返回布尔值true,代表排序成功(或者false,代表出错)。注意,这里将数组$a1引用传递给了sort_my_array(),我们不赞成这样做,应该在函数中重新声明引用。

14.array_walk函数将一个指定函数应用在数组中的每个元素上。因此脚本glue函数将把数组中的所有元素连在一起,输出abcd。

15.本题主要考验你分析脚本的能力。你也许觉得这题很费解——但我们在调试别人写的代码时却不得不经常面对此类令人不悦的问题。相对于我们所见过的一些代码,这已经算相当简单了。脚本中的for循环了5次,每次都把键是数组$array中键为$i的值的值加进$sum。这听起来有点像绕口令,但如果你一步一步想,你将发现, 当$i等于零时,

$array[$array[$i]]等同于$array[$array[0]],也就是$array[1],也就是2。顺着这个思路,最终的答案是78。

第五章(Strings and Regular Expressions)

ZendChina官方:字符串是PHP的“瑞士军刀”——作为一种Web开发语言,PHP最常打交道的就是字符串。因此对于开发者来说,处理字符串是一项非常基础的技能。

幸运的是,由于PHP开发团队的努力,PHP对字符串的处理相当易学。你只需迈过第一个难关,接下来就一马平川了。

但是,PHP的这一部分功能并非完美。本章考验你对字符串的了理解及对处理字符串的函数的认识。此外,你还必须面对正则表达式——一个非常有用,却总是被开发者忽试的工具——的编写艺术。

1. Consider the following script. What line of code should be inserted in the marked location in order to display the string php when this script is executed?

A. echo chr($val); B. echo asc($val);

C. echo substr($alpha, $val, 2); D. echo $alpha{$val}; E. echo $alpha{$val+1}

2. Which of the following will not combine strings $s1 and $s2 into a single string? A. $s1 + $s2 B. \C. $s1.$s2

D. implode('', array($s1,$s2))

E. All of the above combine the strings

3. Given a variable $email containing the string user@example.com, which of the following statements would extract the string example.com?

A. substr($email, strpos($email, \B. strstr($email, \C. strchr($email, \

D. substr($email, strpos($email, \E. strrpos($email, \

4. Given a comma-separated list of values in a string, which function from the given list can create an array of each individual value with a single call? A. strstr()

B. Cannot be done with a single function C. extract() D. explode() E. strtok()

5. What is the best all-purpose way of comparing two strings? A. Using the strpos function B. Using the == operator C. Using strcasecmp() D. Using strcmp()

6. Which of the following PCRE regular expressions best matches the string php|architect? A. .*

B. ...|......... C. \\d{3}\\|\\d{8} D. [az]{3}\\|[az]{9} E. [a-z][a-z][a-z]\\|\\w{9}

7. Which of the following functions can be used to determine the integrity of a string? (Choose 3) A. md5() B. sha1() C. str_rot13() D. crypt() E. crc32()

8. Which PHP function does the following script simulate on a UNIX machine?

A. fopen() B. fread() C. flock() D. split_string()

E. file()

9. Which of the following functions can be used to break a string into an array based on a specific pattern? (Choose 2) A. preg_split() B. ereg() C. str_split() D. explode() E. chop()

10. What will the following script output?

A. Testing 1245 B. Testing 345 C. Testing 1+245 D. 245 E. Nothing

11. What will be the output of the following script? A. 12345 B. 12245 C. 22345 D. 11345 E. Array

12. Which of the strings below will be matched by the following PCRE regular expression? (Choose 2)

A. ******123 B. *****_1234 C. ******1234 D. _*1234 E. _*123

13. Which of the following comparisons will return True? (Choose 2)

A. '1top' == '1' B. 'top' == 0 C. 'top' === 0 D. 'a' == a E. 123 == '123'

14. What happens if you add a string to an integer using the + operator? A. The interpreter outputs a type mismatch error

B. The string is converted to a number and added to the integer C. The string is discarded and the integer is preserved

D. The integer and string are concatenated together in a new string E. The integer is discarded and the string is preserved

15. Consider the following script. Assuming that http://www.php.net can be successfully read, what will it output?

A. The length of the www.php.net homepage

B. The length of the www.php.net homepage stripped of all its <p> tags C. 1 D. 0

E. The length of the www.php.net homepage stripped of all its tags except for <p> tags

16. The ___________ function can be used to compare two strings using a case-insensitive binary algorithm A. strcmp() B. stricmp() C. strcasecmp() D. stristr() E. None of the above

17. Which of the following functions can be used to convert the binary data stored in a string into its hexadecimal representation? (Choose 2) A. encode_hex() B. pack() C. hex2bin()

D. bin2hex() E. printf()

18. The ________________ function can be used to ensure that a string always reaches a specific minimum length.

Your Answer: ____________________________

19. What will the following script output?

Your Answer: ____________________________

20. What will the following script output? A. x B. axle C. axxle D. applex E. xapple

答案详解:

1.substr函数能够胜任,但考虑到输出三个字母就需要三次调用该函数,所以排除此方法。那么$alpha{$val}和$alpha{$val+1}是仅有的两个可能输出题目要求的字符串的选项。因为0是数组的第一个索引,所以答案是D。

2.除了A以外的选项都能输出题目要求的字符串。PHP中,加号(+)不能把两个字符串合并成一个。

3.substr函数返回字符串的一部分,而strpos函数擅长从一个字符串中找出某个指定的子串。同时使用这两个函数将满足题目要求。注意,前一个函数从0开始索引,而后者不是,因此需要+1。答案是D。

4.答案是D。explode函数使用一个字符串分隔另一个字符串,并把结果放入一个新建的数组。strtok函数也可以做同样的事,但需要多次调用。

5.答案是D。strcmp()提供了安全的字符串比较机制。注意,选项C是错的,strcasecmp()不是一个“万能”函数,因为它不区分大小写。

6.选项中没有一个正则表达式能真正代表题目所给字符串的匹配方式,但是选项A和E仍然能勉强匹配。选项A太普通了,它能够匹配任何字符串,因此答案是E。

7.正确答案是A,B和E。用crypt()和str_rot13()来验证一个字符串是否被改变,效率很低。crc32()比前面两个函数好些,如果能容忍一些小错误的话,它是个不错的选择。

8.file函数将文件的文本内容读入一个数组,每个元素是一行。因此答案E正确。也许你想知道为什么要把这样一个题目放在讲字符串的章节中,那是为了提醒你每一章的题目所包含的知识点并不是绝对严格区分开的,正如写PHP脚本时,file函数不能脱离字符串函数单独存在一样。

9.尽管条件不同,但preg_split和explode函数都能满足题目要求。ereg()拿一个正则表达式匹配一个字符串;str_split()按固定长度分隔字符串;而chop()则是rtrim()别名,用来移除字符串末尾处的空格。

10.本题考察你对字符串操作及操作符优先级的认识。连接运算符(.)的优先级比加号(+)高。因此PHP解释器实际执行的运算可以表示为('Testing ' . 1) + (2 . '45')。由于字符串test 1不是数字,所以加号前面的运算等于0。加号后面的运算等于245,PHP输出的结果是0+245,等于245,所以答案是D。

11.可以用访问数组元素的方式访问字符串中的字符,因此脚本只是把字符串中的第二个字符($s[1])替换成了字符2,最终将输出12245。答案是B。

12.本题的要点是理解这个正则表达式的含义——从左往右,首先是零个或多个任意字符(.*),跟着是一个星号(\\*),然后是123,最后是一个数字。因此答案是C和D。

13.B和E正确。选项B中,在比较时,字符串top等同于数字0。==操作符不比对数据类型,所以将返回true。答案E中,字符串123等同于数字123,比较将返回true。

14.字符串将被转换成数字(如果无法发生转换就是0),然后与整型数字相加。答案是B。

15.代码的本意是剔除www.php.net主页上除了p以外的的所有HTML标签。可实际上,在代码的最后一行使用了count函数,它统计变量中的元素数量,而不是字符串中的字符数。由于字符串是标量,对字符串使用count函数将永远返回1。答案是C。

16.题目其实就是在描述strcasecmp函数的作用,因此答案是C。

17.正确答案是B和D。pack函数能对二进制数据进行复杂的格式化,包括将字符串中的字符转化成十六进制表示。bin2hex函数也有同样的转化功能。注意,printf()能将整数转化成十六进制数,但无法转化字符串。

18.这是在说str_pad函数,它可以把字符串填充到指定长度。

19.脚本将输出ablecostscindy。wordwrap函数通常用来把字符串切割成指定长度。然而在本题中,长度被设置为1,因此函数将从空格处切割(第四个参数被设置为false,因此函数不会从单词的中间进行切割)。填充字符串是c,等于把每个空格都换成了c。

20.substr_replace函数是用一个指定字符串替换原字符串中的某个部分,因此脚本输出axle,答案是B。

第六章(Manipulating Files and the Filesystem)

ZendChina官方:你可能觉得PHP的文件操作功能并不怎样,但实际上它对开发者来说非常有用。即使你是做网站开发的,学会相关技能也能让你如虎添翼。多亏了流包装器(stream wrappers,将在第十章详细介绍),PHP才能够打开并读取远程文件,让在本地使用第三方网站的内容变得可能。

站在更底层的角度,文件输入/输出能完成多种任务。可以用他读取预制文件的内容,比如第三方提供的内容;或者通过PHP脚本让浏览器打开一个二进制文件,使得你能更切实的控制它。无论如何,本章不仅考验你打开、关闭和读取文件的能力,还考查多进程下进行文件操作的基础知识——例如文件锁。

1.函数______能读取文本文件中的一行。读取二进制文件或者其他文件时,应当使用______函数。

A.fgets(), fseek() B.fread(), fgets() C.fputs(), fgets() D.fgets(), fread() E.fread(), fseek()

2.文件指针能在PHP脚本结束时自动关闭,但你也可以用______函数来关闭。 答案:____________

3.考虑如下PHP脚本,它一行一行的读取并显示某文本文件的内容。在问号处填入什么才能使脚本正常运作?

A.file_get_contents($file) B.file($file) C.read_file($file) D.fgets($file) E.fread($file)

4.以下哪种方法能保证锁在任何竞争情况下都安全? A.用flock()锁住指定文件

B.用fopen()在系统的临时文件夹里打开文件 C.用tempnam()创建一个临时文件

D.用mkdir()创建一个文件夹来当 E.用tmpfile()创建一个临时文件

5.以下哪个函数能够获得文件的全部内容,并能够用在表达式中?(双选) A.file_get_contents() B.fgets() C.fopen() D.file() E.readfile()

6.在不把文件内容预加载到变量中的前提下,如何解析一个以特殊格式格式化过的多行文件?

A.用file()函数把它分割放入数组 B.用sscanf() C.用fscanf() D.用fgets() E.用fnmatch()

7.考虑如下脚本,最后文件myfile.txt的内容是什么?

A.什么都没有,因为$array实际上是一个字符串,而不是数组 B.49个随机字符 C.50个随机字符 D.41个随机字符

E.什么都没有,或者文件不存在,脚本输出一个错误

8.函数delete是做什么的? A.删除文件 B.删除文件夹 C.释放变量 D.移除数据库记录 E.没有这个函数!

9.考虑如下脚本,哪个PHP函数和它的功能最接近?

A.file_get_contents() B.file_put_contents() C.没有这样的函数 D.file() E.fputs()

10.如果你的脚本无法正确识别一个存储于另一个平台上的文件的行结尾,你该怎么办? A.改变auto_detect_line_ending的设置 B.用正则表达式侦测行的最后一个字母 C.用fpos() D.用ftok() E.每次读取一个字符

11.如果想要可读可写得打开一个文件,该给fopen()传什么参数?(双选) A.w B.r C.a D.+

12.能够读写常规文件中的二进制数据的函数是______,该函数返回的资源能被fgets()使用。

答案:__________

13.以下哪些函数能读取文件的全部内容?(三选) A.fgets()

B.file_get_contents() C.fread() D.readfile() E.file()

14.哪个函数能够往文本文件中写入一个字符串? 答案:____________

15.考虑如下脚本。运行时,尽管文件test.txt已经被用unlink()函数删除,脚本仍然输出1,1。在脚本的最后添加什么函数才能解决这个问题?

A.clearstatcache() B.fflush() C.ob_flush() D.touch() E.以上都不对

16.函数______能判断一个文件是否可写。 答案:____________

17.以下哪个选项能将文件指针移到开头? A.reset() B.fseek(-1)

C.fseek(0, SEEK_END) D.fseek(0, SEEK_SET) E.fseek(0, SEEK_CUR)

18.stat()和fstat()有什么区别?

A.stat()基于文件指针工作,fstat()基于路径工作 B.fstat()基于文件指针工作,stat()基于路径工作 C.fstat()不能处理文件 D.stat()不能处理文件 E.fstat()是stat()的别名

19.以下哪个选项准确的描述出了方框中的脚本的作用?

A.计算Windows系统C盘的剩余磁盘空间大小 B.输出一个表示C盘剩余空间所占比例的两位小数 C.输出C盘剩余容量的byte数 D.计算C盘总容量与剩余空间的比率 E.以上都不对

20.假设image.jpg存在并能够被PHP读取,调用以下脚本时,浏览器上显示什么?

A.一张JPEG图片 B.一个二进制文件 C.下载一个二进制文件 D.下载一张JPEG图片 E.一张残破的图片

答案详解

1.fgets函数主要用来从文本文件中读取一行,当然你也可以指定每次读取的最大长度。fread函数主要用来读取二进制数据。答案是D。

2.函数fclose能关闭文件指针。

3.fgets函数能从文件中读取单独一行。因此答案是D。

4.正确答案是D。这题很难,而且在实践中不大可能会碰到这样的问题——但这不正是你读这本书的原因吗?!你必须记住,flock()使用一种“协议”锁定机制,即所有其他访问此文件的进程也必须要使用flock()。如果某个进程没有这么做,竞争就会产生,锁就不安全。用mkdir创建一个文件夹能保证任何时刻只有一个进程处理能处理某文件,即保证操作的原子性。因此,你可以创建一个临时文件夹并“护”住它,直到I/O操作完成。

5.只有file_get_contents和file函数返回文件的全部内容,因此答案是A和D。readfile函数也能读取文件的全部内容,但它直接把内容送入了输出缓存,因此不能用在表达式中。

6.fscanf函数能根据指定格式解析文件内容,因此答案是C。sscanf函数只能用来操作字符串。

7.答案是E。注意,文件被以r模式打开,即只读模式。因此,如果文件不存在,PHP将输出一个错误来指出没有找到文件。如果文件存在,fopen()将被成功调用,但由于是以只读方式打开,fwrite()会失败。如果我们用w代替r,脚本就能正常运行,并且myfile.txt内将有50个随机字符(记住,可以像访问数组那样使用索引来访问字符串)。

8.答案是E。PHP里没有叫delete()的函数。删除文件用unlink(),删除文件夹用rmdir(),数据库记录用SQL语句删除,释放变量用unset()。

9.脚本实现的功能与file_put_contents()最接近,但这个函数直到PHP5才被引入,因此答案是C。

10.PHP4.3.0开始,php.ini引入了auto_detect_line_endings设置,系统在保存文本文件时能够自动侦测行结束符号的类型,因此答案是A。

11.要可读可写的打开文件,你必须使用r+模式,因此答案是B和D。

12.这是在说fopen()函数。

13.正确答案是B,D和E。file,readfile和file_get_contents都能读取文件的全部内容。

14.fwrite()和fputs()两个函数在这里都可以,而后者其实是前者的别名。在PHP中,写入二进制数据和写入字符串没有区别。

15.PHP会缓存某些文件系统函数的返回值——包括file_exists(),这样能提高脚本处理重复操作时的效率。当脚本里有大量删除文件的操作时,缓存很容易就会过时,因此需要清理缓存。答案是A。

16.这是在说is_writeable函数,它返回一个表示文件是否可写的布尔值。

17.正确答案是D。fseek()用来移动文件指针。SEEK_SET指出偏移量从文件开头开始计算。如果没有特别指出,SEEK_SET就是fseek()的默认模式。注意,rewind函数等效于fseek(0,SEEK_SET)。

18.答案是B。fstat函数通过已打开的文件指针取得文件信息,stat()获取指定路径的文件信息。

19.正确答案是B。disk_free_space函数能确定指定设备上(本题中即Windows下的C盘)的剩余磁盘空间(单位是byte),而disk_total_space()能确定设备的总容量。两者相除,再乘以百分率,最后用number_format()保留两位小数,脚本输出的就是剩余磁盘空间所占的比例。最后在加上百分号以防混淆。

20.答案是E。你注意到两个代码块之间的空行了吗?它将被输出到浏览器上,使得整个图片的二进制数据出错。因此浏览器将显示一个破碎的图片(或者是一条信息,指出图片出错)。

第七章(Date and Time Management)

ZendChina官方:从某一点上来看,几乎所有的网站都需要处理日期与时间。假如你需要收集用户的生日,或者记录某个特定事件的发生时间,PHP的日期函数将很好的帮助你完成任务。

但是PHP的日期/时间管理功能并不完美。它基于UNIX时间戳运行,容易受到攻击,作为开发者,你必须谨慎处理可能遇到的恶意数据。

同时,在Web上进行日期管理是一件国际性的事务。你必须能依据时区、地区的不同来显示对应的日期信息。本章测试题将考察以上所述的全部内容。 问题

1.以下脚本在Windows系统上输出什么?

A.-14462 B.14462 C.-1 D.0 E.一个错误

2.哪个函数能根据区域设置来格式化输出一个时间戳? 答案:__________

3.以下脚本是做什么的?

A.测算for循环的执行时间 B.测定服务器的时钟频率

C.计算操作系统的硬件时钟频率与软件时钟频率的差

D.测算for循环、一个array_sum()函数与一个microtime()的总执行时间 E.测算for循环、两个array_sum()函数与两个microtime()的总执行时间

4.以下脚本的标记处应该填入什么函数?

A.date() B.strftime()

C.microtime() D.checkdate() E.mktime()

5.以下脚本在Windows系统中输出什么?

A.一个警告 B.一个错误 C.-1和一个警告 D.-14462

E.一个提示,指出mktime不支持种输入

6.EST是CST之前的一个时区(就是说任何时候EST都比CST晚一个小时)。那么以下脚本输出什么? A.-3600 B.3600 C.0 D.-1 E.1

7.处理数据库中读取的日期数据时,以下那种方法有助于避免bug?(三选) A.确保日期数据与服务器使用相同的时区

B.如果日期需要被转换成UNIX时间戳进行操作,要确保结果不会溢出 C.用数据库功能测试日期的合法性 D.如果可能,用数据库功能计算日期的值 E.用代码控制日期只能在PHP中进行处理

8.在时区设置为Moscow, Russia的Windows操作系统上运行以下脚本,将输出什么?

A.输出数字0 B.输出数字-1 C.输出数字1 D.报错 E.什么都不输出

9.以下哪个选项对time函数的描述最准确? A.返回从UNIX纪元开始到现在经过的秒数

B.以GMT时区为基准,返回从UNIX纪元开始到现在经过的秒数 C.以本地时区为基准,返回从UNIX纪元开始到现在经过的秒数 D.计算从UNIX纪元开始经过的时间,并以整型数字表示 E.以上都对

10.以下脚本输出什么?

A.00:00:00 B.12:00:00 C.00:i:00 D.12:i:00 E.-1

11.以下哪个表达式能让cookie在一小时后过期(假设客户端的时间和时区设置都正确,并且客户端与服务器不在同一个时区)? A.time() + 3600 B.time(3600) C.gmtime() + 3600 D.gmtime(3600) E.A和C都对

12.getdate()函数返回______。 A.一个整数 B.一个浮点数 C.一个数组 D.一个字符串 E.一个布尔值

13.要把microtime()的输出转化成一个数字值,以下那种方法最简便? A.$time = implode (' ', microtime());

B.$time = explode (' ', microtime()); $time = $time[0] + $time[1]; C.$time = microtime() + microtime();

D.$time = array_sum (explode (' ', microtime())); E.以上都不对

14.以下哪个函数返回的不是时间戳?(双选) A.time() B.date() C.strtotime() D.localtime() E.gmmktime()

15.GMT时区下的时间戳与你所在时区下的时间戳的秒数差距有多大? A.取决于你所在时区与GMT时区的时间差 B.没有差别

C.只当你也在GMT时区时才会相同 D.永远不会相同 E.以上都不对

答案

1.本题实际上很容易回答。特别指出操作系统平台其实是为了迷惑你,事实上,无论是在Windows、Linux或其他类UNIX等使用旧版glibc的平台上,strtotime函数都不能识别UNIX纪元之前的日期(译者注:PHP5里修正了这个问题),因此脚本输出-1,答案是C。

2.正确答案是strftime()。date函数只能格式化输出英文表示的日期,而strftime()则能根据脚本中的地区设置(可以通过stlocale()来改变)来决定在输出中使用的语言。

3.答案是D。脚本中两次调用microtime()都能够获得当前时间,然而只有第二次执行该函数的时间会被算进时间段中。同样,第一次调用array_sum()函数被算进时间段,而第二次不会,因为第二次array_sum()是在microtime()后执行的。

4.checkdate函数能检查格里高利日期(Gregorian date)的合法性(这个函数有一些局限性,比如October 5-14, 1582,尽管日历上并没有这个日期,但函数仍然认为它合法)。这个脚本实质上是创建了一个随机日期并验证它的合法性,答案是D。

5.和第一题不同,本题中脚本执行的平台会影响结果。在Windows下,mktime函数不支持负值(UNIX纪元之前的时间),函数将返回-1(和一个警告)。正确答案是C。

6.由于两个时区有一小时的时差,而strtotime()能把文本表示的日期转换成时间戳,因此结果只能是3600或者-3600(答案A或者B,即一小时相当于的秒数)。现在,最重要的是紧记,CST比EST晚一个小时,也就是当CST是午夜12点时,EST已经是次日了。因此$b比$a大,结果是负数,答案是A。

7.数据库存储日期/时间的能力比PHP强。大多数DBMS能够处理格里高利日历上所有的日期,而基于UNIX时间戳的PHP只能处理较短的一个时间段里的日期。因此在脚本中处理日期时,必须确保在它转换成时间戳后不会溢出(答案B)。此外,在处理日期时,无论是验证一个日期的合法性(答案C)还是进行计算(答案D),都最好尽量让数据库来完成。

8.这是个很迷惑人的问题,但其实很简单。传给gmmktime()的值其实就是UNIX纪元,转化成时间戳后是0。gmmktime()的内部用到mktime(),后者基于当前地区工作。这里的问题是,UNIX纪元不能被用在本初子午线以东的时区——比如莫斯科。这将产生一个负值,Windows操作系统不支持。因此脚本输出-1,答案是B。也可能会输出一个警告,取决于你

的设置(但绝对不是答案D说的“一个错误”)。

9.答案是E。很显然,time()计算从UNIX纪元开始至今经过的秒数,答案A和D都表述了这一点,它们都是正确的。答案B和C说的不是那么直接,但其实也表达了相同的意思。UNIX纪元是一个确定的时间点,因此无论你使用GMT时区或其他你所在的时区,从UNIX纪元至今经过的秒数都是一样的。只有在你把时间戳转换成人们能够理解的表达形式时才需要考虑时区。所以答案B和C也是正确的。这题似乎有点戏弄人的意思,但实际上它考验你对绝对时间与相对时间二者不同之处的认识,这个概念在处理日期时相当重要。

10.传递给date()的参数H和s代表24小时制的时间与秒数。i被一个反斜线转义,因此它将被当作字母直接输出。调用strtotime()时没有给出参数,它将返回当天午夜的时间。因此最终的输出是00:i:00,答案是C。

11.正确答案是A,当前时间加3600秒(1小时*60分钟*60秒)。其他选项都会产生错误的时间戳。

12.getdate函数返回记录指定时间戳相关信息的数组(如果没有指定时间戳,就改用当前日期)。答案是C。

13.答案是D。microtime函数返回一个由时间戳和小数两部分组成的字符串,两部分由空格分开。因此explode()将字符串分割并放入数组,array_sum()把它们相加,转换成数字。

14.答案是B和D。date函数返回字符串,localtime()返回数组。

15.任何时区下的当前时间都是相同的——当前时间是一个绝对的时间点。答案是B。

第八章(E-mail Handling and Manipulation)

ZendChina官方:没有了E-Mail,这世界会变成什么样?网络交流把人们拉得更近,让公司能够更有效的开展业务,不幸的是,也带来了垃圾信息。

还好,你不需要成为一个反垃圾邮件专家就能使用PHP处理电子邮件的功能。事实上,不管你是在运营一家在线商店,还是在编写一套论坛程序,你都会发现发送邮件是你工作中很重要的一部分——与用户保持联系这点非常重要。

用PHP编写邮件管理脚本很简单,但也不失挑战性。如果你只是想发送一封文本邮件,mail函数对你来说就够了。而如果你需要处理更复杂的电子信息——例如HTML邮件和附件时,那么你就需要深入学习一下E-Mail的工作原理了。

1.以下哪个不是合法的电子邮件地址? A.john@php.net

B.\C.joe @ example.com D.jean-cóggeshall@php.net E.john

2.在PHP中,使用sendmail程序从Windows或Novell系统中发送邮件的方式,与从类UNIX系统中发送的方式不同。以下哪些选项说明了这个不同?(双选) A.Windows/Novell不需要第三方软件的支持就能实现该功能 B.UNIX中,sendmail_from的配置来决定了邮件头中的From:信息。

C.在Windows/Novell中,你无法发送有多个收件人的电子邮件,每个收件人都必须单独发送

D.有可能完全相同,这取决于sendmail_path的配置

E.不同与Windows/Novell,在UNIX中,你必须用SMTP和smtp_port配置好MTA主机和端口

3.通过PHP发送有多个收件人或者MIME兼容的邮件,需要遵循什么步骤? A.将必要的头信息(header)通过$message参数(第三个参数)传递给mail函数 B.用PHP代码,通过SMTP直接与MTA连接。

C.将附加的头信息传递给mail函数的$additional_headers参数(第四个参数),每个header一行。

D.向多个收件人发送E-Mail是允许的。PHP不支持发送MIME E-Mail。

E.向mail函数传递$additional_headers参数,每个header一行,行以\\r\\n结尾

本文来源:https://www.bwwdw.com/article/bxkr.html

Top