You are here: 首頁 文章分類選單 PHP分享特區

飛朵啦學習手札

本網站建議使用Firefox2.0以上,或是使用Goole瀏覽器來瀏覽,並使用1024x768解析度來觀看.

PHP分享特區

[PHP] file_exists VS is_file 效能分析

E-mail 列印 PDF

在寫程序時發現在判斷文件是否存在時,有兩種寫法,有的人用了is_file,有的人用了file_exists,用哪個更好或者說更合適呢

寫程序驗證一下:

分別執行1000次,記錄所需時間。

文件存在(當前目錄)

is_file:0.4570ms

file_exists:2.0640ms

文件存在(絕對路徑3層/www/hx/a/)

is_file:0.4909ms

file_exists:3.3500ms

文件存在(絕對路徑5層/www/hx/a/b/c/)

is_file:0.4961ms

file_exists:4.2100ms

文件不存在(當前目錄)

is_file:2.0170ms

file_exists:1.9848ms

文件不存在(絕對路徑5層/www/hx/a/b/c/)

is_file:4.1909ms

file_exists:4.1502ms

目錄存在

file_exists:2.9271ms

is_dir:0.4601ms

目錄不存在

file_exists:2.9719ms

is_dir:2.9359ms

is_file($file)

file_exists($file)

當$file是目錄時,is_file返回false,file_exists返回true

文件存在的情況下,is_file比file_exists要快得多;

要檢測文件所在的目錄越深,速度差越多,但至少快4倍。

文件不存在的情況下,is_file比file_exists要慢一點點,但可以忽略不計。

目錄存在的情況下,is_dir比file_exists要快得多;

目錄不存在的情況下,is_dir比file_exists要慢一點點,但可以忽略不計。

結論:

如果要判斷文件是否存在,用函數is_file(),

如果要判斷目錄是否存在,用函數is_dir(),

 
 

[轉貼]PHP 推撥 IOS PUSH

E-mail 列印 PDF

文章轉貼自:http://blog.csdn.net/newjueqi/article/details/8315093

是按照教程http://blog.csdn.net/newjueqi/article/details/7898591 來做的。

注意事項:

1. 測試階段使用的推送地址:

ssl://gateway.sandbox.push.apple.com:2195

 

正式上線使用的推送地址:

ssl://gateway.push.apple.com:2195

 

最近更新 ( 週四, 25 四月 2013 16:26 )
 
 

[轉貼]php 將數字補零 使用str_pad

E-mail 列印 PDF

用php 補零去google

會找到有些使用sprintf函式的方法 像下面這樣
$var = 1;
echo sprintf("%02d", $var);
其實php本身就有一個專門可以補齊位數的函式 : str_pad()
string str_pad ( string $input , int $pad_length [, string $pad_string= " " [, int $pad_type= STR_PAD_RIGHT ]] )
$input : 原字串
$pad_length : 補齊後的位數
$pad_string : 用來補齊的字串
$pad_type : 補齊的方式 有三種,STR_PAD_RIGHT (由右邊補)、STR_PAD_LEFT (由左邊補)、STR_PAD_BOTH (左右兩邊都補), 預設為STR_PAD_RIGHT
所以其實不只可以補零,要補什麼字都可以
而以補零來舉例的話就是
$value = 7;
//將數字由左邊補零至三位數
$value = str_pad($value,3,'0',STR_PAD_LEFT);
echo $value;
// 結果會印出 007;
//下面這是document裡的例子
$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
echo str_pad($input, 6 , "___");               // produces "Alien_"
這樣應該就可以瞭解這個函式的用法了~
最近更新 ( 週一, 01 十月 2012 16:46 )
 

[轉貼]利用PHPExcel,輸出Excel

E-mail 列印 PDF

關於PHPExcel的一些講解 蠻詳細的

http://www.ewebsite.com.tw/docs-blog/article-33

http://merry05.blog.hexun.com.tw/64300210_d.html

http://mrbignose.blogspot.tw/2010/02/phpphpexcelexcel.html

最近更新 ( 週二, 14 八月 2012 11:15 )
 

計算文字在HTML中的顯示寬度

E-mail 列印 PDF

我自己寫的js版參考以下文章做出的

http://www.rupeng.com/innersupesite/index.php/7/viewspace-583

JS版:

function arial_strlen(val, word_length){

var lencounter=0;

var sub_str = 0;

for (var i = 0; i < val.length; i++) {

if(ck_null(word_length) != '-'){

if(Math.ceil(lencounter*2) >= word_length){

return sub_str;

}

}

ch = val.substr(i,1);

 

var char_050 = ",./;'[]=-/*-!@#$%^&*()_{}:\"<>?1234567890fijlrIt";

 

asc_ch = ch.charCodeAt();

//if(ord(ch)>128){

if (ch.match(/[^\x00-\xff]/ig) != null){

lencounter++;

}else if(char_050.indexOf(ch) != -1){

lencounter+=0.5;

}else if(asc_ch>=48 && ch<=57){

lencounter+=0.55;

}else if(asc_ch>=97 && asc_ch<=122){//a~z

lencounter+=0.55;

}else if(asc_ch>=65 && asc_ch<=90){//A~Z

lencounter+=0.8;

}else{

lencounter++;

}

sub_str++;

}

return Math.ceil(lencounter*2);

}

 
第 5 頁, 共 8 頁