PHP Learning Note (iii): Data type conversions and constants introduction, Learning Note constants _php Tutorial

Source: Internet
Author: User
Tags scalar

PHP Learning Notes (iii): Data type conversions and constants introduction, Learning Note constants


One, PHP data types convert each other

1. Forced conversion
Copy the Code code as follows:
Bool,int,float,string,array,object,null
BOOL Settype (mixed $var, string $type)

1) Change the type of the original variable
Copy the Code code as follows:
$a = "123a"; String
Settype ($a, "int"); Do not omit double quotes
Var_dump ($a);

2) defined before assignment, does not change the original variable type
Copy the Code code as follows:
$a = "123ABC";
$b = (int) "123ABC";
Var_dump ($a);

Tips: Avoid memory overflow, integer 4 bytes (2.147E9), float 8 bytes

2. Automatic conversion

Automatically converted according to the running environment, such as the plus sign "+". If any one operand is a floating-point number, all operands are treated as floating-point numbers, and the result is a floating-point number.
Copy the Code code as follows:
$foo = "0″; $foo is a string (ASCII 48)
$foo + = 2; $foo is now an integer (2)
$foo = $foo + 1.3; $foo is now a floating point number (3.3)
$foo = 5 + "Ten Small Pigs"; $foo is an integer (15)

3. Variable test function
Copy the Code code as follows:
Is_bool (), Is_numeric (), Is_float (), Is_int () ...
Is_scalar ()//detect if the variable is a scalar

Second, the Declaration and use of constants
Copy the Code code as follows:
Cannot be changed after definition, can be accessed anywhere
Default case sensitive, used to capitalize
Constant values can only be scalar (int, float, string, bool)
Constants must be declared when the value is assigned
Define ("Constant name", "value")

Checks if a constant exists
Copy the Code code as follows:
bool Defined (string $name)
Var_dump (defined (' a ')); Do not omit the quotation marks

http://www.bkjia.com/PHPjc/985258.html www.bkjia.com true http://www.bkjia.com/PHPjc/985258.html techarticle PHP Learning Note (c): Data type conversion and constant introduction, learning Note constants One, PHP data types convert each other 1, the cast copy code code is as follows://Bool,int,floa ...

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