php面试题之三——PHP语言基础(基础部分)

22 篇文章 0 订阅

三、PHP语言基础

1. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部)

strlen和mb_strlen都是用于获取字符串长度。
strlen只针对单字节编码字符,也就是说它计算的是字符串的总字节数。如果是多字节编码,如 gbk 和 utf-8,使用 strlen 得到是该字符的总字节数;
可以使用mb_strlen获取其字符个数,使用mb_strlen 要注意两点,一是要开启 mbstring 扩展,二是要指定字符集。

总结:

  • strlen函数不管是字符串是单字节编码还是多字节编码,函数返回的结果都是字符串的总字节数。
  • mb_strlen函数当字符串是单字节编码时,函数返回的结果是字符串的总字节数。当字符串是多字节编码时,函数返回的结果是字符串的个数。
    mb_strlen函数在没有指定字符编码时,表示使用默认字符编码,即单字节编码,函数返回的是字符串的总字节数。
  • PHP默认是单字节编码(内部字符编码),多字节编码方式有gbk、utf-8等。

示例

  1. <?php
  2. /*
  3. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部)
  4. */
  5. header( 'Content-Type:text/html;charset=utf-8');
  6. // (1)英文字符串
  7. $str1 = "duang~";
  8. echo strlen($str1); //总字节数为6,内部字符编码,单字节编码
  9. echo "<br />";
  10. echo mb_strlen($str1); //总字节数为6,内部字符编码
  11. echo "<br />";
  12. echo mb_strlen($str1, 'utf-8'); //总字节数或字符长度为6,指定字符编码(utf-8),多字节编码
  13. echo "<hr />";
  14. // (2)中文字符串
  15. $str2 = "你是我的小苹果";
  16. echo strlen($str2); //总字节数为21,内部字符编码,单字节编码
  17. echo "<br />";
  18. echo mb_strlen($str2); //总字节数为21,内部字符编码
  19. echo "<br />";
  20. echo mb_strlen($str2, 'utf-8'); //字符长度为7,指定字符编码(utf-8),多字节编码
  21. ?>

相关题目 1:实现中文字串截取无乱码的方法。

方法一,使用 php 内置函数 mb_substr()
方法二,自定义函数,以 utf-8 为例,如下:

  1. <?php
  2. /*
  3. 相关题目 1:实现中文字串截取无乱码的方法。
  4. */
  5. header( 'Content-Type:text/html;charset=utf-8');
  6. //=================方法一=======================
  7. $str = "你是我的温暖阳光";
  8. echo mb_substr($str, 2, 4, 'utf-8'); //输出 我的温暖
  9. // ================方法二=======================
  10. /**
  11. * utf8编码字符串截取无乱码
  12. * @param $str string 要处理的字符串
  13. * @param $start int 从哪个位置开始截取
  14. * @param $length int 要截取字符的个数
  15. * @return string 截取后得到的字符串
  16. */
  17. function substr_utf8($str,$start,$length = null)
  18. {
  19. $sep = "";
  20. $arr = array_slice(preg_split( "//u", $str, -1,PREG_SPLIT_NO_EMPTY), $start,$length);
  21. return join($sep,$arr);
  22. }
  23. // 示例
  24. $str = "你是我的温暖阳光";
  25. echo substr_utf8($str, 2, 4); //输出 我的温暖
  26. ?>

相关题目2:如何求解字符串“中国2北333京”的字符数(一个中文一个字符),并找到第四个字符“北”

方法一,使用php内置函数,确保配置中已打开mbstring扩展
方法二,自定义函数实现求其长度,截取使用上面定义的substr_utf8

  1. <?php
  2. /*
  3. 相关题目 2:如何求解字符串“中国2北333京”的字符数(一个中文一个字符),并找到第四个字符“北”
  4. */
  5. header( 'Content-Type:text/html;charset=utf-8');
  6. //=================方法一=======================
  7. $str = "中国2北333京";
  8. echo mb_strlen($str, 'utf-8'); //输出字符数 8
  9. echo mb_substr($str, 3, 1, 'utf-8'); //输出 北
  10. // ================方法二=======================
  11. /**
  12. * utf8编码字符串截取无乱码
  13. * @param $str string 要处理的字符串
  14. * @param $start int 从哪个位置开始截取
  15. * @param $length int 要截取字符的个数
  16. * @return string 截取后得到的字符串
  17. */
  18. function substr_utf8($str,$start,$length = null)
  19. {
  20. $sep = "";
  21. $arr = array_slice(preg_split( "//u", $str, -1,PREG_SPLIT_NO_EMPTY), $start,$length);
  22. return join($sep,$arr);
  23. }
  24. /**
  25. * utf8编码字符串计算长度
  26. * @param $str string 要处理的字符串
  27. * @return int 字符串的长度或字符个数
  28. */
  29. function strlen_utf8($str)
  30. {
  31. return count(preg_split( "//u", $str, -1,PREG_SPLIT_NO_EMPTY));
  32. }
  33. // 示例
  34. $str = "中国2北333京";
  35. echo strlen_utf8($str); //输出字符数 8
  36. echo substr_utf8($str, 3, 1); //输出 北
  37. ?>

2. 下列哪个函数是用正则表达式将字符串分割到数组中 ( )

A. split
B. implode
C. explode
D. join
答案:A

3. 写出下列程序的输出结果(新浪网技术部)

  1. <?php
  2. /*
  3. 写出下列程序的输出结果(新浪网技术部)
  4. */
  5. $x = 87;
  6. $y = ($x % 7) * 16;
  7. $z = $x > $y ? 1 : 0;
  8. echo $z;
  9. ?>

答案:1

4. 写出下列几个预定义全局变量的作用 (新浪网技术部)

  1. $_SERVER[ 'DOCUMENT_ROOT'] //当前运行脚本所在的文档根目录
  2. $_SERVER[ 'HTTP_HOST '] //当前请求的 Host: 头部的内容
  3. $_SERVER[ 'REMOTE_ADDR'] //正在浏览当前页面用户的 IP 地址
  4. $_SERVER[ 'HTTP_REFERER'] //链接到当前页面的前一页面的 URL 地址
  5. $_SERVER[ 'SERVER_NAME'] //当前运行脚本所在服务器主机的名称
  6. $_FILES //包含有所有上传的文件信息
  7. S_FILES[ 'userfile'][ 'name'] //客户端机器文件的原名称
  8. $_FILES[ 'userfile'][ 'type'] //文件 MIME 类型,如果浏览器提供此信息的话,如“image/gif”。
  9. $_FILES[ 'userfile'][ 'size'] //已上传文件的大小,单位为字节
  10. $_FILES[ 'userfile'][ 'tmp_name'] //文件被上传后在服务端储存的临时文件名
  11. $_FILES[ 'userfile'][ 'error'] //和该文件上传相关的错误代码

5. include 和 require 都能把另外一个文件包含到当前文件中,他们有什么区别?Include 和include_once 又有什么区别?(新浪网技术部)

二者区别只有一个,那就是对包含文件的需求程度。include 就是包含,如果被包含的文件不存在的话,那么则会提示一个错误,但是程序会继续执行下去。而 require 意思是需要,如果被包含文件不存在或者无法打开的时候,则会提示错误,并且会终止程序的执行。
这两种结构除了在如何处理失败之外完全一样。
once 的意思是一次,那么 include_once 和 require_once 表示只包含一次,避免重复包含。

