php 簡明文法

來源:互聯網
上載者:User
1 代碼塊 php以 或標記
2 php變數以$開頭
3 字串處理:
3.1 字串串連符 為.號
3.2 字串長度 strlen(string)
3.3 子字串位置 strpos(str1,str2) 未查到時返回FALSE
4 數組 $Arryname = array(items,items) or $Arryname = array(conststring=>items,conststring=>items) or $Arryname[index]=items or $Arryname[constString]=itmes or $Arryname[index]=array ....
5 foreach語句為 foreach(array as value) 這個和其他語言有區別
6 函數定義 function functionName(參數){}
7 取請求參數的值 $_GET["formItmeName"] get的值
$_POST["formItmeName"] post的值
$_COOKIE["cookieName"] cookie的值
$_REQUEST["QuestName"] get post cookie 等
8 日期 建立日期mktime(hour,minute,second,month,day,year,is_dst)
例$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
格式化日期date(format[,timestamp])
例echo date("Y/m/d");echo "
";echo date("Y.m.d");
9 include() 函數可獲得指定檔案中的所有文本,並把文本拷貝到使用 include 函數的檔案中。
require() 函數與 include() 相同,不同的是它對錯誤的處理方式。
include() 函數會產生一個警告(但是指令碼會繼續執行),而 require() 函數會產生一個致命錯誤(fatal error)(在錯誤發生後指令碼會停止執行)。

10 fopen() 函數用於在 PHP 中開啟檔案。
$file=fopen("welcome.txt","r");
fgets() 函數用於從檔案中逐行讀取檔案。
fgetc() 函數用於從檔案逐字元地讀取檔案。
fclose($file); 關閉檔案
if (feof($file)) echo "End of file";
fclose($file);

11 使用 PHP 的全域數組 $_FILES,可以從客戶電腦向遠程伺服器上傳檔案。

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}


12 setcookie() 函數用於設定 cookie。
注釋:setcookie() 函數必須位於 標籤之前。
setcookie(name, value, expire, path, domain);
setcookie("user", "Alex Porter", time()+3600);
PHP 的 $_COOKIE 變數用於取回 cookie 的值。
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
使用 isset() 函數來確認是否已設定了 cookie
if (isset($_COOKIE["user"]))
當刪除 cookie 時,您應當使到期日期變更為過去的時間點
setcookie("user", "", time()-3600);

13 把使用者資訊儲存到 PHP session 中之前,首先必須啟動會話。
注釋:session_start() 函數必須位於 標籤之前:
儲存和取回 session 變數的正確方法是使用 PHP $_SESSION 變數:
session_start();
// store session data
$_SESSION['views']=1;
刪除某些 session 資料,可以使用 unset() 或 session_destroy() 函數
unset() 函數用於釋放指定的 session 變數:unset($_SESSION['views']);
session_destroy() 函數徹底終結 session ,session_destroy();
注釋:session_destroy() 將重設 session,您將失去所有已儲存的 session 資料。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.