Introduction to variable scopes and Super global variables in php

Source: Internet
Author: User
1. assign values to variables after they are declared. There are two ways to assign values: value assignment and reference assignment. 1. value assignment $ colorquot; redquot; $ sum12 + qu... I. variable assignment

You can assign values to variables after they are declared. There are two methods: value assignment and reference assignment.

1. value assignment

$ Color = "red ";

$ Sum = 12 + "15";/* $ sum = 27 */

2. if you want two variables to point to the same copy of a value, you must assign values by referencing them.

Reference assignment

The created variable is the same as that referenced by another variable. if multiple variables reference the same content and modify any of the variables, the other variables will be reflected.

The PHP instance code is as follows:

 

II. Scope of variables

Variables can be declared at any position of the php script, but the location of the declared variables will greatly affect the scope of the access variables. the accessible scope is called scope ).

Scope of php variable 4:

△Local variable

△Function parameters

△Global variable

△Static variable

1. local variables

The variable declared in the function is considered a local variable and can only be referenced in the function. when the function that declares the variable is exited, the variable and the corresponding value are revoked, this eliminates the possibility that variables that cause global access are intentionally or unintentionally modified.

$x = 4 ;  function assignx ( ) {  $x = 0 ;  print "$x inside function is $x . 
" ; } assignx ( ) ; print "$x outside of function is $x .
" ;

The code execution result is:

$ X inside function is 0.

$ X outside function is 4.

2. function parameters

Like other programming languages, any function that accepts parameters must declare these parameters in the function header, although these parameters accept values outside the function, however, you cannot access these parameters after exiting the function. (except for parameters passed by reference)

For example:

function x10 ( $value ) {  $value = $value * 10 ;  return $value ;  } 

After the function is executed, the parameter is about to be revoked.

3. global variables

In contrast to local variables, global variables can be accessed anywhere in the program. when a global variable is changed in the function, you need to display the variable as a global variable in this function, you only need to add global before the variable in the function.

For example:

$somevar = 15 ;  function addit ( ) {  global $somevar ;  $somevar ++ ;  print "somevar is $somevar" ;  }  addit ( ) ;  

$ Somevar: The value displayed is 16. However, if global $ somevar is removed, the variable $ somevar is implicitly set to 0, and 1 is added. The value displayed at the end is 1.

Another way to declare global variables is to use the $ global array of php, as shown below:

$ Somevar = 15; function addit () {$ globals ['somevar '] ++;} addit (); print "somevar is ". $ globals ['somevar ']; // The return value is as follows: somevar is 16.

4. static variables

Static scope: function parameters of common variables are revoked at the end of the function. However, static variables do not lose a value when the function exits and can be retained when the function is called again, you can declare a static variable by adding the keyword static to the front of the worker beam.

Static $ somevar;

Consider an example:

function keep_track ( ) {  static $count = 0 ;  $count ++ ;  print $count ;  print "
" ; } keep_track ( ) ; keep_track ( ) ; keep_track ( ) ; keep_track ( ) ;

If $ count is not specified as static (corresponding, $ count is a local variable), the output will be

1

1

1

1

Because $ count is static, it retains the previous value each time the function is executed. the output is as follows:

1

2

3

4

Static scopes are useful for recursive functions. a recursive function is a powerful programming concept. it is a function that can call itself repeatedly until a certain condition is met.

5. php Super global variables

Php provides a lot of useful predefined variables that can be accessed at the person and location where the script is executed. it is used to provide a large amount of environment-related information, you can use these variables to obtain detailed information about the current user session, user operating system environment, and local operating environment. php creates some variables, the availability and value of many other variables depend on the operating system and web services.

Output all predefined variables:

foreach ( $_server as $var => $value ) {  echo "$var => $value 
" ; }

Show the user's IP address:

Print "hi! Your ip address is ". $ _ server ['remote_addr '];

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.