PHP之基礎篇1

來源:互聯網
上載者:User

一. 基本文法

  1. 開始結束標記: ""

     

    Note:檔案末尾的 PHP 程式碼片段結束標記"?>"可以不要,有些情況下當使用 include() 或者 require() 時省略掉會更好些。

  2. 同java,c一樣,php每個語句後需用分號 " ; " 結束。

二. 類型

php支援的8種基礎資料型別 (Elementary Data Type)

四種標量類型:

  • boolean (布爾型)
  • integer (整型)
  • string (字串)
  • float (浮點型)

兩種複合類型:

  • array (數組型)
  • object (對象)

兩種特殊類型:

  • resource (資源)
  • null (null)

Note:

  • var_dump() 查看錶達式值和類型,is_type() 查看某個類型。
  • 整數溢出,如果給定一個整數或運算結果得出的整數超出int範圍(2^32),將會解釋會float。

三.字串(string)

聲明字串: 字串通常用單引號或雙引號定義。

  1. 在單引號中的變數和特殊含義的字元不會被替換

     php > $age = 12; php > echo "他很高\n他的年齡才$age"; 他很高 他的年齡才12; php > echo '他很高\n他的年齡才$age'; 他很高\n他的年齡才$age;
  2. 字串可以用'.' (點) 操作符串連起來

     php > echo "Hello,"."World"; Hello,World
  3. 存取和修改字串中的字元(類似於數組)

     php > $str = "Hello,World"; php > echo $str[1]; e php > echo $str{0}; H
  4. 其他類型轉換為字串(string)

    在一個值前面加上(string)或者使用strval()函數來轉換成字串型。

     php > $age = 18; php > var_dump($age); int(18) php > $str = (string)$age; php > var_dump($str); string(2) "18"

常用字串函數

  • 字串替換

    str_replace ($search , $replace , $subject [, int &$count ])

    $search: 尋找替換目標值

    $replace: $search的替換值

    subject: 執行替換的數組或者字串

    count: 控制匹配和替換的次數

     php > $str = "Hello,My name is Tom,what is your name?"; php > echo str_replace("name","nickname",$str); Hello,My nickname is Tom,what is your nickname?
  • 去掉字串兩邊空格

    trim ($str)

    $str: 目標字串

      php > $str = "  Hello     ";  php > echo trim($str);  Hello
  • 去掉字串中html和php標記

    strip_tags ($str)

    $str: 目標字串

      php > $str = "

    Hello World

    "; php > echo strip_tags($str); Hello World
  • 將目標字串重複多次

    str_repeat ($str,int $num)

    $str: 目標字串

    $num: 重複次數

      php > $str = "Hello World!!";  php > echo str_repeat($str,5);  Hello World!!Hello World!!Hello World!!Hello World!!Hello World!!
  • 返回字串長度

    strlen ($str)

    $str: 目標字串

      php > $str = "Hello World";  php > echo strlen($str);  11
  • 計算字串出現的次數

    substr_count (string $haystack , string $needle )

    $haystack: 目標字串

    $needle: 尋找出現次數的字串

      php > $str = "Hello World,Hello China";  php > echo substr_count($str,"Hello");  2
  • 截取字串

    substr ( string $string , int $start [, int $length ] )

    $string: 目標字串

    $start: 如果 start 是非負數,返回的字串將從 string 的 start 位置開始,從 0 開始計算。如果 start 是負數,返回的字串將從 string 結尾處向前數第 start 個字元開始。如果string 的長度小於或等於 start,將返回 FALSE。

    $length: 1.如果提供了正數的 length,返回的字串將從 start 處開始最多包括 length 個字元(取決於 string 的長度)。2.如果提供了負數的 length,那麼 string 末尾處的許多字元將會被漏掉(若 start 是負數則從字串尾部算起)。3.如果 start 不在這段文本中,那麼將返回一個Null 字元串。4.如果提供了值為 0,FALSE 或 NULL 的 length,那麼將返回一個Null 字元串。5.如果沒有提供 length,返回的子字串將從 start 位置開始直到字串結尾。

      php > $str = "Hello,World";  php > echo substr($str,1);  ello,World  php > echo substr($str,1,3);  ell  php > echo substr($str,1,-3);  ello,Wo  php > echo substr($str,1,10);  ello,World
  • 聯繫我們

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