New School: PHP server variable settings

Source: Internet
Author: User
Tags php server type casting
Newbie School: PHP server variable setting method introduction, read newbie School: PHP server variable setting method introduction, variable type change (juggling) PHP not needed (or not supported) when a name variable defines its variable type, the type of a variable is determined by the relationship between the variables used, that is, if you assign "> <LINKhref =" http://www.ph to a variable var

Variable type change (juggling)

PHP does not need (or does not support) to specify the variable type in the name variable; the type of a variable is determined by the relationship between the variables used, that is, if you assign a string value to a variable var, var becomes a string variable. If you assign an integer to var, it becomes an integer variable.

An example of PHP's automatic conversion of variable types is the addition operator '+ '. If any operand is a double-precision number, all operands are evaluated as double-precision numbers, and the result is also a double-precision number. Otherwise, the operand is considered an integer, and the result is also an integer. Note that this does not affect the variable type of each operand. The only change is how the operand is processed during calculation.

$ Foo = "0"; // $ foo is a string whose value is "0" (ASCII 48) $ foo ++; // $ foo is a string, the value is "1" (ASCII 49) $ foo + = 1; // $ foo is an integer (2) now $ foo = $ foo + 1.3; // $ foo is now a double precision (3.3) $ foo = 5 + "10 Little Piggies"; // $ foo is an integer (15) $ foo = 5 + "10 Small Pigs"; // $ foo is an integer (15)

If you think the last two expressions in the above example seem a bit strange, please refer to the "string conversion" section.

If you want to force a variable to be calculated as a fixed type, see the "casting" section. If you want to change the type of a variable, see the description of the function "settype.

Determine the type of a variable

Because PHP decides the type of variables and converts them as needed, the type of a specific variable is not very obvious at any time. PHP includes some functions to find out the type of this variable. These functions are gettype (), is_long (), is_double (), is_string (), is_array (), and is_object ().

Type casting)

In PHP, the type is forced to be roughly the same as in C: write the required type in parentheses before the variable to be forced.

$ Foo = 10; // $ foo is an integer $ bar = (double) $ foo; // $ bar is a double number.

The following mandatory methods are allowed:

(Int), (integer)-Forced to integer

(Real), (double), (float)-forced double precision

(String)-Forced to a string

(Array)-Force an array

(Object)-forced object

Note that the parentheses allow both tabs (tabs) and spaces (spaces), so the following statements are equivalent:

$ Foo = (int) $ bar;

$ Foo = (int) $ bar;

String conversion

When a string is calculated as a numeric value, its results and types are determined as described below.

If the string contains characters '.', 'e', or 'e', it is treated as a double-precision variable. Otherwise, it is treated as an integer.

The value of this string is determined by the first part of the word. If the string starts with any valid numeric data, the numeric data is the value of this string. Otherwise, the value is zero ). Valid numeric data follows the following tags, followed by one or more numbers (which can contain decimal points), followed by an optional index. An index consists of one or more numbers following 'E' or 'e.

$ Foo = 1 + "10.5"; // $ foo is the number of double precision (11.5) $ foo = 1 + "-1.3e3"; // $ foo is the number of double precision (-1299) $ foo = 1 + "bob-1.3e3"; // $ foo is an integer (1) $ foo = 1 + "bob3"; // $ foo is an integer (1) $ foo = 1 + "10 Small Pigs"; // $ foo is an integer (11) $ foo = 1 + "10 Little Piggies "; // $ foo is an integer (11); // This string contains the character 'e'

For more information, see the section about strtodd (3) in the Unix manual.

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.