相关题目 1:What is the difference between include & include_once? include & require?(Yahoo)

相关题目 2:语句 include 和 require 都能把另外一个文件包含到当前文件中,它们的区

别是_;为了避免多次包含同一文件,可以用语句_来代替它们。
在如何处理失败时,include()产生一个警告而require()则导致一个致命错误;require_once()/include_once()

相关题目 3:What functions can you use to add library code to the currently running script?(Yahoo)

include、require

6. 用最少的代码写一个求 3 值最大值的函数. (51.com 笔试题)

  1. <?php
  2. /*
  3. 6. 用最少的代码写一个求 3 值最大值的函数. (51.com 笔试题)
  4. */
  5. // 定义函数
  6. function maxnum($a,$b,$c)
  7. {
  8. return $a > $b ? ($a > $c ? $a : $c) : ($b > $c ? $b : $c);
  9. }
  10. // 调用实例
  11. echo maxnum( 24, 15, 8);
  12. ?>

7. 简述 POST 和 GET 传输的最大容量分别是多少? (51.com 笔试题)

POST 根据你 php.ini 文件配置(默认是 8M)
GET 的话大小限制在 2KB

相关题目:表单中 get 与 post 提交方法的区别?

get 是发送请求 HTTP 协议通过 url 参数传递进行接收,而 post 是实体数据,可以通过表单提交大量信息。

8. 有三个 php 文件位于同一目录下,内容如下所示。使用浏览器访问 c.php,请问是否存在问题。如果存在问题,请指出修正方法并写出浏览器查看效果 ,如果不存在问题,请写出浏览器查看效果(酷讯 PHP 工程师笔试题)

A.php:

  1. <?php
  2. function fa(){
  3. echo “in Function A\n”;
  4. }
  5. ?>

B.php:

  1. <?php
  2. include ‘a.php’;
  3. function fb() {
  4. fa();
  5. echo “in Function B\n”;
  6. }
  7. ?>

C.php:

  1. <?php
  2. include ‘a.php’;
  3. include ‘b.php’;
  4. fa();
  5. fb();
  6. ?>

答案:存在问题,a.php 被包含了两次,导致 fa()函数重复定义,使用 include_once 避免重复包含。

9. echo(),print(),print_r()的区别?(新浪)

echo, print是PHP语句print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)
print只能打印出简单类型变量的值(如int,string)
print_r可以打印出复杂类型变量的值(如数组,对象)

echo -- 输出一个或者多个字符串
print --输出一个字符串
print_r -- 打印关于变量的易于理解的信息。

在实际使用中, print 和 echo 两者的功能几乎是完全一样。
可以这么说,凡是有一个可以使用的地方,另一个也可以使用。但是,两者之间也还是一个非常重要的区别:
在 echo 函数中,可以同时输出多个字符串,而在 print 函数中则只可以同时输出一个字符串。同时,echo函数并不需要圆括号,所以echo函数更像是语句而不像是函数。
echo 和 print 都不是函数,而是语言结构,所以圆括号都不是必需的。他们的区别在于:

(1) echo可以输出多个字符串,像下面这样:
echo 'a','b','c';
如果你非要加上圆括号,注意写成echo ('a','b','c');是错误的,应该写成:
echo ('a'),('b'),('c');
它没有像函数的行为,所以不能用于函数的上下文

(2) print只能输出一个字符串,它可以表现得像一个函数,比如你可以如下使用:
$ret = print 'Hello World';
(有返回值所以能够用在更复杂的表达式中,可以判断是否输出成功等表达式)所以它能用在更复杂的表达式中。

另外,echo的效率相对比较快~

如下代码:

  1. <?php
  2. $a= ‘hello ‘;
  3. $b= ‘php world!’;
  4. echo a , a, a,b. ’<br />’; //echo 可以用逗号分隔字符串变量来显示
  5. print a . a. a.b. ’<br />’; //而print不能使用逗号,只能用点号分隔
  6. print a , a, a,b. ’<br />’; //使用逗号时报错。
  7. ?>

说明

  1. ,是 echo 本身支持的一种语法,而.则是字符串连接操作符,使用,的效率要高一些。(少了连接运算)
  2. echo可以使用,来分隔字符串变量,也可以使用,来分隔字符串变量;print只能使用.来分隔字符串变量。
  3. echo效率比print高

总结
echo命令和print命令相同,没有区别
echo()和print()有区别:
echo()没有返回值,与echo命令相同
print()有返回值,总是返回1

补充:
printf()和sprintf()类似,均为格式化输出,不同的是前者输出到标准输出,后者输出到变量

相关题目:What is the difference between "print()" and "echo( )"? (腾讯)

answer: print is a function,echo is a language construct

10. 用 PHP 打印出前一天的时间格式是 2006-5-10 22:21:21。

  1. <?php
  2. /*
  3. 用 PHP 打印出前一天的时间格式是 2006-5-10 22:21:21。
  4. /
  5. // 方法一==
  6. echo date( “Y-m-d H:i:s”,time() - 3600 24);
  7. echo “<br />”;
  8. // 方法二==
  9. echo date( “Y-m-d H:i:s”,strtotime( "-1 day"));
  10. echo “<br />”;
  11. // 将表单中提交的时间字符串"2015-6-23"转成时间戳
  12. $date = strtotime( “2015-6-23”);
  13. echo $date;
  14. ?>

说明:

  • 使用strtotime可以将任何字符串的时间表示(now,seconds,day,week等)转换成时间戳,仅针对英文。
  • 在实际开发的时候,strtotime非常有用,因为对于表单来说,提交的数据都是字符串。比如,“2013-4-27”需要将其转换成时间戳然后存到数据库中。

相关题目:求两个日期的差数,例如 2009-3-1 ~ 2009-4-4 的日期差数

(strtotime("2009-4-4")-strtotime("2009-3-1"))/3600*24

11. 不使用第三个变量交换两个变量的值

  1. <?php
  2. /*
  3. 不使用第三个变量交换两个变量的值
  4. */
  5. // 方法一==
  6. $a = “PHP”;
  7. b = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; &quot; M y S Q L &quot; &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 8 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; e c h o &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ b = &lt;span class=&quot;hljs-string&quot;&gt;&quot;MySQL&quot;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;8&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&#x27; b=<spanclass="hljsstring">"MySQL"</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="8"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">echo</span><spanclass="hljsstring">a=’. KaTeX parse error: Expected 'EOF', got '&' at position 30: …"hljs-string">'&̲lt;br /&gt;'</s…b=’. KaTeX parse error: Expected 'EOF', got '&' at position 30: …"hljs-string">'&̲lt;br /&gt;'</s…a, b ) = &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; a r r a y &lt; / s p a n &gt; ( b) = &lt;span class=&quot;hljs-keyword&quot;&gt;array&lt;/span&gt;( b)=<spanclass="hljskeyword">array</span>(b, a ) ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 12 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 13 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; e c h o &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ a);&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;12&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;13&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&#x27; a);</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="12"></div></div><divclass="hljslncode"><divclass="hljslnline"></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="13"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">echo</span><spanclass="hljsstring">a=’. KaTeX parse error: Expected 'EOF', got '&' at position 30: …"hljs-string">'&̲lt;br /&gt;'</s…b=’.$b. ’<br />’;
  8. echo “<hr />”;
  9. // 方法二==
  10. $a = “PHP”;
  11. b = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; &quot; M y S Q L &quot; &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 20 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; e c h o &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ b = &lt;span class=&quot;hljs-string&quot;&gt;&quot;MySQL&quot;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;20&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&#x27; b=<spanclass="hljsstring">"MySQL"</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="20"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">echo</span><spanclass="hljsstring">a=’. KaTeX parse error: Expected 'EOF', got '&' at position 30: …"hljs-string">'&̲lt;br /&gt;'</s…b=’.$b. ’<br />’;
  12. $a = KaTeX parse error: Expected 'EOF', got '&' at position 30: …"hljs-string">'&̲amp;'</span>.b; //使用&连接两个字符串
  13. // 根据&进行字符串分割
  14. $b = explode( ’&’, $a);
  15. $a = $b[ 1];
  16. $b = b [ &lt; s p a n c l a s s = &quot; h l j s − n u m b e r &quot; &gt; 0 &lt; / s p a n &gt; ] ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 29 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 30 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; e c h o &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ b[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;];&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;29&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;30&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&#x27; b[<spanclass="hljsnumber">0</span>];</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="29"></div></div><divclass="hljslncode"><divclass="hljslnline"></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="30"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">echo</span><spanclass="hljsstring">a=’. KaTeX parse error: Expected 'EOF', got '&' at position 30: …"hljs-string">'&̲lt;br /&gt;'</s…b=’.$b. ’<br />’;
  17. ?>

  1. 请说明 php 中传值与传引用的区别。什么时候传值什么时候传引用?
    变量默认总是传值赋值。
    那也就是说,当将一个表达式的值赋予一个变量时,整个原始表达式的值被赋值到目标变量。这意味着,例如,当一个变量的值赋予另外一个变量时,改变其中一个变量的值,将不会影响到另外一个变量。
    PHP 也提供了另外一种方式给变量赋值:引用赋值。
    这意味着新的变量简单的引用(换言之,“成为其别名” 或者 “指向”)了原始变量。改动新的变量将影响到原始变量,反之亦然。使用引用赋值,简单地将一个&符号加到将要赋值的变量前(源变量)。
    对象默认是传引用。
    对于较大的数据,传引用比较好,这样可以节省内存的开销。

