PHP中幾種常用的運算函式

2014-01-03

round($n)  四捨五入
ceil($n)   無條件進位
floor($n)  無條件捨去

php裏面轉換大小寫strtolower() , strtoupper()
mysql裏面轉換大小寫 ucase() , lcase() 

 

如果需要引用某外部網頁的資料,可以用下面函式來取得該頁面的資料:

echo file_get_contents('htp://url_of_the_iframe/content.php');

 

透過php本文實現POST:

     function Post($url, $post = null)  {  
        $context = array();  
       
        if (is_array($post)) {  
            ksort($post);  
       
            $context['http'] = array (  
                'timeout'=>60,  
                'method' => 'POST',  
                'content' => http_build_query($post, '', '&'),  
             );  
        }  
       
        $result = file_get_contents($url, false, stream_context_create($context));
        if ($result){
               return $result; 
           }else{
               printf("Connection busy! Please Try letter");die();
           } 
     }  
       
     $data = array (  
        'name' => 'test',  
        'email' => 'test@gmail.com',  
        'submit' => 'submit',  
     );  
       
     echo Post($url, $data); 

 

透過php本文實現GET: 

function Get($url) {
         $opts = array(  
            'http'=>array(  
                'method'=>"GET",  
                'timeout'=>1,  
            )  
        );  
           
        $context = stream_context_create($opts);  
       
        $result = file_get_contents($url, false, $context);

        if ($result){
            return $result; 
        }else{
            printf("Connection busy! Please Try letter");die();
        }
        
    }       
        
    echo Get($url);

 

針對上下月份取得時的問題,透過下方函式可以避免 ($sign:0->下個月 1->上個月 / $var:區間 / $format: 輸出格式 )

function GetMonth($sign="1",$var=1,$format="Y-m") {  
    $tmp_date=date("Ym");  
    $tmp_year=substr($tmp_date,0,4);  
    $tmp_mon =substr($tmp_date,4,2);  
    $tmp_nextmonth=mktime(0,0,0,$tmp_mon+$var,1,$tmp_year);  
    $tmp_forwardmonth=mktime(0,0,0,$tmp_mon-$var,1,$tmp_year);  
    if($sign==0){  
        return $fm_next_month=date($format,$tmp_nextmonth);          
    }else{  
        return $fm_forward_month=date($format,$tmp_forwardmonth);           
    }  
}  

Contact

Github

Codepen

歡迎參觀我的賣場
© 2013 Copyright Digishot Web | Design Tools
Visitors【639153】
digishot webdesign studio