笔试题汇总(1)

时间:2022-10-31 21:10:24

PHP笔试题,整理自网络


1.取出三个数中最大的一个


function myMax($a,$b,$c){
	$max=($a>=$b)?$a:$b;
	$max=($max>=$c)?$max:$c;
	return $max;
}
echo myMax(4,2,3);

2.用PHP打印出前一天的时间,打印格式是2007年5月10日22:21:21

date_default_timezone_set('Asia/Shanghai');
echo date('Y-m-d H:i:s',strtotime('-1 day'));
3. a.html和b.html在同一个文件夹下,打开a.html五秒钟后,自动跳转到b.html

php实现:

sleep(5);
js实现:

function a2b(){
	window.location="b.html";
}
setTimeout('a2b()',5000);
4.显示客户端和服务器端ip

echo $_SERVER["REMOTE_ADDR"];
echo gethostbyname("www.baidu.com");
5.

$str1 = null;
$str2 = false;
echo $str1==$str2?'yes':'no'; //yes
echo $str1===$str2?'yes':'no';//no
$str3 = '';
$str4 = 0;
echo $str3==$str4?'yes':'no'; //yes
echo $str3===$str4?'yes':'no'; //no
$str5 = 0;
$str6 = '0';
echo $str5==$str6?'yes':'no'; //yes
echo $str5===$str6?'yes':'no'; //no
6.获取服务器时间
<?php 
date_default_timezone_set('Asia/Shanghai');
function get_time($server){ 
$data = "HEAD / HTTP/1.1\r\n"; 
$data .= "Host: $server\r\n"; 
$data .= "Connection: Close\r\n\r\n"; 
$fp = fsockopen($server, 80); 
fputs($fp, $data); 
$resp = ''; 
while ($fp && !feof($fp)) 
$resp .= fread($fp, 1024); 
preg_match('/^Date: (.*)$/mi',$resp,$matches); 
return strtotime($matches[1]); 
} 
echo date('Y-m-d H:i:s',get_time("www.baidu.com")); 
?>
7.求a,b 两文件的相对路径
function getRelative($a , $b){
	$arr_a = explode("/" , $a) ;
	$brr_b = explode("/" , $b) ;
	$i = 1 ;
	while (true) {
		if($arr_a[$i] == $brr_b[$i]) {
			$i ++ ;
			} else {
			break ;
		}
	}
	$c = count($brr_b) ;
	$d = count($arr_a) ;
	$e = ($c>$d)?$c:$d ;
	$str1 = '';
	$str2 = '';
	for ($j = $i ;$j<$e ;$j++) {
	if(isset($arr_a[$j])) {
		if($j<($d-1)){
			$str1 .= $arr_a[$j] . "/" ;
		} else {
			$str1 .= $arr_a[$j] ;
		}
	}
		if(isset($brr_b[$j])) {
			$str2 .= "../" ;
		}
	}
	return $str2 . $str1 ;
}

$a = "www.baidu.com/1/2/a.php" ;
$b = "www.baidu.com/1/3/4/b.php" ;
$relative = getRelative($a,$b) ;
echo $relative;