相关题目 1:What would the following code print to the browser? Why?

  1. <?php
  2. n u m = &lt; s p a n c l a s s = &quot; h l j s − n u m b e r &quot; &gt; 10 &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 3 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − f u n c t i o n &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; f u n c t i o n &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − t i t l e &quot; &gt; m u l t i p y &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − p a r a m s &quot; &gt; ( num = &lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;3&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;multipy&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;( num=<spanclass="hljsnumber">10</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="3"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljsfunction"><spanclass="hljskeyword">function</span><spanclass="hljstitle">multipy</span><spanclass="hljsparams">(num){
  3. $num = KaTeX parse error: Expected 'EOF', got '}' at position 217: …s-ln-line"> }̲</div></div></l…num);
  4. echo $num;
  5. ?>

输出 10

相关题目 2:What is the difference between a reference and a regular variable? How do you pass by reference & why would you want to?(Yahoo)

reference 传送的是变量的地址而非它的值,所以在函数中改变一个变量的值时,整个应用都见到这个变量的新值。
一个 regular variable 传送给函数的是它的值,当函数改变这个变量的值时,只有这个函数才见到新值,应用的其他部分仍然见到旧值。

13. 将 1234567890 转换成 1,234,567,890 每 3 位用逗号隔开的形式。(百度)

  1. <?php
  2. s t r = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ 123456789 0 ′ &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 3 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − f u n c t i o n &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; f u n c t i o n &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − t i t l e &quot; &gt; s t r &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − p a r a m s &quot; &gt; ( str =&lt;span class=&quot;hljs-string&quot;&gt;&#x27;1234567890&#x27;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;3&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;( str=<spanclass="hljsstring">1234567890</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="3"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljsfunction"><spanclass="hljskeyword">function</span><spanclass="hljstitle">str</span><spanclass="hljsparams">(str)
  3. {
  4. // 反转字符串,得到0987654321
  5. s t r = s t r r e v ( str = strrev( str=strrev(str);
  6. // 使用逗号分割字符串,得到098,765,432,1,
  7. s t r = c h u n k s p l i t ( str = chunk_split( str=chunksplit(str, 3, ’,’);
  8. // 再次反转字符串,得到,1,234,567,890
  9. s t r = s t r r e v ( str = strrev( str=strrev(str);
  10. // 去掉左边的",",得到1,234,567,890
  11. s t r = l t r i m ( str = ltrim( str=ltrim(str, ’,’);
  12. return KaTeX parse error: Expected 'EOF', got '}' at position 181: …s-ln-line"> }̲</div></div></l…str);
  13. ?>

相关题目 1:如何实现字符串翻转?

strrev(),不过这种方法都不能解决中文字符串翻转的问题,会出错的。

  1. <?php
  2. header( “Content-Type:text/html;charset=utf-8”);
  3. /**
  4. * 反转utf8编码的中文字符串
  5. * @param string s t r &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 7 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − c o m m e n t &quot; &gt; ∗ &lt; s p a n c l a s s = &quot; h l j s − d o c t a g &quot; &gt; @ r e t u r n &lt; / s p a n &gt; s t r i n g &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 8 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − c o m m e n t &quot; &gt; ∗ / &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 9 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − f u n c t i o n &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; f u n c t i o n &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − t i t l e &quot; &gt; s t r r e v u t f 8 &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − p a r a m s &quot; &gt; ( str&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;7&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt; * &lt;span class=&quot;hljs-doctag&quot;&gt;@return&lt;/span&gt; string&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;8&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt; */&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;9&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;strrev_utf8&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;( str</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="7"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljscomment"><spanclass="hljsdoctag">@return</span>string</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="8"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljscomment">/</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="9"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljsfunction"><spanclass="hljskeyword">function</span><spanclass="hljstitle">strrevutf8</span><spanclass="hljsparams">(str)
  6. {
  7. return join( "",array_reverse(preg_split( "//u", $str)));
  8. }
  9. // 实例
  10. s t r = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; &quot; 悄 悄 是 别 离 的 笙 箫 &quot; &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 16 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; e c h o &lt; / s p a n &gt; s t r r e v u t f 8 ( str = &lt;span class=&quot;hljs-string&quot;&gt;&quot;悄悄是别离的笙箫&quot;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;16&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; strrev_utf8( str=<spanclass="hljsstring">""</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="16"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">echo</span>strrevutf8(str);
  11. ?>

相关题目 2:假设现在有一个字符串 www.baidu.com 如何使用 PHP 对它进行操作使字符串以 moc.udiab.输出? (亿邮)

  1. <?php
  2. s t r = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; &quot; w w w . b a i d u . c o m &quot; &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 3 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; e c h o &lt; / s p a n &gt; s t r r e v ( s t r r e p l a c e ( &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ w w w ′ &lt; / s p a n &gt; , &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ ′ &lt; / s p a n &gt; , str = &lt;span class=&quot;hljs-string&quot;&gt;&quot;www.baidu.com&quot;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;3&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;echo&lt;/span&gt; strrev(str_replace(&lt;span class=&quot;hljs-string&quot;&gt;&#x27;www&#x27;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#x27;&#x27;&lt;/span&gt;, str=<spanclass="hljsstring">"www.baidu.com"</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="3"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">echo</span>strrev(strreplace(<spanclass="hljsstring">www</span>,<spanclass="hljsstring"></span>,str));
  3. ?>

14. 用 PHP 写出显示客户端 IP 与服务器 IP 的代码。

客户端 IP:$_SERVER["REMOTE_ADDR"]
服务器端 IP:$_SERVER["SERVER_ADDR"]

15. 简述如何得到当前执行脚本路径,包括所得到参数。

获取当前执行脚本路径使用$_SERVER["SCRIPT_FILENAME"]__FILE__
获取参数,使用$_SERVER["QUERY_STRING"]

16. What is the difference between foo() & @foo()?(Yahoo)

foo() 会执行这个函式,任何解译错误、语法错误、执行错误都会在页面上显示出来。
@foo() 在执行这个函式时,会隐藏所有上述的错误讯息。
很多应用程序都使用 @mysql_connect() 和 @mysql_query 来隐藏 mysql 的错误信息,这是不对的,因为错误不该被隐藏,你必须妥善处理它们,可能的话解决它们。

17. 下面哪个选项没有将 john 添加到 users 数组中? ( 百度 )

A. $users[ ] = "john";
B. array_add($users, "john");
C. array_push($users, "john");
D. $users ||= "john" ;
答案:BD

18. 检测一个变量是否有设置的函数?是否为空的函数是?

isset 检测一个变量是否设置
empty 检测是否为空
注意二者的区别,如果 变量 是非空或非零的值,则 empty() 返回 FALSE。换句话说,""、0、"0"、NULL、FALSE、array() 以及没有任何属性的对象都将被认为是空的。
isset是检测变量是否设置/定义,empty是检测已定义的变量的值是否为空。

19. 在 PHP 中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量______中;而链接到当前页面的的前一页面 URL 记录在预定义变量______中。

当前脚本名称:$_SERVER["PHP_SELF"]或者 $_SERVER["SCRIPT_NAME"];
链接到当前页面的前一页面的 URL 地址:$_SERVER["HTTP_REFERER"]。

20. sort()、assort()、和 ksort() 有什么分别?它们分别在什么情况下使用?

sort(),根据数组中元素的值,以英文字母顺序排序,索引键会由 0 到 n-1 重新编号。主要是当数组索引键的值无关紧要时用来把数组排序。
assort(),PHP 没有 assort() 函式,所以可能是 asort() 的笔误。
asort(),对数组进行排序,数组的索引保持和单元的关联。主要用于对那些单元顺序很重要的结合数组进行排序。
ksort(),根据数组中索引键的值,以英文字母顺序排序,特别适合用于希望把索引键排序的关联数组。

21. 在 PHP 中 error_reporting 这个函数有什么作用?

打开或者关闭错误报告,如:
error_reporting(0);
error_reporting(E_ALL & ~ E_NOTICE);
error_reporting(E_ALL);

相关题目:error_reporting(2047) 什么作用?(新浪)

2047 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024
其中:
1 对应 E_ERROR,2 对应 E_WARNING,4 对应 E_PARSE,
8 对应 E_NOTICE,16 对应 E_CORE_ERROR,32 对应 E_CORE_WARNING
,64 对应 E_COMPILE_ERROR,128对应 E_COMPILE_WARNING,256 对应 E_USER_ERROR,
512 对应 E_USER_WARNING,1024 对应 E_USER_NOTICE。
error_reporting(2047)意味着上述错误都会显示出来。

  1. 写出以下程序的输出结果 (CBSI)

  1. <?php
  2. s t r = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ c d ′ &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 3 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; str = &lt;span class=&quot;hljs-string&quot;&gt;&#x27;cd&#x27;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;3&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; str=<spanclass="hljsstring">cd</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="3"></div></div><divclass="hljslncode"><divclass="hljslnline"> s t r = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ h o t d o g ′ &lt; / s p a n &gt; ; &lt; s p a n c l a s s = &quot; h l j s − r e g e x p &quot; &gt; / / &lt; / s p a n &gt; str = &lt;span class=&quot;hljs-string&quot;&gt;&#x27;hotdog&#x27;&lt;/span&gt;;&lt;span class=&quot;hljs-regexp&quot;&gt;//&lt;/span&gt; str=<spanclass="hljsstring">hotdog</span>;<spanclass="hljsregexp">//</span>cd = ‘hotdog’;
  3. $ s t r . = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ o k ′ &lt; / s p a n &gt; ; &lt; s p a n c l a s s = &quot; h l j s − r e g e x p &quot; &gt; / / &lt; / s p a n &gt; str .= &lt;span class=&quot;hljs-string&quot;&gt;&#x27;ok&#x27;&lt;/span&gt;;&lt;span class=&quot;hljs-regexp&quot;&gt;//&lt;/span&gt; str.=<spanclass="hljsstring">ok</span>;<spanclass="hljsregexp">//</span>cd .= ‘ok’;
  4. echo $cd;
  5. ?>

<p>hotdogok</p>
</li>

相关题目:什么是可变变量?

获取一个普通变量的值作为这个可变变量的变量名。

23. 常量如何定义? 如何检测一个常量是否被定义?

定义常量:define()
检测常量是否定义:defined()
如:

  1. define( “TEST”, “happy new year!”);
  2. if( defined( “TEST”)){
  3. echo TEST;
  4. }

  1. 执行程序段<?php echo 8%(-2) ?>将输出
    %为取模运算,输出 0
    $a % $b其结果的正负取决于 $a 的符号。

echo ((-8)%3)."<br />";//输出 -2
echo (8%(-3))."<br />";//输出 2

25. 数组函数 arsort 的作用是_;语句 error_reporting(2047)的作用是_。

arsort:对数组进行逆向排序并保持索引关系
error_reporting(2047)的作用 report All errors and warnings,见 21 题中相关题目。

26. 以 Apache 模块的方式安装 PHP,在文件 http.conf 中首先要用语句_动态装载 PHP模块,然后再用语句_使得 Apache 把所有扩展名为 php 的文件都作为 PHP 脚本处理。

LoadModule php5_module "c:/php/php5apache2.dll";
AddType application/x-httpd-php .php

27. 一个函数的参数不能是对变量的引用,除非在 php.ini 中把____设为 on.

allow_call_time_pass_reference :是否启用在函数调用时强制参数被按照引用传递,默认关闭。

28. 在 PHP 中,heredoc 是一种特殊的字符串,它的结束标志必须____顶格写,并且不能包含任何其它字符除";"

29.echo count("abc"); 输出什么?(新浪?)

输出 1
count :计算数组中的单元数目或对象中的属性个数,通常是一个 array,任何其它类型都只有一个单元。
对于对象,如果安装了 SPL,可以通过实现 Countable 接口来调用 count()。该接口只有一个方法 count(),此方法返回 count() 函数的返回值。
如果 var 不是数组类型或者实现了 Countable 接口的对象,将返回 1,有一个例外,如果 var 是 NULL 则结果是 0。

相关题目:What will be the output of the following PHP code:(腾讯)


 
 
  1. <?php
  2. echo count(strlen( "http://php.net"));
  3. ?>

answer: 1

30. 写个函数用来对二维数组排序(新浪)

  1. <?php
  2. /**
  3. * 根据某列对二维数组进行排序
  4. * @param $arr array 要排序的二维数组
  5. * @param $row string 排序依据的某列
  6. * @param t y p e s t r i n g a s c 表 示 正 序 , 为 默 认 值 ; d e s c 表 示 逆 序 &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 7 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − c o m m e n t &quot; &gt; ∗ &lt; s p a n c l a s s = &quot; h l j s − d o c t a g &quot; &gt; @ p a r a m &lt; / s p a n &gt; a r r a y 返 回 排 序 后 的 二 维 数 组 &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 8 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − c o m m e n t &quot; &gt; ∗ / &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 9 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − f u n c t i o n &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; f u n c t i o n &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − t i t l e &quot; &gt; a r r a y s o r t &lt; / s p a n &gt; &lt; s p a n c l a s s = &quot; h l j s − p a r a m s &quot; &gt; ( type string asc表示正序,为默认值;desc表示逆序&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;7&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt; * &lt;span class=&quot;hljs-doctag&quot;&gt;@param&lt;/span&gt; array 返回排序后的二维数组&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;8&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt; */&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;9&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;array_sort&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;( typestringascdesc</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="7"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljscomment"><spanclass="hljsdoctag">@param</span>array</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="8"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljscomment">/</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="9"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljsfunction"><spanclass="hljskeyword">function</span><spanclass="hljstitle">arraysort</span><spanclass="hljsparams">(arr, r o w , row, row,type =‘asc’)
  7. {
  8. a r r t e m p = &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; a r r a y &lt; / s p a n &gt; ( ) ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 12 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − c o m m e n t &quot; &gt; / / 将 排 序 依 据 作 为 数 组 的 键 名 &lt; / s p a n &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 13 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; f o r e a c h &lt; / s p a n &gt; ( arr_temp = &lt;span class=&quot;hljs-keyword&quot;&gt;array&lt;/span&gt;();&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;12&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-comment&quot;&gt;// 将排序依据作为数组的键名&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;13&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;foreach&lt;/span&gt; ( arrtemp=<spanclass="hljskeyword">array</span>();</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="12"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljscomment">//</span></div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="13"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">foreach</span>(arr as $v) {
  9. a r r t e m p [ arr_temp[ arrtemp[v[$row]] = KaTeX parse error: Expected 'EOF', got '}' at position 183: …-line"> }̲</div></div></l…arr_temp);
  10. // 按照键名对二维数组进行排序,并保持索引关系
  11. if ( KaTeX parse error: Expected '}', got 'EOF' at end of input: … ksort(arr_temp);
  12. } elseif( KaTeX parse error: Expected '}', got 'EOF' at end of input: … krsort(arr_temp);
  13. }
  14. // 返回排序结果
  15. return $arr_temp;
  16. }
  17. // 实例
  18. KaTeX parse error: Expected 'EOF', got '&' at position 308: …g">'id'</span>=&̲gt;<span class=…person);
  19. echo “</pre>”;
  20. echo “<hr />”;
  21. p e r s o n 2 = a r r a y s o r t ( person2 = array_sort( person2=arraysort(person, ‘name’);
  22. echo “<pre>after:<br />”;
  23. print_r($person2);
  24. echo “</pre>”;
  25. ?>

31. 写 5 个不同的自己的函数,来获取一个全路径的文件的扩展名,允许封装 php 库中已有的函数。(新浪)

  1. <?php
  2. /*
  3. 写 5 个不同的自己的函数,来获取一个全路径的文件的扩展名,允许封装 php 库中已有的函数。(新浪)
  4. */
  5. // 方法一
  6. function ext_name1($path){
  7. p a t h i n f o = s t r r c h r ( path_info = strrchr( pathinfo=strrchr(path, ‘.’); //.php
  8. return ltrim( KaTeX parse error: Expected 'EOF', got '}' at position 224: …s-ln-line"> }̲</div></div></l…path){
  9. p a t h i n f o = s u b s t r ( path_info = substr( pathinfo=substr(path,strrpos( p a t h , &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ . ′ &lt; / s p a n &gt; ) ) ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 14 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; r e t u r n &lt; / s p a n &gt; l t r i m ( path, &lt;span class=&quot;hljs-string&quot;&gt;&#x27;.&#x27;&lt;/span&gt;));&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;14&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; ltrim( path,<spanclass="hljsstring">.</span>));</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="14"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">return</span>ltrim(path_info, ’.’);
  10. }
  11. // 方法三
  12. function ext_name3($path){
  13. p a t h i n f o = p a t h i n f o ( path_info = pathinfo( pathinfo=pathinfo(path);
  14. return KaTeX parse error: Expected 'EOF', got '}' at position 233: …s-ln-line"> }̲</div></div></l…path){
  15. $arr = explode( ’.’, $path);
  16. return a r r [ c o u n t ( arr[count( arr[count(arr) -1];
  17. }
  18. // 方法五
  19. function ext_name5($path){
  20. KaTeX parse error: Double superscript at position 41: …js-string">'/^[^̲\.]+\.([\w]+)/’;
  21. return preg_replace( p a t t e r n , &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ pattern, &lt;span class=&quot;hljs-string&quot;&gt;&#x27; pattern,<spanclass="hljsstring">{1}’, basename($path));
  22. }
  23. // 实例
  24. KaTeX parse error: Expected group after '_' at position 124: …"hljs-keyword">_̲_FILE__</span>)…path<br />";
  25. echo ext_name1( KaTeX parse error: Expected 'EOF', got '&' at position 73: …"hljs-string">"&̲lt;br /&gt;"</s…path); echo “<br />”;
  26. echo ext_name3( KaTeX parse error: Expected 'EOF', got '&' at position 73: …"hljs-string">"&̲lt;br /&gt;"</s…path); echo “<br />”;
  27. echo ext_name5($path); echo “<br />”;
  28. ?>

32. PHP 的意思,它能干些什么?

PHP( Hypertext Preprocessor,超文本预处理器的字母缩写)是一种被广泛应用的开放源代码的多用途脚本语言,它可嵌入到 HTML 中,尤其适合 web 开发。
PHP 主要是用于服务端的脚本程序,因此可以用 PHP 来完成任何其它的 CGI 程序能够完成的工作,例如收集表单数据,生成动态网页,或者发送/接收Cookies。但 PHP 的功能远不局限于此。
PHP 脚本主要用于以下三个领域:

  • 服务端脚本。这是 PHP 最传统,也是最主要的目标领域。
  • 命令行脚本。可以编写一段 PHP 脚本,并且不需要任何服务器或者浏览器来运行它。通过这种方式,仅仅只需要 PHP 解析器来执行。
  • 编写桌面应用程序。

33. Name a few ways to output (print) a block of HTML code in PHP?(Yahoo)

你可以使用 PHP 中任何一种输出语句,包括 echo、print、printf,大部分人都使用如下例的 echo:
echo "My string $variable";
你也可以使用这种方法:

  1. echo <<< END
  2. This text is written to the screen as output and this $variable is parsed too. If you wanted you
  3. can have <span> HTML tags in here as well.</span> The END; remarks must be on a line of itsown, and can ’t contain any extra white space.
  4. END;

34. 写出以下程序的输出结果 (CBSI)

  1. <?php
  2. $b = 201;
  3. $c = 40;
  4. $a = $b > $c ? 4 : 5;
  5. echo $a;
  6. ?>

输出结果为 4

  1. $arr = array('james', 'tom', 'symfony');请打印出第一个元素的值,并请将数组的值用','号分隔并合并成字串输出。
    打印第一个元素:echo $arr[0];
    以’,’合并成字符串:echo implode(',',$arr);

36. $a = 'abcdef'; 请取出$a 的值并打印出第一个字母

echo $a{0} 或 echo $a[0]

相关题目:$string="abcdefg",那么$string{4}的值是? (卓望)

值是 e

37. What does === do? What's an example of something that will give true for '==', but not'==='? (Yahoo)

=== 表示全等,是指的两个变量的值和类型都相等。
如 if (strpos('abc','a') == false) 和 if (strpos('abc','a') === false)

38. Which of the following snippets prints a representation of 42 with two decimal places?(腾讯)

A. printf("%.2d\n", 42);
B. printf("%1.2f\n", 42);
C. printf("%1.2u\n", 42);
answer:B

39. Given $text = 'Content-TypeType:text/xml'; Which of the following prints 'text/xml'? (腾讯)

A. print substr($text, strchr($text, ':'));
B. print substr($text, strchr($text, ':') + 1);
C. print substr($text, strpos($text, ':') + 1);
D. print substr($text, strpos($text, ':') + 2);
E. print substr($text, 0, strchr($text, ':'));
answer:C
分析:

  1. <?php
  2. t e x t = &lt; s p a n c l a s s = &quot; h l j s − s t r i n g &quot; &gt; ′ C o n t e n t − T y p e : t e x t / x m l ′ &lt; / s p a n &gt; ; &lt; / d i v &gt; &lt; / d i v &gt; &lt; / l i &gt; &lt; l i &gt; &lt; d i v c l a s s = &quot; h l j s − l n − n u m b e r s &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e h l j s − l n − n &quot; d a t a − l i n e − n u m b e r = &quot; 3 &quot; &gt; &lt; / d i v &gt; &lt; / d i v &gt; &lt; d i v c l a s s = &quot; h l j s − l n − c o d e &quot; &gt; &lt; d i v c l a s s = &quot; h l j s − l n − l i n e &quot; &gt; &lt; s p a n c l a s s = &quot; h l j s − k e y w o r d &quot; &gt; p r i n t &lt; / s p a n &gt; s u b s t r ( text = &lt;span class=&quot;hljs-string&quot;&gt;&#x27;Content-Type:text/xml&#x27;&lt;/span&gt;;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div class=&quot;hljs-ln-numbers&quot;&gt;&lt;div class=&quot;hljs-ln-line hljs-ln-n&quot; data-line-number=&quot;3&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;hljs-ln-code&quot;&gt;&lt;div class=&quot;hljs-ln-line&quot;&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;print&lt;/span&gt; substr( text=<spanclass="hljsstring">ContentType:text/xml</span>;</div></div></li><li><divclass="hljslnnumbers"><divclass="hljslnlinehljslnn"datalinenumber="3"></div></div><divclass="hljslncode"><divclass="hljslnline"><spanclass="hljskeyword">print</span>substr(text, strchr( KaTeX parse error: Expected 'EOF', got '&' at position 338: …"hljs-string">"&̲lt;br /&gt;"</s…text, strchr( KaTeX parse error: Expected 'EOF', got '&' at position 399: …"hljs-string">"&̲lt;br /&gt;"</s…text, strpos( KaTeX parse error: Expected 'EOF', got '&' at position 367: …"hljs-string">"&̲lt;br /&gt;"</s…text, strpos( KaTeX parse error: Expected 'EOF', got '&' at position 367: …"hljs-string">"&̲lt;br /&gt;"</s…text, 0,strchr($text, ’:’)); //出错,strchr返回的是字符串
  3. echo “<br />”;
  4. ?>

40. What is the value of $a?

  1. <?php
  2. $a = in_array( ‘01’, array( ‘1’)) == var_dump( ‘01’ == 1);
  3. ?>

A. True
B. False
answer:A

41. What is the value of $result in the following PHP code? (腾讯)

  1. <?php
  2. function timesTwo($int)
  3. {
  4. $int = $int * 2;
  5. }
  6. $int = 2;
  7. r e s u l t = t i m e s T w o ( result = timesTwo( result=timesTwo(int);
  8. ?>

answer: NULL

42. 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()
answer:C

43. 运行以下程序,$a 的值是多少?

  1. <?php
  2. /*
  3. 运行以下程序,$a 的值是多少?
  4. */
  5. $a = “hello”;
  6. KaTeX parse error: Expected 'EOF', got '&' at position 5: b = &̲amp;a;
  7. unset($b);
  8. $b = “world”;
  9. echo a , a, a,b;
  10. ?>

answer:hello

44. 运行以下程序,$b 的值是多少?

  1. <?php
  2. /*
  3. 运行以下程序,$b的值是多少?
  4. */
  5. $a = 1;
  6. $b = $a++;
  7. echo a , a, a,b;
  8. ?>

answer:1

45. 运行以下程序,$x 的值是多少?


 
 
  1. <?php
  2. /*
  3. 运行以下程序,$x 的值是多少?
  4. */
  5. $array = array();
  6. $x = empty($array);
  7. echo $x ? "true": "false";
  8. ?>

answer:ture

46. 将字符 09 转换成十进制数字。(百度)

使用 intval 函数,echo intval("09"),或者使用 int 强制类型转换。

47. 请 写 一 个 函 数 , 实 现 以 下 功 能 : 字 符 串 "open_door" 转 换 成 "OpenDoor" 、"make_by_id" 转换成 "MakeById"。


 
 
  1. <?php
  2. /**
  3. * 字符串转换,如open_door->OpenDoor,make_by_id->MakeById
  4. * @param $str string 要转换的字符串
  5. * @return string 转换后的字符串
  6. */
  7. function change_str($str){
  8. $arr = explode( '_',$str); //将以“_”间隔的字符串拆分成数组的单元
  9. $arr = array_map( 'ucfirst', $arr); //每个数组单元的首个字符大写
  10. return implode( '', $arr); //将数组单元合并输出字符串
  11. }
  12. // 实例
  13. $str1 = 'open_door';
  14. $str2 = 'make_by_id';
  15. echo change_str($str1); //OpenDoor
  16. echo change_str($str2); //MakeById
  17. ?>

48. 要求写一段程序,实现以下数组$arr1 转换成数组$arr2:


 
 
  1. <?php
  2. $arr1 = array(
  3. '0' => array( 'fid' => 1, 'tid' => 1, 'name' => 'Name1'),
  4. '1' => array( 'fid' => 1, 'tid' => 2, 'name' => 'Name2'),
  5. '2' => array( 'fid' => 1, 'tid' => 5, 'name' => 'Name3'),
  6. '3' => array( 'fid' => 1, 'tid' => 7, 'name' => 'Name4'),
  7. '4' => array( 'fid' => 3, 'tid' => 9, 'name' => 'Name5')
  8. );
  9. // =======================================================
  10. $arr_tmp = array();
  11. $arr2 = array();
  12. foreach ($arr1 as $v) {
  13. // 方法一
  14. $arr_tmp[$v[ 'fid']][] = array_slice($v, 1);
  15. // 方法二
  16. /*
  17. $arr_tmp[$v['fid']][] = array(
  18. 'tid' => $v['tid'],
  19. 'name' => $v['name']
  20. )
  21. */
  22. }
  23. foreach ($arr_tmp as $v) {
  24. $arr2[] = $v;
  25. }
  26. print_r($arr1);
  27. print_r($arr2);
  28. ?>

49. 如何将一个数组元素的排列顺序反转过来(例如以下代码反转以后的顺序是: array ('d','c', 'b', 'a'))? (选择 2 个答案)


 
 
  1. <?php
  2. $array = array ( 'a', 'b', 'c', 'd');
  3. ?>

A. array_flip()
B. array_reverse()
C. sort()
D. rsort()
答案:BD

50. $val = max('string', array(2, 5, 7), 42);$val 值为

array(2,5,7)
max 用法示例如下:


 
 
  1. <?php
  2. echo max( 1, 3, 5, 6, 7); //7
  3. echo "<br />";
  4. echo max( array( 2, 4, 5)); //5
  5. echo "<br />";
  6. echo max( 0, 'hello'); //0
  7. echo "<br />";
  8. echo max( 'hello', '0'); //hello
  9. echo "<br />";
  10. echo max( -1, 'hello'); //hello
  11. echo "<br />";
  12. //对于多个数组,max从左向右比较
  13. //因此在本例中:2 == 2,但是4 < 5
  14. $val = max( array( 2, 4, 8), array( 2, 5, 7)); //array(2,5,7)
  15. var_dump($val);
  16. echo "<br />";
  17. // 如果同时给出数组和非数组作为参数,则总是将数组视为最大值返回
  18. $val = max( 'string', array( 2, 5, 7), 42);
  19. var_dump($val);
  20. ?>

51. 定义常量 MYPI=3.14_________________________;

define("MYPI",3.14);

52. 简述单引号和双引号的用法

双引号串中的内容可以被解释而且替换,而单引号串中的内容总被认为是普通字符。

53. Switch 完整语法和注意事项

使用 Switch 语句可以避免冗长的 if..elseif..else 代码块,case只能处理整数,或者能像整数一样运算的类型,比如char,使用时case 后用'',注意 break不能少,default是为了处理一些之前没有包含到的情况,这样更为安全。

54. 用 PHP 编写代码在页面输出当前的北京时间,格式为 “2007-01-18 09:22:03”

date_default_timezone_set('PRC');
echo date("Y-m-d H:i:s",time());

55. 简述 GBK、GB2312、BIG5 、GB18030

GB2312 支持的汉字较少,GBK 是相比 GB2312 汉字更为丰富,包括全部中日韩汉字,GB18030 相比 GBK 增加了一些少数名族汉字汉字库更为多样,但是常人很难用到,一般简体中文使用 GBK 而繁体中文使用 BIG5。

56. 计算某段字符串中某个字符出现的次数(例如 : gdfgfdgd59gmkblg 中 g 的次数)


 
 
  1. $text = 'gdfgfdgd59gmkblg';
  2. echo substr_count ( $text, 'g');

57. 以下语句可能存在错误,如果存在错误请指出什么错误(每个语句单独考虑)

print_r $val = 333;// error,print_r 是一个函数,需加上括号,如果是 print 则可以
print_r($val = 333); // 正确
$a += ($b = 4) + 5;// notice,$a 未定义,但可以正确运行
$foo[bar] = 'enemy';// notice,最好 bar 加上引号
function x($a = "1", $b){}// 无,但给参数默认值应尽量从右向左

58. 写出如下程序的输出结果(小米)


 
 
  1. <?php
  2. header( "Content-type:text/html;charset=utf-8");
  3. $str1 = null;
  4. $str2 = false;
  5. echo $str1 == $str2 ? '相等' : '不相等'; //相等
  6. $str3 = '';
  7. $str4 = 0;
  8. echo $str3 == $str4 ? '相等' : '不相等'; //相等
  9. $str5 = 0;
  10. $str6 = '0';
  11. echo $str1 === $str2 ? '相等' : '不相等'; //相等
  12. ?>

59. 写出如下程序的输出结果


 
 
  1. <?php
  2. $a1 = null;
  3. $a2 = false;
  4. $a3 = 0;
  5. $a4 = '';
  6. $a5 = '0';
  7. $a6 = 'null';
  8. $a7 = array();
  9. $a8 = array( array());
  10. echo empty($a1) ? 'true' : 'false'; //ture
  11. echo '<br />';
  12. echo empty($a2) ? 'true' : 'false'; //ture
  13. echo '<br />';
  14. echo empty($a3) ? 'true' : 'false'; //ture
  15. echo '<br />';
  16. echo empty($a4) ? 'true' : 'false'; //ture
  17. echo '<br />';
  18. echo empty($a5) ? 'true' : 'false'; //ture
  19. echo '<br />';
  20. echo empty($a6) ? 'true' : 'false'; //false
  21. echo '<br />';
  22. echo empty($a7) ? 'true' : 'false'; //ture
  23. echo '<br />';
  24. echo empty($a8) ? 'true' : 'false'; //false
  25. ?>

60. 写出如下程序的输出结果


 
 
  1. <?php
  2. $test = 'aaaa';
  3. $abc = &$test;
  4. unset($test);
  5. echo $abc;
  6. ?>

aaaa

61. 写出如下程序的输出结果


 
 
  1. <?php
  2. $count = 5;
  3. function get_count()
  4. {
  5. static $count = 0;
  6. return $count++;
  7. }
  8. echo $count; //5
  9. ++$count; //6
  10. echo get_count(); //0
  11. echo get_count(); //1
  12. ?>

501

分析:
在 PHP 中,作用域是不重叠的,函数之外的是全局变量,函数内部定义的则是局部变量,二者是两个不同的变量,除非在函数内使用 global 显式声明使用全局变量或直接用$_GLOBALS 来引用。

62. 写出如下程序的输出结果


 
 
  1. <?php
  2. $GLOBALS[ 'var1'] = 5;
  3. $var2 = 1;
  4. function get_value()
  5. {
  6. global $var2;
  7. $var1 = 0;
  8. return $var2++;
  9. }
  10. get_value();
  11. echo $var1; //5
  12. echo $var2; //2
  13. ?>

52

63. 写出如下程序的输出结果


 
 
  1. <?php
  2. function get_arr()
  3. {
  4. unset($arr[ 0]);
  5. }
  6. $arr1 = array( 1, 2);
  7. $arr2 = array( 1, 2);
  8. get_arr(&$arr1);
  9. get_arr($arr2);
  10. echo count($arr1); //1
  11. echo count($arr2); //2
  12. ?>

12

64. $arr = array('james', 'tom', 'symfony'); 请将’jack’添加到$arr 数组的开头,并把 jack 打印出来。


 
 
  1. array_unshift($arr,’jack’);
  2. echo $arr[ 0];

65. $arr = array('james', 'tom', 'symfony');请将$arr 数组的值用 ’,’分割并合并成字符串输出?

echo implode(‘,’,$arr);

66. $str = ‘jack,james,tom,symfony’; 请将$str 用’,’分割,并把分割后的值放到$arr 数组中?

$arr = explode(‘,’,$str);

67. $arr = array(3,7,2,1,’d’,’abc’);请将$arr 按照从大到小的顺序排序,并保持其键值不变?


 
 
  1. arsort($arr);
  2. print_r($arr);

68. $mail = “gaofei@163.com”; 请将此邮箱的域(163.com)取出来并打印,看最多能写出几种方法?


 
 
  1. echo strstr($mail, '163');
  2. echo substr($mail, 7);
  3. echo substr($mail, strpos($mail, '@')+ 1);
  4. $arr = explode( "@",$mail);
  5. echo $arr[ 1];

69. PHP 中的注释的写法,要列出三种?

// 注释内容 C 风格的单行注释
# 注释内容 unix 风格的单行注释
/* 注释内容 */ C 风格的多行注释

70. 如何在一个 php 函数中使用具有全局属性的变量,说出两种方式?

global $a 或者 $GLOBALS['a']

71. 若$aa='aa';则 print('aa is 'aa''."
")会输出什么?

aa is 'aa'

72. 如何把$a = ‘123’转化成 123,列出两种方式?


 
 
  1. <?php
  2. $a = '123';
  3. $a = (int)$a; //第一种方式
  4. $a = intval($a); //第二种方式
  5. settype($a, 'int'); //第三种方式
  6. ?>

相关题目:字符串怎么转成整数,有几种方法?怎么实现?

  1. 强制类型转换: (整型)字符串变量名;
  2. 直接转换:settype(字符串变量,整型);
  3. intval(字符串变量);

73. 说说 break 和 continue 可以用在哪些语句里,并且列出二者的区别?

break 和 continue 可以用在 switch 语句中,或是循环结构中
它们的区别主要是体现在循环语句中,break 表示 跳出整个循环,不再执行,循环将终止,而 continue 则表示跳出本次循环,继续下一次的循环,不会终止循环。

74. 函数中的默认参数位置如何放置?

当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧;否则,函数将不会按照预期的情况工作。

75. 如何往一个数组后面添加一个值?列出两种方式?


 
 
  1. $arr[ ] = $value;
  2. array_push($arr, $value);

[!!!]76. 说出数组涉及到的常用函数。

array --声明一个数组
count -- 计算数组中的单元数目或对象中的属性个数
foreach -- 遍历数组
list -- 将数组中元素的值赋值给变量,批量声明变量
explode -- 将字符串转成数组
implode -- 将数组转成一个新字符串
array_merge -- 合并一个或多个数组
is_array -- 检查是否是数组
print_r -- 输出数组
sort -- 数组排序
array_keys -- 返回数组中所有的键名
array_values -- 返回数组中所有的值
key -- 从关联数组中取得键名

[!!!]77. 字符串的常用函数?

trim()-- 去除字符串首尾处的空白字符(或者其他字符)
strlen()-- 字符串长度
substr()-- 截取字符串
str_replace()-- 替换字符串函数
strstr()-- 查找字符串的首次出现位置,返回 第一次出现的位置开始到结尾的字符串
explode()-- 将字符串分割成数组
implode()-- 将数组分割成字符串
str_repeat() -- 重复一个字符串
addslashes() -- 转义字符串
htmlspecialchars() -- HTML 实体转义

78. 以下代码的执行后是,$result 值为:(奇矩互动)


 
 
  1. <?php
  2. $srcArray= array( 'a', 'b', 'c', 'd');
  3. $randValue=array_rand($srcArray);
  4. $result=is_string($randValue);
  5. ?>

A. a
B. false
C. true
D. b
E. c
答案:B
mixed array_rand( array $input [, int $num_req ] )
从数组中随机取出一个或多个单元,它接受 input 作为输入数组和一个可选的参数num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1。
如果你只取出一个,array_rand() 返回一个随机单元的键名,否则就返回一个包含随机键名的数组。
所以使用 array_rand 函数,返回结果要么是键名,要么是数组,在这个例子中,数组是索引数组,所以返回的是整型。

79. 以下代码的执行后是,$result 值为: (奇矩互动)


 
 
  1. <?php
  2. $a= '01';
  3. $result = 1;
  4. if (in_array($a, array( '1'))) {
  5. $result = 2;
  6. } elseif ($a == '1') {
  7. $result = 3;
  8. } elseif ($a == '01') {
  9. $result = 4;
  10. } else{
  11. $result = 5;
  12. }
  13. echo $result;
  14. ?>

A. 1
B. 2
C. 3
D. 4
E. 5
答案:B

80. php 函数名是否区分大小写? (奇矩互动)

A. 不区分
B. 区分
答案:A
函数名是大小写无关的,不过在调用函数的时候,通常使用其在定义时相同的形式。

81.以下代码的执行后是,$result 值为:(奇矩互动)


 
 
  1. <?php
  2. $x = '';
  3. $result = is_null($x);
  4. ?>

A. null
B. true
C. false
D. 1
答案:C

is_null -- 检测变量是否为 NULL,如果变量是 null 则返回 TRUE,否则返回 FALSE。
在下列情况下一个变量被认为是 NULL:

  1. 被赋值为 NULL
  2. 尚未被赋值
  3. unset()

82. 翻转字符串中的单词,字符串仅包含大小写字母和空格,单词间使用空格分隔。如 :输入"This is PHP",输出"PHP is This"(非必要请不要使用 PHP 自带函数) (小米)


 
 
  1. <?php
  2. function reverse($str)
  3. {
  4. $arr = explode( ' ',$str);
  5. $arr = array_reverse($arr);
  6. return implode( ' ',$arr);
  7. }
  8. $str = 'This is PHP';
  9. echo reverse($str);
  10. ?>

83. 请列举出你所知道的全局环境变量 (亿邮)

$_ENV;
$_SERVER;
$_REQUEST;
$_FILES;
$_SESSION;
$_COOKIE;
$_GET;
$_POST;
$GLOBALS;

84. 设有这样一个数组$a=array(array(id=>0),array(id=>1),array(id=>2)......)id=>0-10000,请问你如何使用 PHP 判断 id=>500 是否在这个数组内? (亿邮)

使用 in_array 函数,代码如下:


 
 
  1. <?php
  2. $a = array(
  3. array( 'id' => 0),
  4. array( 'id' => 1),
  5. array( 'id' => 2),
  6. array( 'id' => 3),
  7. array( 'id' => 4),
  8. array( 'id' => 500)
  9. );
  10. $b = array( 'id' => 500);
  11. if (in_array($b,$a)) {
  12. echo "Yes";
  13. } else{
  14. echo "No";
  15. }
  16. ?>

85. 在 PHP 中两个数组怎么连接合并在一起 (亿邮)

使用 array_merge()函数

86. 运行以上代码后$a $b $c 分别是? (卓望)


 
 
  1. <?php
  2. $a = $b = $c = 0;
  3. $a = $b && $c;
  4. ?>

$a 为 false,$b = $c = 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值