Introduction to the method of PHP server variable setting

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

Variable type change (juggling)

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 是一个字符串 ,值是“0”(ASCII 48)
$foo++; // $foo 是一个字符串,值是“1”(ASCII 49)
$foo += 1; // $foo 现在是一个整数(2)了
$foo = $foo + 1.3; // $foo 现在是一个双精度数(3.3)了
$foo = 5 + "10 Little Piggies"; // $foo 是一个整数 (15)
$foo = 5 + "10 Small Pigs"; // $foo 是一个整数 (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 是双精度数 (11.5)
$foo = 1 + "-1.3e3"; // $foo 是双精度数(-1299)
$foo = 1 + "bob-1.3e3"; // $foo 是整数 (1)
$foo = 1 + "bob3"; // $foo 是整数 (1)
$foo = 1 + "10 Small Pigs"; // $foo 是整数 (11)
$foo = 1 + "10 Little Piggies"; // $foo 是整数 (11);
// 这个字符串包括字符'e'

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

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.