[PHP Learn] PHP learn from the beginning 1

Source: Internet
Author: User
Preface: About 2006, study a period of time of PHP, and then did a download site, and later because of the postgraduate stage with Java, EE-related, so PHP was stranded, PHP these years have undergone a great change, the biggest change is to support the object-oriented.

Now because PHP needs to do something, learn again, start from the beginning!


Local and global scopes:

Variables declared outside the function have global scope and can only be accessed outside of the function


PHP Global Keywords

Global variables outside of the access function for the global keyword

$x = 5;

$y = 10;


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, which is also accessible within the function and can be used to update global variables directly.


The above example can be rewritten as:

$x = 5;

$y = 10;


function MyTest ()

{

$GLOBALS [' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y '];

}


MyTest ();

Echo $y;


?>

The difference between Echo and print:

  • echo-capable of outputting more than one string
  • Print-only one string is output 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 uses three parameters:

      1. The first parameter defines the name of the constant
      2. The second parameter defines the value of a constant
      3. The optional third parameter specifies whether the constant name is case-sensitive. The default is False.
     
      

    Constant output with no $


    Operator name Example Results
    == Equals $x = = $y Returns True if the $x equals $y.
    === Congruent (exact same) $x = = = $y Returns True if $x equals $y and they are of the same type.
    != Not equal to $x! = $y Returns true if $x is not equal to $y.
    <> Not equal to $x <> $y Returns true if $x is not equal to $y.
    !== Not congruent (completely different) $x!== $y Returns true if $x are not equal to $y and they are of different types.
    > Greater than $x > $y Returns True if the $x is greater than $y.
    < Greater than $x < $y Returns True if the $x is less than $y.
    >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y.
    <= Less than or equal to $x <= $y Returns True if $x is less than or equal to $y.


    Array:

    #array
    $car =array ("Volvo", "BWM", "Jeep");
    Var_dump ($car);


    Results:

    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 is "superglobals", which means that they is always accessible, regardless of scope- And you can access the them from any function, class or file without have to do anything special.

    The PHP Superglobal variables are:

  • $GLOBALS
  • $_server
  • $_request
  • $_post
  • $_get
  • $_files
  • $_env
  • $_cookie
  • $_session
  • $_server[' Http_referer ']:

    HTTP Referer is part of the header, and when the browser sends a request to the Web server, it usually takes referer to tell the server which page link I took from, and the server can get some information for processing.



    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 was viewing the current page
    $_server[' Remote_host '] Returns the Host name from where the user was 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 of the Web SERVER configuration file (if your script runs on a Virtu Al host, it would be is 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 is 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-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 a HTML form with method= "POST". $_post is also widely used to pass variables.


    Example






    $name = $_post[' fname ');
    Echo $name;
    ?>



    Htmlspecialchars


    In practice, is this filter invalid?



    PHP Regular Expressions:

    "+", "*", and "?". which

    The "+" metacharacters specify that their leading characters must appear one or more times in the target object.

    The "*" meta-character specifies that its leading character must appear 0 or more times in the target object,

    “?” A meta-character specifies that its leading object must appear 0 or more times in the target object.

    /jim{2,6}/
    The regular expression above specifies that the character m can appear consecutively 2-6 times in the matching object.


    \s: Used to match a single space character, including Tab key and line break;
    \s: Used to match all characters except a single space character;
    \d: Used to match numbers from 0 to 9;
    \w: Used to match letters, numbers, or underscore characters;
    \w: Used to match all characters that do not match the \w;
    . : Used to match all characters except the line break.


    The \b Locator specifies that the matching pattern must be one of the two boundaries at the beginning or end of the target string.

    The "\b" locator specifies that the matching object must be within the first and end two boundaries of the target string, that is, the matching object is neither the beginning of the target string nor the end of the target string.

    /\bbom/
    Because the regular expression pattern above starts with the "\b" locator, it can match a string that starts with "bomb" or "BOM" in the target object.
    /man\b/
    Because the regular expression pattern above ends with the "\b" locator, you can match a string that ends with "human", "Woman" or "man" in the target object.



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

    The "()" symbol contains content that must appear in the target object at the same time.

    /[^a-c]/

    ^ Representative negation

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.