PHP Learning PHP variables

Source: Internet
Author: User
Tags array arrays header html form http request variables variable valid
PHP variables

PHP3 supports the following types of variables:
(i), internal variables
There are mainly integers (interger), floating-point numbers (Float-point numbers), strings (string), arrays (array), objects (object).
1 Initialization variables
To initialize a variable in PHP, you simply assign it a value. For most types, this is the most straightforward. For arrays and objects, you can use other methods.
2 Initializing an array
Arrays can be assigned using one of two methods: Using a series of consecutive values, or using the array () function to construct (see the array Functions section).
To add a continuous number to an array, you simply assign the value to an array variable without the subscript. The value is added to the array as the last element of the array.
Example: $names [] = "Jill"; $names [0] = "Jill" $names [] = "Jack"; $names [1] = "Jack" is similar to C, Perl,
The array subscript is also starting from 0.
3 Initializing objects
To initialize an object, you need to establish a variable of that type with the new statement.
class Foo {
function Do_foo () {
echo "doing foo.";         }} $bar = new Foo; $bar->do_foo ();
4 variable Scopes
The scope of a variable is its valid range. For most PHP variables there is only one scope. Local variable scopes are used in user-defined functions.
Variables used within a function are set to local variables by default. For example: $a = 1; /* Global Scope * *
Function Test () {echo $a;/* reference to local scope variable * *}
Test (); This program does not output anything because the Echo statement outputs a local variable $a, and the $a within the function has never been assigned a value.
You may notice that this is a little bit different from C, where a global variable can be referenced directly within a function unless it is overwritten by a local variable.
This makes it possible to change the value of a global variable without notice. There must be an explicit description of the use of global variables within a function in PHP.
For 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 within the function, the variables are referenced globally. There is no limit to the number of global variables that a function can manipulate.
Another notable area of scope is the static variable.
A static variable exists in a local function, but its value is not 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 the $a to 0 and then "0" each time it is invoked. There is no effect on the $a++ since the function call ends after the variable
The $a was released. To make the counting program count effectively without losing the current count result, $a to be known as a static variable:
Function Test () {static $a = 0;         echo $a; $a + +;
Now, every time you call the test () function, it will hit the $a value and add the value to it. Static variables are essential when using recursive functions.
Recursive functions are calls to their own functions. You must be very careful when writing recursive functions,
Because its number of cycles is uncertain. You must ensure that there are sufficient conditions to end the recursive process. Here is a simple recursive function count to 10:
Function Test () {static $count = 0; $count + +;
Echo $count;          if ($count <) {Test (); }         }
(ii) Dynamic variables are sometimes more convenient to use variable names. That is, a variable name that can be dynamically assigned and used.
An assignment statement for a common variable such as: $a = "Hello"; A dynamic variable refers to the value of the variable as the name of a new variable.
In the above example, hello, you can use double $ as the variable name.
Example: $ $a = "world"; At this point, two variables are defined and stored in the PHP symbol tree: $a content is "Hello", $hello content is "world".
Therefore, the statement: echo "$a ${$a}"; The display result is identical to: echo "$a $hello"; (iii) PHP external variable 1, HTML form (get and post)
When a form is submitted to PHP3 script, PHP automatically gets the variables in the form. For example:

Name:
             
When "submit" is pressed, PHP3 automatically generates the variable: $name, which contains all the content that the user entered. 2, IMAGE SUBMIT variable name
When submitting a form, you can use the following tag to replace the standard submit button with a map: When the user clicks on the graph,
Two additional variables sub_x and sub_y are sent to the server along with the form. It contains the coordinates that the user clicks on the diagram.
An experienced person may notice that the name the browser actually sent contains a period instead of an underscore, but PHP automatically converts the period to an underscore.
3. HTTP Cookies
PHP supports HTTP cookies. Cookies store data in the client browser to keep in touch with the user or authenticate the user.
You can use the Setcookie () function to set up cookies. Cookies are part of the HTTP request header and must be returned to the user's browser before any output data
Call the Setcookie () function. It is similar to the limit of the Header () function. Any cookies returned by the client will be automatically converted to the standard PHP variable.
Just like the data for Get and post methods.
If you want to set multiple values in a cookie, add the name of the cookie to [].
For example: Setcookie ("mycookie[]", "testing", Time () +3600);
Note: New cookies will overwrite cookies already in your browser with the same name, unless they have different paths or domains.
4. Environment variable
PHP automatically converts an environment variable to a normal variable.
Echo $HOME; /* Shows the home environment variable, if set. */
Although information from get,post and cookie structures is automatically converted to PHP variables, it is best to explicitly read them from environment variables to ensure that the correct values are obtained.
You can use the getenv () function for this purpose. You can also set variables by using the putenv () function.
Variable type conversions
Explicit type declarations are not required (and not supported) in PHP when a variable is defined, and the type of a variable depends on the type of its value.
That is, if you assign a string value to the variable Var, var becomes a string variable. If you assign an integer value to Var, it becomes an integer variable.
An example of a PHP automatic type conversion is the addition operator ' + '. If any of the operands are double, all operands are computed by the double type.
The result is also double type. Otherwise, all operands are computed by the integer type and the result is an integer. Note: The type of the operand itself does not change;
The type transformation is only done $foo = "0" in the calculation; $foo is a string (ASCII) $foo + +; $foo is the string "1" (ASCII 49)
$foo + 1; $foo is now the integer (2) $foo = $foo + 1.3; $foo's now a double (3.3)
$foo = 5 + "Little piggies"; $foo is a double $foo = 5 + Small Pigs; $foo is an integer (15)
To change the type of a variable, you can also use the Settype () function.
1. Force type Conversion
Mandatory type conversions in PHP are the same as in C: write the desired type name in parentheses before the variable that needs to cast the type.
$foo = 10; $foo is an integer $bar = (double) $foo; $bar is a double
The allowed casts are: (int), (integer)-Cast to Integer (real), (double), (float)-cast to double
(string)-Cast to String (array)-Cast to Array (object)-Cast to Object
Note: Parentheses can contain tabs or spaces, and the following function is computed: $foo = (int) $bar; $foo = (int) $bar;
2. String conversion
When a string is evaluated as a numeric type, the value and type of the result are determined in the following manner.
If the string contains any '. ', ' e ', and ' e ' characters, it is evaluated as a double type. Otherwise, it is evaluated as an integer type.
The value is calculated from the beginning of the string. If the string is a valid number, the value is used, otherwise the value is 0.
A valid number is a symbol bit (optional) followed by one or several digits (which can also contain a decimal point) followed by an optional exponent.
Index is an ' e ' or ' e ' followed by one or several digits. $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 + "Small Pigs"; $foo is an integer (11)
$foo = 1 + "Little piggies"; $foo is a double (11); The string contains ' E '

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.