PHP server variable Setting method Introduction _php Tutorial

Source: Internet
Author: User
Tags php server type casting
variable type change (juggling)

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

An example of a PHP automatic conversion variable type is the operator ' + ' of the addition. If any one operand is a double-precision number, then all operands are evaluated as double-precision numbers, and the result is 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 during the calculation.

$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 + "Ten Little piggies"; $foo is an integer (15)

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

If you think the last two expressions in the example above look a bit strange, look at the "Conversion of Strings" section.

If you want to force a variable to be evaluated as a fixed type, see the section "type coercion (casting)". 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 types of variables and generally translates them as needed, the type of a particular variable is not obvious at any time. PHP includes some functions to find out the type of the variable. These functions are GetType (), Is_long (), is_double (), is_string (), Is_array (), and Is_object ().

Type coercion (type casting)

Type coercion in PHP is similar in C: Write the kind of type you want in parentheses in front of a strong variable.

$foo = 10; $foo is an integer

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

The following enforcement methods are allowed:

(int), (integer) – Force integer

(real), (double), (float) – Force double-precision number

(string) – forced into a string

(array) – Force an array

(object) – coerced into an object

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

$foo = (int) $bar;

$foo = (int) $bar;

String conversions

When a string is evaluated 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-type variable, otherwise it is considered 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, then the numeric data is the value of the string participating in the operation. Otherwise, the value is 0 (zero). Valid numeric data follows these tags, followed by one or more digits (which 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 the double precision number (11.5)

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

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

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

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

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

This string includes the character ' E '


http://www.bkjia.com/PHPjc/446697.html www.bkjia.com true http://www.bkjia.com/PHPjc/446697.html techarticle variable type change (juggling) PHP does not need (or is not supported) to define his variable type in the fame variable; the type of a variable is determined by the relationship that the variable is used in, and ...

  • 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.