[php learn] php 從頭開始學習1_PHP教程

來源:互聯網
上載者:User

[php learn] php 從頭開始學習1


前言:大概在2006年的時候,學習過一段時間的php,並且當時做了一個下載的網站,後來由於讀研究生階段用的是java、j2ee相關,所以php就擱淺掉了,php這些年也發生了很大的變化,最大一個變化是支援物件導向了。

現在由於需要php做些東西,再次學習,從頭開始!


Local和Global範圍:函數之外聲明的變數擁有global範圍,只能在函數之外訪問
PHP global關鍵字global關鍵字用與訪問函數外的全域變數function myTest(){ global $x,$y; $y=$x+$y;
}
myTest();echo $y;?>
PHP同時在名為$GLOBALS[index]的數組中儲存了所有的全域變數。下標存為變數名,這個數組在函數內也可以訪問,並且能夠用於直接更新全域變數。
上面的例子可以重寫為:function myTest(){ $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];}
myTest();echo $y;
?>

echo 和 print 之間的差異:

  • echo - 能夠輸出一個以上的字串
  • print - 只能輸出一個字串,並始終返回 1
    var_dump()函數會返回變數的資料類型和值。

    設定 PHP 常量

    如需設定常量,請使用 define() 函數 - 它使用三個參數:

    1. 首個參數定義常量的名稱
    2. 第二個參數定義常量的值
    3. 可選的第三個參數規定常量名是否對大小寫敏感。預設是 false。
      常量輸出不用帶$
      運算子 名稱 例子 結果
      == 等於 $x == $y 如果 $x 等於 $y,則返回 true。
      === 全等(完全相同) $x === $y 如果 $x 等於 $y,且它們類型相同,則返回 true。
      != 不等於 $x != $y 如果 $x 不等於 $y,則返回 true。
      <> 不等於 $x <> $y 如果 $x 不等於 $y,則返回 true。
      !== 不全等(完全不同) $x !== $y 如果 $x 不等於 $y,且它們類型不相同,則返回 true。
      > 大於 $x > $y 如果 $x 大於 $y,則返回 true。
      < 大於 $x < $y 如果 $x 小於 $y,則返回 true。
      >= 大於或等於 $x >= $y 如果 $x 大於或者等於 $y,則返回 true.
      <= 小於或等於 $x <= $y 如果 $x 小於或者等於 $y,則返回 true。

      數組:#array
      $car=array("Volvo","BWM","Jeep");
      var_dump($car);

      結果:array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BWM" [2]=> string(4) "Jeep" }


      foreach:

      Syntax

      foreach ($array as $value) {
      code to be executed;
      }

      Example

      $colors = array("red","green","blue","yellow");

      foreach ($colors as $value) {
      echo "$value
      ";
      }
      ?>

      PHP Global Variables - Superglobals

      Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

      The PHP superglobal variables are:

        $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_ENV $_COOKIE $_SESSION $_SERVER['HTTP_REFERER']:HTTP Referer是header的一部分,當瀏覽器向web伺服器發送請求的時候,一般會帶上Referer,告訴伺服器我是從哪個頁面連結過來的,伺服器籍此可以獲得一些資訊用於處理。

        Element/Code Description
        $_SERVER['PHP_SELF'] Returns the filename of the currently executing script
        $_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface (CGI) the server is using
        $_SERVER['SERVER_ADDR'] Returns the IP address of the host server
        $_SERVER['SERVER_NAME'] Returns the name of the host server (such as www.w3schools.com)
        $_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as Apache/2.2.24)
        $_SERVER['SERVER_PROTOCOL'] Returns the name and revision of the information protocol (such as HTTP/1.1)
        $_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (such as POST)
        $_SERVER['REQUEST_TIME'] Returns the timestamp of the start of the request (such as 1377687496)
        $_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string
        $_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request
        $_SERVER['HTTP_ACCEPT_CHARSET'] Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1)
        $_SERVER['HTTP_HOST'] Returns the Host header from the current request
        $_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it)
        $_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol
        $_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page
        $_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page
        $_SERVER['REMOTE_PORT'] Returns the port being used on the user's machine to communicate with the web server
        $_SERVER['SCRIPT_FILENAME'] Returns the absolute pathname of the currently executing script
        $_SERVER['SERVER_ADMIN'] Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as someone@w3schools.com)
        $_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the web server for communication (such as 80)
        $_SERVER['SERVER_SIGNATURE'] Returns the server version and virtual host name which are added to server-generated pages
        $_SERVER['PATH_TRANSLATED'] Returns the file system based path to the current script
        $_SERVER['SCRIPT_NAME'] Returns the path of the current script
        $_SERVER['SCRIPT_URI'] Returns the URI of the current page


        PHP $_REQUEST


        PHP $ _REQUEST is used to collect data after submitting an HTML form.

        Example






        $name = $_REQUEST['fname'];
        echo $name;
        ?>




        PHP $_POST

        PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.


        Example






        $name = $_POST['fname'];
        echo $name;
        ?>



        htmlspecialchars
        實際應用中,這個過濾無效?

        phpRegex:“+”, “*”,以及 “?”。其中,“+”元字元規定其前置字元必須在目標對象中連續出現一次或多次,“*”元字元規定其前置字元必須在目標對象中出現零次或連續多次,“?”元字元規定其前置對象必須在目標對象中連續出現零次或一次。
        /jim{2,6}/
        上述Regex規定字元m可以在匹配對象中連續出現2-6次,

        \s:用於匹配單個 空格符,包括tab鍵和分行符號;
        \S:用於匹配除單個空格符之外的所有字元;
        \d:用於匹配從0到9的數字;
        \w: 用於匹配字母,數字或底線
        \W:用於匹配所有與\w不匹配的字元;
        . :用於匹配除分行符號之外的所有字元。

        \b定位器規定匹配模式必須出現在目標字串的開頭或結尾的兩個邊界之一“\B”定位器則規定匹配對象必須位於目標字串的開頭和結尾兩個邊界之內,即匹配對象既不能作為目標字串的開頭,也不能作為目標字串的結尾。/\bbom/
        因為上述Regex模式以“\b”定位器開頭,所以可以與目標對象中以 “bomb”, 或 “bom”開頭的字串相匹配。
        /man\b/
        因為上述Regex模式以“\b”定位器結尾,所以可以與目標對象中以 “human”, “woman”或 “man”結尾的字串相匹配。


        /([a-z][A-Z][0-9])+/“()”符號包含的內容必須同時出現在目標對象中。
        /[^A-C]/^代表否定


        http://www.bkjia.com/PHPjc/854422.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/854422.htmlTechArticle[php learn] php 從頭開始學習1 前言:大概在2006年的時候,學習過一段時間的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.