PHP Variable Primer tutorial, PHP variable Basics

Source: Internet
Author: User
    1. $a = ' Hello ';
    2. function Test () {
    3. Var_dump ($a);
    4. }
    5. Test ();
    6. Include ' B.inc ';
Copy Code

B.inc content:

    1. echo ' Hello ';
    2. ?>
Copy Code

The program can output hello normally, but the annotated test () cannot parse correctly because the variable $ A is undefined.

4. Use global variables if you want to use global variables in a function, there are two ways to do it.

Global keyword Global $a, $b; After you declare the globals in a function, all references to any of the variables point to their global version.

$GLOBALS Super global variable array $globals[' B '] = $GLOBALS [' a '] + $BLOBALS [' B ']; usage is similar to the Global keyword.

5 static variable static variable exists only in local function domain, but its value is not lost when program execution leaves this scope. Also, only one time is initialized at the time of declaration, and each call to the function does not overwrite the value of the static function.

A static variable is assigned a value in the declaration with the result of an expression, resulting in a parse error, which is parsed at compile time.

    1. function Test () {
    2. static $cnt = 0;
    3. Echo $cnt;
    4. $cnt + +;
    5. if ($cnt < 10) {
    6. Test ();
    7. }
    8. $CNT--;
    9. }
    10. Test ();
Copy Code

Similar to the static C language, the following C code can also be 0~9 10 numbers in turn output.

    1. #include
    2. void Test (void) {
    3. static int cnt = 0;
    4. printf ("%d", CNT);
    5. cnt++;
    6. if (CNT < 10) {
    7. Test ();
    8. }
    9. cnt--;
    10. }
    11. int main (void) {
    12. Test ();
    13. return 0;
    14. }
Copy Code

The static and global definitions for variables are implemented in a reference manner.

5, variable variable variables are a special use of PHP language, I do not know the existence of other languages.

In short, a mutable variable is a variable variable that gets the value of an ordinary variable as the variable name of the variable variable.

    1. $a = ' Hello ';
    2. $ $a = ' world ';
    3. echo "$a $ $a"; Hello $hello
    4. echo "$a ${$a}"; Hello World
Copy Code

Ambiguous problems can occur when mutable variables are used in arrays. For example, write the $ $a [1], the compiler will error, you want to express the meaning needs to be replaced in the following two ways.

${$a [1]} $a [1] as a variable

${$a}[1]$ $a as a variable and takes out the value of index 1 in the variable.

6. Form variables When a form is submitted to a PHP script, the information in the form is automatically available in the script and can be accessed through $_get[], $_post[], and $_request[].

Note that the points and spaces in the variable name are converted to underscores. For examplebecomes $_request["A_b", the following example shows the use of identifiers in a form.

Copy Code

Form processing file process.php.

    1. Var_dump (Isset ($_post[' my.text '));
    2. Var_dump (Isset ($_post[' mytext '));
    3. Var_dump (Isset ($_post[' my_text '));
    4. Var_dump ($_post[' my_text ');
Copy Code

Because the period is not a valid character in the PHP variable name, the output: Boolean Falseboolean Falseboolean True

The string ' H3 ' (length=2) MAGIC_QUOTES_GPC configuration directive affects the value of Get/post/cooie, which is deprecated and is not escaped by single quotes, double quotes, backslashes, and null characters in the input. If you need to escape using addslashes (), you need to use stripslashes () if you need to dereference a referenced string.

PHP also understands arrays in the context of form variables.

example, use more complex form variables and post the form to yourself and display the data at commit time.

    1. if (Isset ($_post[' action ')) {
    2. Var_dump ($_post);
    3. } else {
    4. $page = $_server[' php_self ');
    5. $s = <<<>
    6. STR;
    7. Echo $s;
    8. }
Copy Code

Take extra care when you have complex variables in Heredoc, and the above code $_server[' php_self ' does not add curly braces to the run times error.

    1. if (Isset ($_post[' action ')) {
    2. Var_dump ($_post);
    3. } else {
    4. $s = <<<>
    5. STR;
    6. Echo $s;
    7. }
Copy Code

For the above program, when the user clicks on the image somewhere, the form will be transferred to the server, and add two variables sub_x and sub_y, which contains the coordinates of the user click the picture.

Array (size=3) ' action ' = = String ' 1 ' (length=1) ' sub_x ' = = String ' 334 ' (length=3) ' sub_y ' = = String ' 282 ' (Leng th=3) Cookies

PHP can use the Setcookie () function to set Cookies,cookies as part of the HTTP information header, so it must be called before any output is sent to the browser.

PHP Cookies use:

Cookie data is available in the corresponding cookie array, and if multiple values are assigned to a cookie variable, they must be assigned to a group.

  • 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.