PHP variable _ PHP Tutorial

Source: Internet
Author: User
Tags variable scope
PHP variables for PHP learning. PHP variable PHP3 supports the following types of variables: (1) internal variables mainly include integer (interger), float-pointnumbers), string (string), array (array ), object ). 1. initial PHP variable

PHP3 supports the following types of variables:
(1) internal variables
Mainly include integer, float-point numbers, string, array, and object ).
1 Initialization variable
To initialize a variable in PHP, you just need to assign a value to it. For most types, this is the most direct. You can use other methods for arrays and objects.
2. initialize the array
An array can be assigned a value using either of the two methods: using a series of continuous values, or using an Array () function (see the array functions section ).
To add continuous values to an array, you only need to assign a value to the array variable without any lower mark. This value is added to the array as the final element of the array.
Example: $ names [] = "Jill"; // $ names [0] = "Jill" $ names [] = "Jack "; // $ names [1] = "Jack" is similar to c and perl,
The array subscript also starts from 0.
3. initialize the object
To initialize an object, you must use the new statement to create a variable of this type.
Class foo {
Function do_foo (){
Echo "Doing foo." ;}}$ bar = new foo; $ bar-> do_foo ();
4. variable scope
The scope of a variable is its valid range. Most PHP variables have only one scope. Use local variable scopes in user-defined functions.
The variables used in the function are set as local variables by default. Example: $ a = 1;/* global scope */
Function Test () {echo $ a;/* reference to local scope variable */}
Test (); this program will not output anything, because the echo statement must output the local variable $ a, and $ a in the function has never been assigned a value.
You may notice that this is a little different from the C language. The global variable in C can be referenced directly in the function unless it is overwritten by a local variable.
This makes it possible for people to change the value of the global variable. To use global variables in a function in PHP, it must be explicitly described.
Example: $ a = 1; $ B = 2; Function Sum () {global $ a, $ B;
$ B = $ a + $ B;} Sum (); echo $ B; the above program will output "3 ".
By declaring $ a and $ B as global variables in the function, you need to refer to global variables. There is no limit on the number of global variables that a function can manipulate.
Another noteworthy aspect of the scope is the static variable.
A static variable exists in a local function, but its value will not be lost when the program leaves the function.
Consider the following example: Function Test () {$ a = 0; echo $ a; $ a ++ ;}
This function is useless because it assigns $ a to 0 and then outputs "0" for each call ". Adding $ a ++ does not work because the variable after the function call ends.
$ A is released. To enable the recorder to effectively count without losing the current result, $ a should be known as a static variable:
Function Test () {static $ a = 0; echo $ a; $ a ++;
} Now, every time you call the Test () function, it will output the value of $ a and increase its value. Static variables are essential when recursive functions are used.
Recursive functions call their own functions. Be careful when writing recursive functions,
The number of cycles is unknown. Make sure there are enough conditions to end the recursive process. Below is a simple recursive function to count to 10:
Function Test () {static $ count = 0; $ count ++;
Echo $ count; if ($ count <10) {Test ();}}
(2) it is easier to use variable names for dynamic variables. That is, a variable name that can be dynamically assigned and used.
A value assignment statement for a common variable, for example, $ a = "hello"; a dynamic variable references the value of this variable as the name of a new variable.
In the above example, hello, you can use double $ as the variable name.
For example: $ a = "world"; at this point, the two variables are defined and stored in the PHP symbol tree: $ a's content is "hello ", the content of $ hello is "world ".
Therefore, the result of the statement echo "$ a $ {$ a}"; is exactly the same as echo "$ a $ hello"; (3) PHP external variables 1. HTML forms (GET and POST)
When a form is submitted to PHP3 script, PHP automatically obtains the variables in the form. For example:

Name:
             
