PHP Learning PHP Variable _php tutorial

Source: Internet
Author: User
Tags variable scope
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 Initializing 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
An array can be assigned using one of two methods: using a series of sequential values, or by using the array () function (see section functions of the array).
To add successive values to an array, you simply assign the assignment to an array variable that is not marked. The value is added as the last element of the array into 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, create 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 Scope
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.
The variables used within the 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 overridden by a local variable.
This makes it possible for people to pay no attention to modifying the values of global variables. In PHP, the use of global variables within a function must be explicitly described.
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 in the function as global variables, the variables you want to refer to are global. 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 the 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 $ A to 0 and then "0" on each invocation. The self-addition of $a++ has no effect because the function call ends after the variable
$a was released. To make the Count program count without losing the current count result, $a to declare a static variable:
Function Test () {static $a = 0;         echo $a; $a + +;
Now, each time the test () function is called, it will hit a value of $ A and increase its value. Static variables are essential when using recursive functions.
A recursive function is a function that calls itself. You must be very careful when writing recursive functions,
Because the number of cycles is uncertain. You must ensure that there are sufficient conditions to end the recursion process. The following is a simple recursive function that counts to 10:
Function Test () {static $count = 0; $count + +;
Echo $count;          if ($count <) {Test (); }         }
(b) 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 an ordinary variable such as: $a = "Hello"; A dynamic variable refers to the value of the variable as the name of a new variable.
In the example above, hello can be used as a variable name by double $.
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 exactly the same as: echo "$a $hello"; (c) 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 entered by the user. 2. IMAGE SUBMIT variable Name
When submitting a form, you can use the following markup to replace the standard submit button with a graph: When the 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 at which the user clicked on the diagram.
Experienced people may notice that the actual name given by the browser 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 to verify the identity of the user.
You can use the Setcookie () function to set cookies. Cookies are part of the HTTP request header, so they 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 the Get and post methods.
If you want to set multiple values in a cookie, add [] to the name of the cookie,
For example: Setcookie ("mycookie[", "Testing", Time () +3600);
Note: The new cookie will overwrite the cookie already in your browser, unless they have a different path or domain.
4. Environment variables
PHP automatically converts the 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 the environment variables to ensure that the correct values are obtained.
You can use the getenv () function for this purpose. You can also set a variable by using the Putenv () function.
Variable type conversions
It is not necessary (and not supported) to make explicit type declarations in a variable definition, and the type of a variable depends on the type of its value.
That is, if you assign a string value to 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 one operand is of type double, all operands are evaluated by 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 is not changed;
The type transform is only done at calculation $foo = "0"; $foo is a string (ASCII) $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 + "Ten Little piggies"; $foo is a double ($foo) = 5 + "Small Pigs"; $foo is an integer (15)
To change the type of the variable, you can also use the Settype () function.
1. Forced type conversion
Coercion type conversions in PHP are the same as in C: write the desired type name in parentheses before the variable that needs to be cast for the type.
$foo = 10; $foo is an integer $bar = (double) $foo; $bar is a double
The allowable 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: The parentheses can contain tab or space, and the following function will be evaluated: $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 sign bit (optional) followed by one or several digits (also containing a decimal point) followed by an optional exponent.
An exponent is an ' e ' or ' e ' followed by one or several 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 + "Small Pigs"; $foo is an integer (11)
$foo = 1 + "Ten Little piggies"; $foo is a double (11); The string contains ' E '

http://www.bkjia.com/PHPjc/315727.html www.bkjia.com true http://www.bkjia.com/PHPjc/315727.html techarticle PHP variable PHP3 supports variables of the following types: (i), internal variables mainly have integers (interger), floating-point numbers (Float-point numbers), strings (string), arrays (array), objects (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.