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

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

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

Local和Global作用域:
函數之外聲明的變量擁有global作用域,隻能在函數之外訪問

PHP global關鍵字
global關鍵字用與訪問函數外的全局變量
<?php
$x=5;
$y=10;

function myTest()
{
global $x,$y;
$y=$x+$y;

}

myTest();
echo $y;
?>

PHP同時在名為$GLOBALS[index]的數組中存儲瞭所有的全局變量。下標存為變量名,這個數組在函數內也可以訪問,並且能夠用於直接更新全局變量。

上面的例子可以重寫為:

<?php
$x=5;
$y=10;

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

      <?php
      $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

        <form method="post" action="”>
        Name:

        <?php
        $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

        <form method="post" action="”>
        Name:

        <?php
        $name = $_POST[‘fname’];
        echo $name;
        ?>

        htmlspecialchars

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

        php正則表達式:

        “+”, “*”,以及 “?”。其中,

        “+”元字符規定其前導字符必須在目標對象中連續出現一次或多次,

        “*”元字符規定其前導字符必須在目標對象中出現零次或連續多次,

        “?”元字符規定其前導對象必須在目標對象中連續出現零次或一次。

        /jim{2,6}/
        上述正則表達式規定字符m可以在匹配對象中連續出現2-6次,

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

        \b定位符規定匹配模式必須出現在目標字符串的開頭或結尾的兩個邊界之一

        “\B”定位符則規定匹配對象必須位於目標字符串的開頭和結尾兩個邊界之內,即匹配對象既不能作為目標字符串的開頭,也不能作為目標字符串的結尾。

        /\bbom/
        因為上述正則表達式模式以“\b”定位符開頭,所以可以與目標對象中以 “bomb”, 或 “bom”開頭的字符串相匹配。
        /man\b/
        因為上述正則表達式模式以“\b”定位符結尾,所以可以與目標對象中以 “human”, “woman”或 “man”結尾的字符串相匹配。

        /([a-z][A-Z][0-9])+/

        “()”符號包含的內容必須同時出現在目標對象中。

        /[^A-C]/

        ^代表否定

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *