PHP Server variable Setting method

Source: Internet
Author: User
Tags numeric numeric value php server valid

PHP does not need (or is not) to define his variable type in the reputation variable; the type of a variable is determined by the relationship in which the variable is used, that is, if you assign a value of a string to a variable Var, var becomes a string variable. If you assign an integer to Var again, he becomes an integer variable.

An example of an automatic conversion variable type of PHP is the operator ' + ' of the addition. If any operand is a double-precision number, then all operands are evaluated as a double-precision number, and the result is also a double-precision number. Otherwise, the operand will be considered an integer, and the result will be an integer. Note that this does not affect the variable type of each operand itself, and the only change is how the operand is handled in the calculation process.

$foo = "0"; $foo is a string with a value of "0" (ASCII 48)

$foo + +; $foo is a string with a value of "1" (ASCII 49)

$foo + 1; $foo is now an integer (2).

$foo = $foo + 1.3; $foo is now a double-precision number (3.3).

$foo = 5 + "Little piggies"; $foo is an integer (15)

$foo = 5 + "Small Pigs"; $foo is an integer (15)

If you think the last two expressions in the above example look a little odd, see the "Convert String" section.

If you want to force a variable to be counted as a fixed type, see the "type coercion (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 itself determines the type of variables and generally converts them as needed, the type of a particular variable is not obvious at any time. PHP includes a number of 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 coercion (casting)

Type coercion in PHP is likely to be similar to the C language: the type to be required is written in the parentheses in front of the stronger variable.

$foo = 10; $foo is an integer

$bar = (double) $foo; $bar is a double precision number

The following mandatory methods are allowed:

(int), (integer) – Force integer

(real), (double), (float) – Force double

(string) – Force as String

(array) – Force a group of

(object) – Force objects

Note that tab characters (tabs) and spaces (spaces) are allowed in parentheses, so the following statements are equivalent:

$foo = (int) $bar;

$foo = (int) $bar;

String conversion

When a string is computed as a numeric value, his result and type are determined as described below.

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

The value of this string is determined by the first part of the word. If this string starts with data of any valid number, the numeric data is the value of the string participating in the operation. Otherwise, the value is 0 (zero). Valid digital data is followed by the following tags, followed by one or more digits (can contain a decimal point), followed by an optional exponent. An exponent is made up of one or more numbers following the ' e ' or ' e '.

$foo = 1 + "10.5"; $foo is a double precision number (11.5)

$foo = 1 + " -1.3e3"; $foo is a double precision number (-1299)

$foo = 1 + "bob-1.3e3"; $foo is an integer (1)

$foo = 1 + "BOB3"; $foo is an integer (1)

$foo = 1 + "Small Pigs"; $foo is an integer (11)

$foo = 1 + "Little piggies"; $foo is an integer (11);

This string includes the character ' E '

For more information, refer to the UNIX manual section on strtod (3).

Related Article

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.