PHP type conversion problems

Source: Internet
Author: User
I wrote the following code for PHP type conversion: & lt ;? Php & nbsp; $ a & nbsp; = & nbsp; ''; & nbsp; & nbsp; // & nbsp; $ a & nbsp; = & nb PHP type conversion problem
I wrote the following code:

$ A = ''; // $ a = ""
Var_dump ($ a); // string (0 )""
Echo"
";
$ B = 1;
Var_dump ($ B); // int (1)
Echo"
";
$ A + = $ B;
Var_dump ($ a); // int (1). This is the problem. how can we make variable a of the string type?
Exit;


Excuse me:
What are the default transformation rules for PHP variables?
What is the most commonly used forced conversion function in development?
Php string
------ Solution --------------------
Var_dump (string) $ );
------ Solution --------------------
$ A + = $ B
Yes
$ A = $ a + $ B
Since you are performing arithmetic operations, the result is of course a numerical value rather than a string.

$ A. = $ B
Is the string
------ Solution --------------------
$ A = strval ($ );
------ Solution --------------------
$ A = intval ($ a); $ a = int ($ a), both of which are forcibly converted to an integer.
------ Solution --------------------

Var_dump (string) $ a); // method 1
Var_dump (strval ($ a); // method 2
Settype ($ a, "string"); // method 3
Var_dump ($ );

------ Solution --------------------
For more information, see The php Manual!
------ Solution --------------------
For example:
$ A = '1 ';
$ B = '2'; // $ B = 2; the same result is true for int.

$ A + = $ B; // enter 3
$ A. = $ B; // enter 12

It depends on what operations LZ is going to perform.

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.