常见的PHP面试题

PHP是什么?

PHP原始为Personal Home Page的缩写,已经正式更名为 “PHP: Hypertext Preprocessor”(超文本预处理器)。由Rasmus Lerdorf在1995年创建。

empty,isset,is_null 区别

  1. 返回结果:empty与if()完全相反,is_null与isset完全相反
  2. 内核:isset是语句,is_null是函数,因此isset执行速度远远大于isnull
  3. 调用:因为是函数,is_null可以作为可变函数调用,也可以接受函数返回值作为参数,isset统统不行。
  4. 代替方法:同样因为执行速度,建议使用 “=== NULL” 来代替isnull
  5. 什么时候用哪个呢?我的建议是哪个方便用哪个。

print,print_r,echo区别

  1. 内核:print,echo是PHP本身的一种语言结构,print_r是函数。
  2. 用途:print,echo只能输出字符串,print_r可以打印数组结构。
  3. 调用方法:echo可以带多个参数,其他两个只能带一个参数。

要求:dir/upload.image.jpg,找出 .jpg 或者 jpg

<?php
		$file='dir/upload.image.jpg';

		echo substr(strrchr($file, '.'),1)."\n";
		
		echo substr($file,strrpos($file, '.')+1)."\n";
		
		echo end(explode('.', $file))."\n";

		$info = pathinfo($file);
		echo $info["extension"]."\n";

		echo pathinfo($file, PATHINFO_EXTENSION);
	?>

求两个日期的差数,例如 2007-2-5 ~ 2007-3-6 的日期差数

<?php
		function get_days($date1,$date2)
		{ 
			$time1=strtotime($date1);
			$time2=strtotime($date2);
			return abs($time1-$time2)/86400; 
		} 
		echo get_days('2007-2-5','2007-3-6');  
		$a=mktime(0,0,0,2,5,2007); //hour,minute,second,month,day,year
		$b=mktime(0,0,0,3,6,2007);
		echo date(‘d’,($b-$a));
    ?>

实现不使用第3个变量,交换$a、$b的值,$a、$b的初始值自己定

<?php
		$a=$a+$b;
		$b=$a-$b;
		$a=$a-b; 
     ?>

字符串“open_door” 转换成 “OpenDoor”

<?php
		foreach($arr as $v){   
		$res.=ucfirst($v);   
		}   
		return $res; 
		} 
		echo diversion('open_door'); 
		echo diversion('make_by_id');
	 ?>

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

<?php
		$client=$_SERVER["REMOTE_ADDR"];
		$server=$_SERVER["SERVER_ADDR"];
		echo $client;
		echo $server;
	  ?>

语句include和require的区别是什么?

  1. 在失败的时候:include产生一个warning,而require产生直接产生错误中断.
  2. require在运行前载入,include在运行时载入.

待续……