1、不用新变量直接交换现有两个变量的值
1:
list($a, $b) = array($b, $a);
2:
$a = $a . $b;
$b = strlen( $b );
$b = substr( $a, 0, (strlen($a) – $b ) );
$a = substr( $a, strlen($b) );
3:(必须用一个两个字符串都都不能出现的字符做为分隔符)
$a = $b.','.$a ;
$a = explode(',', $a);
$b = $a[1];
$a = $a[0];
4:这个是当两个数都是数字的时候:
$a = $a + $b;
$b = $a – $b;
$a = $a – $b;
5:借助数组
$a = array($a,$b);
$b = $a[0];
$a = $a[1];
2、解决多线程同时读写一个文件的问题
<?php
$fp = fopen("/tmp/lock.txt","w+");
if(flock($fp, LOCK_EX)){// 进行排它型锁定
fwrite($fp,"Write something here\n");
flock($fp, LOCK_UN);// 释放锁定
}else{
echo "Couldn't lock the file !";
}
fclose($fp);
?>
3、写一个函数,可以遍历文件夹下的所有文件和文件夹。
function get_dir_info($path){
$handle = opendir($path);//打开目录返回句柄
while(($content = readdir($handle))!== false){
$new_dir = $path . DIRECTORY_SEPARATOR . $content;
if($content == '..' || $content == '.'){
continue;
}
if(is_dir($new_dir)){
echo "<br>目录:".$new_dir . '<br>';
get_dir_info($new_dir);
}else{
echo "文件:".$path.':'.$content .'<br>';
}
}
}
get_dir_info($dir);
4、一群猴子排成一圈,按1,2,…,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停 的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n, 输出最后那个大王的编号。
<?php
function king($n, $m){
$arr = array();
for($i = 0; $i < $n; $i++){
$arr[$i] = $i;
}
$nums = 1;
while(count($arr) > 1){
foreach ($arr as $key => $value) {
if($nums == $m){
unset($arr[$key]);
$nums = 1;
}else{
$nums++;
}
}
}
$new_arr = array_values($arr);
var_dump($new_arr[0] + 1);
}
king(10,10);
5、求两个文件的相对路径
getpath('/a/b/c/d/e.php', '/a/d/12/34/c.php');
/** 计算path1 相对于 path2 的路径,即在path2引用paht1的相对路径
* @param String $path1
* @param String $path2
* @return String
*/
function getpath($path1, $path2){
$arr1 = explode('/', $path1);
$arr2 = explode('/', $path2);
// 获取相同路径的部分
$intersection = array_intersect_assoc($arr1, $arr2);
$depth = 0;
for($i=0,$len=count($intersection); $i<$len; $i++){
$depth = $i;
if(!isset($intersection[$i])){
break;
}
}
// 前面全部匹配
if($i==count($intersection)){
$depth ++;
}
// 将path2的/ 转为 ../,path1获取后面的部分,然后合拼
// 计算前缀
if(count($arr2)-$depth-1>0){
$prefix = array_fill(0, count($arr2)-$depth-1, '..');
}else{
$prefix = array('.');
}
$tmp = array_merge($prefix, array_slice($arr1, $depth));
$relativePath = implode('/', $tmp);
return $relativePath;
}
6、select * from table where (ID = 10) or (ID = 32) or (ID = 22) 让结果按10, 32, 22的顺序检索出来?
感觉是错的!!!!!!!!!!!!
Select *
from user_info
Where (ID IN (10, 32, 22))
order BY FIND_IN_SET(ID, '10, 32, 22')
7、实现中文字串截取无乱码的方法
function GBsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$end=$start+$length;
for($i=$start;$i<$end;$i++){
if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str;
}else{
return $string;
}
}
8、
$num = 10;
function multiply(){
$num = $num * 10;
}
multiply();
echo $num;
会输出10而不是100
9、写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名
答案1:
function getExt($url){
$arr = parse_url($url);
$file = basename($arr['path']);
$ext = explode(".",$file);
return $ext[1];
}
答案2:
function getExt($url) {
$url = basename($url);
$pos1 = strpos($url,".");
$pos2 = strpos($url,"?");
if(strstr($url,"?")){
return substr($url,$pos1 + 1,$pos2 - $pos1 - 1);
} else {
return substr($url,$pos1);
}
}
10、冒泡排序
Function mysort($arr){
For($i=0;$i<count($arr); $i++){
For($j=0; $j<count($arr)-1-$i; $j++){
If($arr[$j]> $arr[$j+1]){
$tmp=$arr[$j];
$arr[$j]=$arr[$j+1];
$arr[$j+1]=$tmp;
}
}
}
Return$arr;
}
11、写一个二维数组排序算法函数
//二维数组排序, $arr是数据,$keys是排序的健值,$order是排序规则,1是升序,0是降序
function array_sort($arr, $keys, $order=0) {
if(!is_array($arr)) {
return false;
}
$keysvalue =array();
foreach($arr as$key => $val) {
$keysvalue[$key] = $val[$keys];
}
if($order == 0){
asort($keysvalue);
}else {
arsort($keysvalue);
}
reset($keysvalue);
$new_array =array();
foreach($keysvalue as $key => $vals) {
$new_array[$key] = $arr[$key];
}
return $new_array;
}
12、实现以下功能: 字符串“open_door” 转换成 “OpenDoor”、”make_by_id” 转换成 ”MakeById”。
function test($str){
$arr1=explode('_',$str);
//$arr2=array_walk($arr1,ucwords( ));
$str = implode(' ',$arr1);
return ucwords($str);
}