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);
}