Php variable Getting Started Tutorial, php variable basics

Source: Internet
Author: User
Php variable Getting Started Tutorial, php variable basics

  1. $ A = 'hello ';
  2. Function test (){
  3. Var_dump ($ );
  4. }
  5. // Test ();
  6. Include 'B. Inc ';

B. inc content:

  1. Echo 'hello ';
  2. ?>

The program can output hello normally, but the annotated test () cannot be parsed normally, because the variable $ a is undefined.

4. if you want to use global variables in a function, you can use the following two methods.

Global keyword global $ a, $ B; after a global variable is declared in the function, all references to any variable will point to its global version.

$ GLOBALS Super global variable array $ GLOBALS ['B'] = $ GLOBALS ['A'] + $ BLOBALS ['B']; usage is similar to that of global keywords.

5. static variablesStatic variables only exist in local function domains, but their values are not lost when the program runs out of this scope. In addition, it is initialized only once during Declaration. The value of a static function is not overwritten when a function is called each time.

If a value is assigned to a static variable in the declaration result, the parsing error is caused. the static declaration is parsed during compilation.

  1. Function test (){
  2. Static $ cnt = 0;
  3. Echo $ cnt;
  4. $ Cnt ++;
  5. If ($ cnt <10 ){
  6. Test ();
  7. }
  8. $ Cnt --;
  9. }
  10. Test ();

Similar to the C language static, the following C code can also be set to 0 ~ Output 10 numbers in sequence.

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

Static and global definitions of variables are implemented by reference.

5. variableVariable variables are a special usage in the PHP language and do not exist in other languages.

In short, a variable is a variable that obtains the value of a common variable as the variable name of the variable.

  1. $ A = 'hello ';
  2. $ A = 'world ';
  3. Echo "$ a"; // hello $ hello
  4. Echo "$ a $ {$ a}"; // hello world

When variables are used in arrays, ambiguity may occur. For example, if you write $ a [1], the compiler will report an error. the meaning you want to express must be replaced by the following two methods.

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

$ {$ A} [1] $ a acts as a variable and retrieves the value of index 1 in the variable.

6. Form variablesWhen 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: the points and spaces in the variable name are converted into underscores. For exampleChanged to $ _ REQUEST ["a_ B"]. the following example shows the use of identifiers in the form.

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']);

Because the period is not a valid character in the PHP variable name, the output result is: boolean falseboolean true.

String 'h3 '(length = 2) magic_quotes_gpc configuration command affects the value of get/post/cooie. this feature has been removed, the single quotation marks, double quotation marks, backslash, and NULL characters in the input are not escaped. If you need to escape, you can use addslashes (). If you need to reference a referenced string, you need to use stripslashes ().

Php also understands arrays in the context of Form variables.

Example: use more complex form variables, post the form to yourself, and display the data when submitting the form.

  1. If (isset ($ _ POST ['action']) {
  2. Var_dump ($ _ POST );
  3. } Else {
  4. $ Page = $ _ SERVER ['php _ SELF '];
  5. $ S = <
  6. STR;
  7. Echo $ s;
  8. }

Be especially careful when heredoc contains Complex Variables. the above code $ _ SERVER ['php _ SELF '] without curly braces will report an error during running.

  1. If (isset ($ _ POST ['action']) {
  2. Var_dump ($ _ POST );
  3. } Else {
  4. $ S = <
  5. STR;
  6. Echo $ s;
  7. }

For the above program, when a user clicks an image somewhere, the form will be transmitted to the server, and two variables sub_x and sub_y will be added, including the coordinates of the user clicking the image.

Array (size = 3) 'action' => string '1' (length = 1) 'sub _ X' => string '123456' (length = 3) 'Sub _ Y' => string '123' (length = 3) cookies

Php can use the setcookie () function to set cookies. cookies are part of the http header and must be called before sending any output to the browser.

Php cookie usage:

Cookie data is available in the corresponding cookie array. if you assign multiple values to a cookie variable, you must assign it to an array.

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.