Leilin Peng Share: PHP variables

  Is a variable for storing information of "container":

  

  $x=5;

  $ Y = 6;

  $z=$x+$y;

  echo $z;

  ?>

  And algebraic similar

  x=5

  y = 6

  z=x+y

  In algebra, we use letters (such as x), and give it a value (such as 5).

  From the above expression z = x + y, we can calculate the value of z 11.

  In PHP, these letters are called variables.

Variable is a container for storing data.

  PHP variables

  Similar algebra, a value can be imparted to the PHP variable (x = 5) or expression (z = x + y).

  Variable names can be very short (e.g., x and y) or a more descriptive name (e.g., age, carname, totalvolume).

  PHP variable rules:

  Variables begin with $ symbol, followed by the variable name

  Variable names must begin with a letter or an underscore character

  Variable names can only contain alphanumeric characters and the underscore (Az, 0-9, and _)

  Variable names can not contain spaces

  Variable names are case sensitive (the Y $ and $ Y are two different variables)

PHP PHP statements and variables are case-sensitive.

  Creating (statement) PHP variables

  PHP did not order declaring variables.

  Variable is created the first time that you assign to it:

  

  $txt="Hello world!";

  $x=5;

  $ Y = 10.5;

  ?>

  In the above statement is executed, the stored values ​​of variables txt Hello world !, save variable x and the value 5.

  Note: When you assign a text value to a variable, on both sides of the text value in quotation marks.

  PHP is a weakly typed language

  In the example above, we note that, do not have to declare the data type of the variable to PHP.

  PHP based on the value of the variable, automatically convert the variable to the correct data type.

  In a strongly typed programming language, we must declare the type and name (define) variables before using variables.

  PHP variable scope

  Scope of a variable is the script variables can be referenced / used parts.

  There are four different PHP variable scope:

  local

  global

  static

  parameter

  Local and global scope

  In all external variables defined functions with global scope. In addition to functions, global variables can be accessed by any part of the script, to access a global variable in a function, you need to use the global keyword.

  In PHP variables declared inside a function are local variables, can only be accessed inside the function:

  

  $ X = 5; // global variables

  function myTest()

  {

  $ Y = 10; // local variables

  echo "

Test function in the variables:

";

  echo "variable x is: $ x";

  echo "
";

  echo "variable y is: $ y";

  }

  myTest();

  echo "

Test function outside variables:

";

  echo "variable x is: $ x";

  echo "
";

  echo "variable y is: $ y";

  ?>

  In the above example myTest () function defines the variable $ x and $ y. Variable $ x out of function declaration, it is a global variable, $ Y variables declared within a function so that it is a local variable.

  When we call myTest () function and output values ​​of two variables, the function will output the value of $ y is a local variable, but not the output value of $ x, $ x variable as defined in the outer function can not be used in the function, if to access a global variable in a function, you need to use the global keyword.

  Then we () function output myTest outer two variables, $ function will output the value of x full local variables, but not the output value of $ y, $ y variable is defined as a function, the local variable belongs.

You can use the same variable name in different functions, such as the variable name in the function definition is local, act only within the function.

  PHP global keyword

  global keyword is used within the function to access the global variables.

  Call the global variable outside the function defined within a function, we need to function in front of the variables plus the global keyword:

  

  $x=5;

  $ Y = 10;

  function myTest()

  {

  global $ x, $ y;

  $ Y = $ x + $ y;

  }

  myTest();

  echo $ y; // Output 15

  ?>

  PHP will all global variables are stored in an array called $ GLOBALS [index] in. Save the variable name index. This array can be accessed within the function can also be directly used to update global variables.

  Examples of the above can be written as:

  

  $x=5;

  $ Y = 10;

  function myTest()

  {

  $ GLOBALS [ 'and'] = $ GLOBALS [ 'x'] + $ GLOBALS [ 'and'];

  }

  myTest();

  echo $ and;

  ?>

  Static Scope

  When a function is complete, it's usually all variables will be deleted. However, sometimes you want a local variable is not deleted.

  To do this, use the static keyword when you first declare a variable:

  

  function myTest()

  {

  static $x=0;

  echo $ x;

  $x++;

  }

  myTest();

  myTest();

  myTest();

  ?>

  Then, each time the function is called, the variable will retain the value of the time it is called before the function.

  Note: This variable is still local variables of the function.

  Parameters Scope

  The parameters are passed to the local variable by calling code values.

  Parameters are declared in the parameter list, as part of a function declaration:

  

  function myTest($x)

  {

  echo $ x;

  }

  myTest(5);

  ?>

  We will do it discussed in more detail in the section PHP function.

  View all PHP tutorial article: https://www.codercto.com/courses/l/5.html (edit: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/linpeng1/p/10967227.html