Home > Backend Development > PHP Tutorial > PHP data type conversion problem caused by a small question_PHP tutorial

PHP data type conversion problem caused by a small question_PHP tutorial

WBOY
Release: 2016-07-14 10:08:16
Original
1125 people have browsed it

The topics are as follows:

[php]
$a = 3;
$b = 5;
$c = $a > $b ? 2:4;
echo $d = $a<$c;
/*analysis why the result is 1*/
echo $d;
echo gettype($d);
echo var_dump($d);
/*example 2*/
if('ABC'==0)
echo 'yes';
else
echo 'no';
/*example 3 the result $h=10,$g=10;*/
$h=+$g=10;
echo $h,"$g";
/*This is about operator precedence*/
?>
Be confused by the results! ?
As a weakly typed language, PHP allows mixed operations on different types of data (similar to VbScript). The PHP interpretation engine automatically performs type conversion. Attention must be drawn.
First, let’s recall the PHP echo() function in the manual
Definition and usage
The echo() function outputs one or more strings.
Grammar
echo(strings)
Parameters Description
strings Required. One or more strings to send to the output.
Note: Note: echo() is not actually a function, so you don't need to use parentheses with it. However, if you wish to pass one or more arguments to echo(), then using parentheses will cause a parsing error. The echo() function can be used as echo('...') or echo '...'. The operand type of echo should be string.
For detailed usage, please refer to http://www.w3school.com.cn/php/func_string_echo.asp
In order to better understand PHP data type conversion and understand the complexity and weirdness of calculation results, please remember the following points:
1: Pay attention to the difference between echo and print functions, such as:
1),
echo "123" . print("abc"); //The syntax is correct, but the output is not 123abc, but abc1231
// The reason is that the PRINT () function outputs ABC first, and then connects the return value of "123" and the function of the function 1
print "abc" . echo('ooo'); //echo('ooo') will cause Parse error: syntax error
2) The echo() function can use simplified syntax.
For example:
[php]
$color = "red";
?>

Roses are

3) The echo() function is a little faster than the print() function.
Next, you need to be familiar with the operation of the dot operator. Here is an example from netizen "Zhang Qing (mesh)":
Example 1:
In the most general case, output a string, such as echo 'abc' . "123", the result is abc123, there is no explanation.
Example 2:
echo true;
The output result is 1. true is PHP's built-in Boolean constant, which is converted to the string "1" before output.
echo false;
The output result is empty. false is also a built-in Boolean constant in PHP and is converted to an empty string before output.
Example 3:
echo 0=="ABC";
The output result is 1. In 0=="ABC", the string "ABC" is first converted to a numeric type (there is another rule for converting a string into a numeric value, which I will talk about when I have the opportunity). When it is 0, it is then summed 0 comparison, the result is true, and then echo true is executed, it is 1. Two type conversions are performed silently.
Note again that the result of echo 'ABC'==0; is still 1. Note that 0 is not converted to the string "0" and then compared with the string 'ABC' to obtain false. Haha, is it weird?
There is another similar question:
if('ABC'==0)
echo 'yes';
else
echo 'no';
Do you know whether the output is yes or no?
Example 4: Here comes another one that’s even weirder.
echo 100 . "ABC"; // Attention! There are spaces on both sides of the dot symbol
This sentence outputs 100ABC, because 100 is converted into "100".
echo 100."ABC"; // Attention! There are no spaces on either side of the dot symbol, it is a ligature
But this sentence reports a grammatical error! Why? Because PHP treats the dot symbol here as a decimal point, "100."ABC"" is certainly not a correct number.
echo 100. "ABC";
There is only a space to the right of the dot symbol, which is a syntax error. The reason is as above.
echo 100 ."ABC";
There is only a space to the left of the dot symbol, the syntax is correct, and 100ABC is output.
Similar:
$a = 100;
echo $a . 200; // There are spaces on both sides of the dot symbol
Output 100200.
$a = 100;
echo $a.200; //There are no spaces on both sides of the dot symbol
Syntax error. "$a.200" is not a qualified variable either.
Example 5:
echo 100 + "ABC";
The output result is 100. Be careful not to think that this will cause a syntax error, because the "+" sign here is an arithmetic operator, not a miswriting of the connection operator. "ABC" is converted into the number 0, which is equal to 100 after adding 100, and then converted into the string "100" and output. Two type conversions were performed.
echo 100 + "20" . 30; // There are spaces on both sides of the dot symbol
The result output is 12030.
echo 100 + "20" .30; //There is a space on the left side of the dot symbol and no space on the right side
Syntax error. .30 is treated as 0.30 of type double. Put directly together with the result "120" of 100 + "20", PHP cannot distinguish clearly and reports an error.
You can also give some examples. If you look carefully, you can still find the rules of type conversion. PHP is actually not weird.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477788.htmlTechArticleThe title is as follows: [php] ?php $a = 3; $b = 5; $c = $a $ b ? 2:4; echo $d = $a$c; /*analysis why the result is 1*/ echo $d; echo gettype($d); echo var_dump($d); /*example 2*/ if (ABC==0)...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template