空间。
13、抽象类和接口的区别
参考:https://blog.csdn.net/sunlylorn/article/details/6124319
14、PHP创建多级目录
function makedir($path)
{
if (is_dir($path)){
echo "目录已存在";
}else {
$res = mkdir($path, 0777, true);
if ($res) {
echo "创建成功";
} else {
echo "创建失败";
}
}
}
15、PHP写一段代码,确保多个进程同时写入一个文件成功
function writeData($filepath, $data)
{
$fp = fopen($filepath, 'a'); //以追加的方式打开文件,返回的是指针
do {
usleep(100); //暂停执行程序,参数是以微秒为单位的
} while (!flock($fp, LOCK_EX)); //LOCK_EX 取得独占锁定(写入的程序)进行排它型锁定 获取锁 有锁就写入,没锁就得
$res = fwrite($fp, $data . "\n"); // 以追加的方式写入数据到打开的文件
flock($fp, LOCK_UN); //LOCK_UN 释放锁定(无论共享或独占)。
fclose($fp); //关闭打开的文件指针
return $res;
}
16、PHP的is_writeable()函数存在Bug,无法准确判断一个目录/文件是否可写,请写一个函数来判断目录/文件是否绝对可写。
下面是CodeIgniter 中的is_really_writable函数解决方案,详见函数注释
其中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);
return TRUE;
} elseif (!is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) {
return FALSE;
}
fclose($fp);
return TRUE;
}
}
17、php中去除一个字符串中非字母部分
preg_replace('/[^a-z]/i', '', $str);
18、将一个字符串去除字符串中非字母部分,并将字符串中’_‘号后面首字母和字符串首字母大写,如a2b_ab23c得到AbAbc。
function getStr($str)
{
// [^a-z]用来匹配任何不在a和z之间的字符,i表示不区分大小写。
$str = preg_replace('/[^a-z_]/i', '', $str);
$arr = explode('_', $str);
foreach($arr as $key=>$value){
// ucfirst()首字母大写
$arr[$key] = ucfirst($value);
}
$str = implode('', $arr);
echo $str;
}
getStr('a2b_ab23c');
19、使用js实现进入一个页面10s后弹出提示框,提示框内容为‘hello world’。
setTimeout("alert('hello world')", 10000)
20、写一个sql语句查询表A中user_name字段重复出现两次以上的所有数据。
SELECT user_name, COUNT(user_name) AS num
FROM A GROUP BY user_name HAVING num >=2;
注意:
- having只能用在group by之后,对分组后的结果进行筛选(即使用having的前提条件是分组)。
- where肯定在group by 之前。
- where后的条件表达式里不允许使用聚合函数,而having可以。
21、redis内存满了之后怎么处理的
参考博客:https://blog.csdn.net/qq_28018283/article/details/80764518
如果达到设置的上限,Redis的写命令会返回错误信息(但是读命令还可以正常返回。)或者你可以将Redis当缓存来使用配置淘汰机制,当Redis达到内存上限时会冲刷掉旧的内容。有5中内存淘汰机制,具体看参考博客。
22、PHP互换两个变量值的方法(不用第三变量)
function exchange()
{
/**
* 双方变量为字符串或者数字时,可用此交换方法
* 使用异或运算
*/
$a = "This is A"; // a变量原始值
$b = "This is B"