This article mainly introduces related information about PHP variables. For more information, see the basic elements in modern programming languages: variables, flow control interfaces, and functions. Can I write programs without variables? This is obviously possible, for example:
The code is as follows:
Echo "Hello AndHM ";
?>
This program is simple and outputs a string.
Just like we can program using binary, we can do most of the work without using variables. without using variables, our program will lose great flexibility, and variables will let us store the values, it can be used elsewhere in the program, or new values are saved by computation. Variables have three basic features:
Name. Variable identifier. Like a puppy, the master may give these puppies a name they like. In variable naming, PHP inherits the syntax style of Perl. the variable starts with the dollar sign, followed by the variable name. A valid variable name must start with a letter or underline followed by any number of letters, numbers, or underscores. PHP also supports compound variables, that is, variables like $ a, which will be interpreted twice. This gives PHP very flexible dynamic features.
Type. The type of variables is like the breed of puppies. different puppies may have different lineage, some smart, some shopping, and so on. In many static languages, variables are specified during definition and cannot be changed during the program running. if one of your puppies can only specify a specific breed, will it take a lot of effort ;-) PHP is like this. it is a weak type language and can be assigned any type of value.
Value. This indicates the specific content. This is like a real puppy. You can name any puppy "Xiao Qi", which is also true in programming languages. you can give the variable a value that can represent a range. However, at the same time, a variable can have only one value.
The letters that make up the variable name in PHP can be English letters a-z, A-Z, or ASCII characters from 127 to 255 (0x7f-0xff ). Variable names are case sensitive.
In addition to variables, we often come into use with variable-related concepts in PHP, such as constants, global variables, static variables, and type conversion. This chapter describes the implementation related to variables. These include the low-level storage structure of PHP variables, implementation of weak-type systems, and mutual conversion between these types.
First look at a piece of PHP code:
The code is as follows:
$ Foo = 10;
$ Bar = 20;
Function change (){
Global $ foo;
$ Bar = 0;
$ Foo ++;
}
Change ();
Echo $ foo, '', $ bar;
?>
The code output is 11 20.
But why is there such output? How can variables be implemented inside PHP? How is the scope of a variable implemented? This is the topic about variables in this Chapter. next we will start with the most basic implementation of variables.
Not all variable values in programming languages can be changed. Think about the variables we have learned in mathematics. Their values cannot be changed. For example, x + y = 10; the values of the variables x and y cannot be changed. In a specific scenario, that is, an equation only represents a specific value. The benefit of variable values cannot be changed: this will produce as few side effects as possible, this is true in Erlang. it is a functional programming language and is worth learning.