PHP开发工程师笔试试卷
姓名:***
一、PHP开发部分
1.合并两个数组有几种方式,试比较它们的异同
答:
1)array_merge()
2)’+’
3)array_merge_recursive
array_merge 简单的合并数组
array_merge_recursive 合并两个数组,如果数组中有完全一样的数据,将它们递归合并
array_combine 和 ‘+’ :合并两个数组,前者的值作为新数组的键
2.请写一个函数来检查用户提交的数据是否为整数(不区分数据类型,可以为二进制、八进制、十进制、十六进制数字)
答:其实主要还是is_int和 floor 这个方法
if(!is_numeric($jp_total)||strpos($jp_total,".")!==false){
echo "不是整数";
}else{
echo "是整数";
}
3.PHP的strtolower()和strtoupper()函数在安装非中文系统的服务器下可能会导致将汉字转换为乱码,请写两个替代的函数实现兼容Unicode文字的字符串大小写转换
答:原因是:中文是由多字节组成的,而只有英文系统的单个英文字符只有一个字节,所以该系统把中文的每一个字节都做了strtolower()处理,改变后的中文字节拼接在一起就成了乱码(新生成的编码映射对应的字符可能就不是中文了)
手动解决:用str_split(string string,intstring,intsplit_length = 1)按每个字节切割,像中文能切割成三个字节。对识别到的字节若是英文字母则进行转换。
<?php
function mystrtoupper($a){
$b = str_split($a, 1);
$r = '';
foreach($b as $v){
$v = ord($v);
if($v >= 97 && $v<= 122){
$v -= 32;
}
$r .= chr($v);
}
return $r;
}
$a = 'a中你继续F@#$%^&*(BMDJFDoalsdkfjasl';
echo 'origin string:'.$a."\n";
echo 'result string:';
$r = mystrtoupper($a);
var_dump($r);
4.PHP的is_writeable()函数存在Bug,无法准确判断一个目录/文件是否可写,请写一个函数来判断目录/文件是否绝对可写
答:其中bug存在两个方面,
1)在windowns中,当文件只有只读属性时,is_writeable()函数才返回false,当返回true时,该文件不一定是可写的。
如果是目录,在目录中新建文件并通过打开文件来判断;
如果是文件,可以通过打开文件(fopen),来测试文件是否可写。
2)在Unix中,当php配置文件中开启safe_mode时(safe_mode=on),is_writeable()同样不可用。
读取配置文件是否safe_mode是否开启。
/**
* Tests for file writability
*
* is_writable() returns TRUE on Windows servers when you really can't write to
* the file, based on the read-only attribute. is_writable() is also unreliable
* on Unix servers if safe_mode is on.
*
* @access private
* @return void
*/
if ( ! function_exists('is_really_writable'))
{
function is_really_writable($file)
{
// If we're on a Unix server with safe_mode off we call is_writable
if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
{
return is_writable($file);
}
// For windows servers and safe_mode "on" installations we'll actually
// write a file then read it. Bah...
if (is_dir($file))
{
$file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
{
return FALSE;
}
fclose($fp);
@chmod($file, DIR_WRITE_MODE);
@unlink($file