Time of Update: 2017-01-19
本文執行個體講述了PHP輸出兩個數字中間有多少個迴文數的方法。分享給大家供大家參考。具體分析如下:"迴文數"是一種數字。如:98789, 這個數字正讀是98789,倒讀也是98789,正讀倒讀一樣,所以這個數字就是迴文數。<?php for($i=10;$i<100;$i++){ $len=strlen($i); $l=1; $k=intval($len)/2+1; for($j=0;$j<$k;$j++){ if
Time of Update: 2017-01-19
本文執行個體講述了php實現通用的從資料庫表讀取資料到數組的函數。分享給大家供大家參考。具體分析如下:此函數不關心表結構,只需要指定表名、結構和查詢條件既可以對錶進行通用查詢操作,非常實用。function listmytablerows($table, $name, $field, $where, $textID){/ / Connect to the database and query executionconnect ();$Sql = "select * from". $table.
Time of Update: 2017-01-19
本文執行個體講述了PHP安全上傳圖片的方法。分享給大家供大家參考。具體分析如下:這段代碼用於上傳圖片,可以根據圖片類型檢測圖片是否安全,不是簡單的檢測副檔名<?php // upload.phpecho <<<_END<html><head><title>PHP Form Upload</title></head><body><form method='post'
Time of Update: 2017-01-19
本文執行個體講述了php中實現可以返回多個值的函數用法。分享給大家供大家參考。具體分析如下:在python和golang中都有一個函數同時返回多個值的方法,其實php也可以,但相比python和golang要稍微麻煩一點,下面是一個簡單的示範範例,這裡用到了list函數<?php function retrieve_user_profile() { $user[] = "Jason"; $user[] = "jb51.net"; $user[] = "English"
Time of Update: 2017-01-19
本文執行個體講述了php輸出指定時間以前時間格式的方法。分享給大家供大家參考。具體分析如下:比如說你需要在php中輸出3天前,20分鐘以前,可以參考下面的代碼function ago($time) { $time = strtotime($time); $delta = time() - $time; if ($delta < 60) { return 'less than a minute ago.'; } else if ($delta < 120) { return
Time of Update: 2017-01-19
本文執行個體講述了php中文繁體和簡體相互轉換的方法。分享給大家供大家參考。具體分析如下:下面的代碼用到了繁體和簡體字型檔對照表,實現繁體與簡體字的轉換功能複製代碼 代碼如下:<?phpclass utf8_chinese{ private $utf8_gb2312; private $utf8_big5; private $data; public function
Time of Update: 2017-01-19
本文執行個體講述了php使用NumberFormatter格式化貨幣的方法。分享給大家供大家參考。具體實現方法如下:$amount = '12345.67';$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);echo 'UK: ' . $formatter->formatCurrency($amount, 'EUR') . PHP_EOL;$formatter = new
Time of Update: 2017-01-19
本文執行個體講述了php自訂錯誤處理用法。分享給大家供大家參考。具體如下:<?phperror_reporting(E_ALL);function ErrHandler($errorno, $errorstr, $errorfile, $errorline){ $display = true; $notify = false; $halt_script = false; $error_msg = "<br>The $errorno error is
Time of Update: 2017-01-19
本文執行個體講述了php內嵌函數用法。分享給大家供大家參考。具體分析如下:php中可以在函數內部內嵌一個函數,調用範圍僅限於函數本身<?phpfunction msg(){ echo("<center><h2>Displaying even numbers</h2></center><p><p>"); function displayeven() { $ctr=0;
Time of Update: 2017-01-19
本文執行個體講述了php顯示指定目錄下子目錄的方法。分享給大家供大家參考。具體實現方法如下:<?phpecho "<h2>subdirs in dir</h2><ul>";$basedir = basename( __FILE__ );$dirtoscan = ($basedir . '/somedir/');$albumlisting = scandir($dirtoscan);foreach ($albumlisting as $item)
Time of Update: 2017-01-19
本文執行個體講述了php給每個段落添加空格的方法。分享給大家供大家參考。具體實現方法如下:<?php //Prepends whitespace to each line of a string function white_space( $string, $whitespace ) { //Create an array from the string, each key having one line $string = explode( PHP_EOL, $string
Time of Update: 2017-01-19
本文執行個體講述了php使用mysqli向資料庫添加資料的方法。分享給大家供大家參考。具體實現方法如下:$mydb = new mysqli('localhost', 'username', 'password', 'databasename');$sql = "INSERT INTO users (fname, lname, comments)VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[comments]')";if
Time of Update: 2017-01-19
本文執行個體講述了php計算函數執行時間的方法。分享給大家供大家參考。具體如下:我們可以通過在程式的前後分別記錄開始和結束時間,兩個時間差就是程式的執行時間。<?php$long_str = "this is a test to see how much time md5 function takes to execute over this string";// start timing from here$start = microtime(true);// function
Time of Update: 2017-01-19
SAPI:Server Application Programming Interface服務端應用編程連接埠。他就是php與其他應用互動的介面,php指令碼要執行有很多中方式,通過web伺服器,或者直接在命令列行下,也可以嵌入其他程式中。SAPI提供了一個和外部通訊的介面,常見的SAPI有:cgi、fast-cgi、cli、Apache模組的dll等。1、CGICGI即通用閘道介面(common gatewag
Time of Update: 2017-01-19
本文執行個體講述了php提交表單發送郵件的方法。分享給大家供大家參考。具體如下:儲存下面的html代碼到:email.html檔案<html><head><title>Simple Send Mail </title></head><body><h1>Mail Form</h1><form name="form1" method="post"
Time of Update: 2017-01-19
本文執行個體講述了php帶抄送和密件副本的郵件發送方法。分享給大家供大家參考。具體分析如下:程式中用到了php的mail函數,該函數定義如下: bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Time of Update: 2017-01-19
SESSION與COOKIE的不同之處首先是,cookie的檔案是儲存在用戶端的,而session是儲存在伺服器的,相比而言,為了提高一定的安全性,session更具有優勢。因為session在伺服器端一般情況是伺服器的管理員管理的,但cookie是在用戶端的儲存,任何人都可以看的,如果不指定,密碼也是明文儲存,安全性顯而易見。而且session相對來說更強大一些,可以儲存數組,甚至對象等,在某種程度上,可以降低開發成本。下面是session的使用代碼:session資料的增加:複製代碼
Time of Update: 2017-01-19
本文執行個體講述了php校正表單檢測欄位是否為空白的方法。分享給大家供大家參考。具體如下:php校正表單,檢測欄位是否為空白,當表單中有未填寫的欄位,則會顯示錯誤資訊。<html><body><form METHOD="POST" ACTION="ErrorCheck.php"><h1>Contact Information</h1><label>Nickname:</label><input TYPE="
Time of Update: 2017-01-19
本文執行個體講述了php擷取從html表單傳遞數組的方法。分享給大家供大家參考。具體如下:將表單的各個元素的name都設定成同一個數組對象既可以以數組的方式傳遞表單值html頁面如下:<form method="post" action="arrayformdata.php"><label>Tags</label><input type="text" name="tags[]"/><input type="text" name="tags[]
Time of Update: 2017-01-19
本文執行個體講述了php輸出xml屬性的方法。分享給大家供大家參考。具體分析如下:這段代碼通過一個簡單的範例示範了php如何讀取xml檔案並輸出xml屬性<?php $xml = simplexml_load_file("books.xml"); foreach($xml->book[0]->author->attributes() AS $a => $b) { echo "$a = $b <br />"; }?>xml