三個PHP常用代碼範例

來源:互聯網
上載者:User

標籤:

作為一個正常的程式員,會好幾種語言是十分正常的,相信大部分程式員也都會編寫幾句PHP程式,如果是WEB程式員,PHP一定是必備的。儘管PHP經常被人詬病,被人貶低,被人當玩笑開,事實證明,PHP是全世界網站開發中使用率最高的程式設計語言。PHP最大的缺點是太簡單,文法不嚴謹,架構體系很弱,但這也是它最大的優點。

網上有人總結幾種程式設計語言的特點:

PHP 就是: Quick and Dirty

Java 就是: Beauty and Slowly

Ruby 就是: Quick and Beauty

python 就是: Quick and Simple

 

一、隨機顏色產生器

function randomColor() { 

    $str = ‘#‘; 

    for($i = 0 ; $i < 6 ; $i++) { 

        $randNum = rand(0 , 15); 

        switch ($randNum) { 

            case 10: $randNum = ‘A‘; break; 

            case 11: $randNum = ‘B‘; break; 

            case 12: $randNum = ‘C‘; break; 

            case 13: $randNum = ‘D‘; break; 

            case 14: $randNum = ‘E‘; break; 

            case 15: $randNum = ‘F‘; break; 

        } 

        $str .= $randNum; 

    } 

    return $str; 

$color = randomColor();

 

二、時間差異計算函數

function ago($time)

{

   $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");

   $lengths = array("60","60","24","7","4.35","12","10");

   $now = time();

   $difference     = $now - $time;

   $tense         = "ago";

   for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {

       $difference /= $lengths[$j];

   }

   $difference = round($difference);

   if($difference != 1) {

       $periods[$j].= "s";

   }

   return "$difference $periods[$j] ‘ago‘ ";

}

 

三、裁剪圖片

$filename= "test.jpg";

list($w, $h, $type, $attr) = getimagesize($filename);

$src_im = imagecreatefromjpeg($filename);

 

$src_x = ‘0‘;   // begin x

$src_y = ‘0‘;   // begin y

$src_w = ‘100‘; // width

$src_h = ‘100‘; // height

$dst_x = ‘0‘;   // destination x

$dst_y = ‘0‘;   // destination y

 

$dst_im = imagecreatetruecolor($src_w, $src_h);

$white = imagecolorallocate($dst_im, 255, 255, 255);

imagefill($dst_im, 0, 0, $white);

imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

header("Content-type: image/png");

imagepng($dst_im);

imagedestroy($dst_im);

 

三個PHP常用代碼範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.