When you press "submit", PHP3 automatically generates the variable $ name, which contains all the content entered by the user. 2. image submit variable name
When submitting a form, you can use the following tag to replace the standard submit button with a pair of graphs: When a user clicks the graph,
Two additional variables sub_x and sub_y are sent to the server along with the form. It contains the coordinates of users' clicks on the graph.
Experienced users may notice that the name actually sent by the browser contains a full stop instead of an underline, but PHP automatically converts the full stop to an underline.
3. HTTP Cookies
PHP supports HTTP cookies. Cookies store data in the client browser to maintain contact with users or verify user identity.
You can use the setcookie () function to set cookies. Cookies are part of the HTTP request header. Therefore, before any output data is returned to the user's browser
Call the SetCookie () function. It is similar to the limitation of the Header () function. Cookies returned by any client will be automatically converted to standard PHP variables,
It is like the data of the GET and POST methods.
If you want to set multiple values in a cookie, add [] to the cookie name.
Example: SetCookie ("MyCookie []", "Testing", time () + 3600 );
Note: New cookies overwrite existing cookies with the same name in your browser, unless they have different paths or domains.
4. environment variables
PHP automatically converts environment variables to common variables.
Echo $ HOME;/* Shows the HOME environment variable, if set .*/
Although information from GET, POST, and Cookie structures will be automatically converted to PHP variables, it is best to read them explicitly from the environment variables to ensure that they GET the correct values.
To do this, you can use the getenv () function. You can also use the putenv () function to set variables.
Variable type conversion
PHP does not need or support explicit type declaration during variable definition. the type of a variable depends on the type of its value.
That is to say, if you assign a string value to the variable var, var becomes the string variable. If you assign an integer value to var, it becomes an integer variable.
An example of PHP automatic type conversion is the addition operator '+ '. If any operand is of the double type, all operands are calculated based on the double type,
The result is double. Otherwise, all operands are calculated based on the integer type, and the result is also integer. Note: the type of the operand does not change;
Type conversion only performs $ foo = "0" during calculation; // $ foo is a string (ASCII 48) $ foo ++; // $ foo is the string "1" (ASCII 49)
$ Foo + = 1; // $ foo is now an integer (2) $ foo = $ foo + 1.3; // $ foo is now a double (3.3)
$ Foo = 5 + "10 Little Piggies"; // $ foo is a double (15) $ foo = 5 + "10 Small Pigs "; // $ foo is an integer (15)
To change the variable type, you can also use the settype () function.
1. forced type conversion
Forced type conversion in PHP is the same as that in C: write the desired type name in the brackets before the variable that requires forced type conversion.
$ Foo = 10; // $ foo is an integer $ bar = (double) $ foo; // $ bar is a double
The following mandatory conversions are allowed: (int), (integer)-cast to integer (real), (double), (float)-cast to double.
(String)-cast to string (array)-cast to array (object)-cast to object
Note: The brackets can contain tabs or spaces. the following functions are calculated as: $ foo = (int) $ bar;
2. string conversion
When a string is calculated as a numeric value, the value and type of the result are determined in the following way.
If the string contains any '.', 'e', and 'e' characters, it is calculated as a double type. Otherwise, it is calculated as an integer.
The value starts from the start of the string. If the string is a valid number, use this value; otherwise, the value is 0.
A valid number is a sign (optional) followed by one or several digits (which can also contain a decimal point), followed by an optional index.
An index is an 'e' or 'e' followed by one or more numbers. $ Foo = 1 + "10.5"; // $ foo is a double (11.5)
$ Foo = 1 + "-1.3e3"; // $ foo is a double (-1299) $ foo = 1 + "bob-1.3e3"; // $ foo is a double (1)
$ Foo = 1 + "bob3"; // $ foo is an integer (1) $ foo = 1 + "10 Small pig"; // $ foo is an integer (11)
$ Foo = 1 + "10 Little Piggies"; // $ foo is a double (11); the string contains 'e'

PHP3 supports the following types of variables: (1) internal variables include integer, floating-point numbers, string, array ), object ). 1 Initial...

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.