Home > Backend Development > PHP Tutorial > [php learn] PHP learning from scratch 1_PHP tutorial

[php learn] PHP learning from scratch 1_PHP tutorial

WBOY
Release: 2016-07-13 10:21:36
Original
994 people have browsed it

[php learn] php learn from scratch 1

Foreword: Around 2006, I studied PHP for a period of time, and made a download website at that time. Later, because I used Java and j2ee during my graduate studies, I dropped PHP. A lot of changes have taken place over the years, the biggest one being support for object-oriented programming.

Now because I need to do something with php, I have to learn it again and start from scratch!


Local and Global scope: Variables declared outside the function have global scope and can only be accessed outside the function
PHP global keyword The global keyword is used to access global variables outside functions function myTest() { global $x,$y; $y=$x+$y;
}
myTest(); echo $y; ?>
PHP also stores all global variables in an array named $GLOBALS[index]. The subscript is stored as a variable name. This array is also accessible within the function and can be used to directly update global variables.
The above example can be rewritten as: function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; }
myTest(); echo $y;
?>

Difference between echo and print:

  • echo - can output more than one string
  • print - can only output one string, and always returns 1
    The var_dump() function returns the data type and value of the variable.

    Set PHP constants

    To set a constant, use the define() function - it takes three arguments:

    1. The first parameter defines the name of the constant
    2. The second parameter defines the value of the constant
    3. The optional third parameter specifies whether the constant name is case-sensitive. The default is false.
      <?php
      define("GREETING", "Welcome to W3School.com.cn!");
      echo GREETING;
      ?>
      Copy after login
      Constant output does not need to include $
      运算符 名称 例子 结果
      == 等于 $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 如果 $x 小于 $y,则返回 true。
      >= 大于或等于 $x >= $y 如果 $x 大于或者等于 $y,则返回 true.
      小于或等于 $x 如果 $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
        In actual application, this filtering is invalid?

        php regular expression: "+", "*", and "?". in, The "+" metacharacter stipulates that its leading character must appear one or more times continuously in the target object. The "*" metacharacter stipulates that its leading character must appear zero times or multiple times in a row in the target object. The "?" metacharacter specifies that its leading object must appear zero or once in the target object.
        /jim{2,6}/
        The above regular expression stipulates that the character m can appear 2-6 times continuously in the matching object,

        s: used to match a single space character , including tab key and newline character;
        S: used to match all characters except a single space character;
        d: used to match numbers from 0 to 9;
        w: is used to match letters, numbers or underscore characters ;
        W: used to match all characters that do not match w;
        . : Used to match all characters except newline characters.

        The b locator specifies that the matching pattern must appear at one of two boundaries, the beginning or the end of the target string. The "B" locator stipulates that the matching object must be located within the two boundaries of the beginning and end of the target string, that is, the matching object cannot be used as the beginning or the end of the target string. /bbom/
        Because the above regular expression pattern starts with the "b" locator, it can match strings that start with "bomb", or "bom" in the target object.
        /manb/
        Because the above regular expression pattern ends with the "b" locator, it will match any string in the target object that ends with "human", "woman", or "man".


        /([a-z][A-Z][0-9])+/ The content contained in the "()" symbol must also appear in the target object.
        /[^A-C]/ ^ stands for negation


        www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854422.htmlTechArticle[php learn] PHP learning from scratch 1 Preface: Around 2006, I studied PHP for a period of time , and made a download website at that time. Later, because I was in graduate school, I used...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template