An explanation of conversions in PHP

Source: Internet
Author: User

The cast is divided into two types, the first of which is only temporary, and permanent conversions. In the interim conversion, the first can be shown in the first way, that is, the form of parentheses, temporarily converted to integer type we can pass(int)Are in this form, or are(integer)temporarily converted to integral type, and then to the next, temporarily to the floating-point type can be(float)or a(Double)or a(rea!)temporarily converted to a floating-point type, the third is temporarily converted to a Boolean type and can be(Boolean)or you can experiment .(BOOL)abbreviated form, we can try to write a Boolean type, and then down it can be converted to a string type. The string type can be passed(String)string type, and then can also be converted to empty, you can pass(unset)is converted to NULL, and can also be converted to an array,(Array)Converted to an array, and can also be converted to an object, but remember not to convert to a resource(object)into an object, so first, let's try this .()the form of parentheses.

to temporarily convert the type of a variable. That temporary conversion is not really a change in the type of the variable. It is possible to write a $var = 12; This is an integral type then I'll temporarily convert it to a floating-point type by var_dump (float) $var, (double) $var, (real) $var); to print more than one, it's all about putting this () The parenthesis type is placed before the variable to implement our temporary conversion.

Then let's run it and take a look at the results.

You 'll see three floats and prove it.

  code example

  <?php

  Temporary Conversion

  $var = 12;

  Var_dump (float) $var, (double) $var, (real) $var);

Then convert it temporarily to a string type, and then convert it to String type,bool type, including the full name of the boolean ,var_dump ((float) $var, (double) $ var, (Real) $var, (String) $var, (BOOL) $var, (Boolean) $var); Test it and see if it's possible .

  String12,bool true,bool true no problem, No problem, you can convert it to empty again, in var_dump ((unset) $var, (array) $var, (object) $var); This is a temporary conversion and look again.

you see an array of NULL arrays An object can be, and then you see that it's converted to other types and there's an integral type. We have no experiments, Try again to write a $string = ' 3king '; now use it to convert it into an integral type.

  Var_dump ((int) $string, (integer) $string); temporarily convert them to integral type

3 the same to remember our rules, when the string converted to integer type, is not to go to the legal part AH.

  code example;

<?php

Temporary Conversion

$var = 12;

Var_dump (float) $var, (double) $var, (real) $var, (String) $var, (BOOL) $var, (Boolean) $var);

Echo '

Var_dump ((unset) $var, (array) $var, (object) $var);

Echo '

$string = ' 3king ';

Var_dump ((int) $string, (integer) $string);

Echo '

Yes, well, at the end, we're here to experiment, and that kind of conversion to a Boolean type of false, see if you can, write a $string = '; when converting to a Boolean type, it should be converted into Var_dump ((bool) $string);

At this point you see that there is a bool type of false without problems, which is the form of our temporary conversions through this parenthesis () .

  code example

  <?php

Temporary Conversion

$var = 12;

Var_dump (float) $var, (double) $var, (real) $var, (String) $var, (BOOL) $var, (Boolean) $var);

Echo '

Var_dump ((unset) $var, (array) $var, (object) $var);

Echo '

$string = ' 3king ';

Var_dump ((int) $string, (integer) $string);

Echo '

$string = ";

Var_dump ((bool) $string);

Echo '

original link:http://www.maiziedu.com/wiki/php/coercion/

An explanation of conversions in PHP

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.