对不起 [英] Butt ugly
问题描述
来自汇编程序/ C / C ++ / C#背景我得说这很难看:
<?php
echo" 2 + 2 = 。 2 + 2; //这将打印4
echo" 2 + 2 =" ,2 + 2; //这将打印2 + 2 = 4
echo" test" 。 2 + 2; //这将打印2
?>
R.
< blockquote> Rik G.写道:
来自汇编程序/ C / C ++ / C#背景我得说这是丑陋的:
<?php
echo2 + 2 = 。 2 + 2; //这将打印4
echo2 + 2 = ,2 + 2; //这将打印2 + 2 = 4
回声测试 。 2 + 2; //这将打印2
?>
R.
如果您了解优先权,则不会。请记住,''。''具有很高的优先级。
完成所有工作后:
<?php
echo" 2 + 2 =" 。 (2 + 2); //这将打印2 + 2 = 4
echo" 2 + 2 =" ,(2 + 2); //这将打印2 + 2 = 4
echo" test" 。 (2 + 2); //这将打印测试4
?>
-
============== ====
删除x来自我的电子邮件地址
Jerry Stuckle
JDS计算机培训公司
js ******* @ attglobal.net
==================
" Jerry Stuckle" < JS ******* @ attglobal.net>在消息中写道
news:pb ******************** @ comcast.com ...Rik G.写道:来自汇编程序/ C / C ++ / C#背景我得说这是屁股
丑陋:
<?php
echo" ; 2 + 2 = 。 2 + 2; //这将打印4
echo2 + 2 = ,2 + 2; //这将打印2 + 2 = 4
回声测试 。 2 + 2; //这将打印2
?>
R.
如果您了解优先权,则不会。请记住,''。''具有很高的优先级。
以下所有工作:
<?php
echo" 2 + 2 = 。 (2 + 2); //这将打印2 + 2 = 4
echo2 + 2 = ,(2 + 2); //这将打印2 + 2 = 4
回声测试 。 (2 + 2); //这将打印测试4
?>
好的,谢谢,看起来更好但仍然:为什么它会吃掉字符串2 + 2
="和测试在案例1和3中?
这里有一些甚至更丑陋的东西:
echo" 2 + 2 =" 。 2 + 3; //这将打印5
echo" 2 + 2 =" ,2 + 3; //这将打印2 + 2 = 5
echotest 。 2 + 3; //这将打印3
Jerry Stuckle写道:Rik G.写道:来自汇编程序/ C / C ++ / C#背景我得说这是丑陋的:
<?php
echo" 2 + 2 =" 。 2 + 2; //这将打印4
echo2 + 2 = ,2 + 2; //这将打印2 + 2 = 4
回声测试 。 2 + 2; //这将打印2
如果您了解优先权,则不会。请记住,''。''具有很高的优先级。以下所有工作:
<?php
echo" 2 + 2 =" 。 (2 + 2); //这将打印2 + 2 = 4
echo2 + 2 = ,(2 + 2); //这将打印2 + 2 = 4
回声测试 。 (2 + 2); //这将打印测试4
不仅优先,类型杂耍也在这里起主要作用。
详细说明:
echo" 2 + 2 =" 。 2 + 2; //这将打印4
1.字符串2 + 2 =变为2 + 2 = 2 (确实优先)。
2.尝试在此字符串中添加一个数字,将其转换为2(第一个数字),
并添加2,因此给出4. br />
echo" 2 + 2 =" ,2 + 2; //这将打印2 + 2 = 4
字符串2 + 2 =从
加上2 + 2
echo" test"得到的整数中单独回应。 2 + 2; //这将打印2
1.字符串test变成测试2。
2.试图将数字2添加到测试2中。将字符串转换为整数
(0),并且添加2确实给出了
变量变量,没有严格类型,在某些情况下是一种祝福,在
其他人中,这令人非常恼火。你只需要留意它:-)。
阅读材料:
http://nl3.php.net/manual/en/languag...e-juggling.php
http://nl3.php.net/manual /en/language.operators.php
表15-1
如有疑问,请将一段代码转换为某种类型(类型)(例如
(bool),(string),(int)等。)
Grtz
- -
Rik Wasmus
Coming from an Assembler/C/C++/C# background I got to say this is butt ugly:
<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
echo "test " . 2+2; // This will print 2
?>
R.
Rik G. wrote:Coming from an Assembler/C/C++/C# background I got to say this is butt ugly:
<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
echo "test " . 2+2; // This will print 2
?>
R.
Not if you understand precedence. Remember, ''.'' has a high precedence. The
following all work:
<?php
echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
echo "test " . (2+2); // This will print test 4
?>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:pb********************@comcast.com...Rik G. wrote:Coming from an Assembler/C/C++/C# background I got to say this is butt ugly:
<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
echo "test " . 2+2; // This will print 2
?>
R.
Not if you understand precedence. Remember, ''.'' has a high precedence.
The following all work:
<?php
echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
echo "test " . (2+2); // This will print test 4
?>
OK, thanks, that looks better but still: why does it eat the strings "2 + 2
= " and "test " in case 1 and 3?
And here''s for something even but uglier:
echo "2 + 2 = " . 2+3; // This will print 5
echo "2 + 2 = " , 2+3; // This will print 2 + 2 = 5
echo "test " . 2+3; // This will print 3
Jerry Stuckle wrote:Rik G. wrote:Coming from an Assembler/C/C++/C# background I got to say this is
butt ugly:
<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
echo "test " . 2+2; // This will print 2
Not if you understand precedence. Remember, ''.'' has a high
precedence. The following all work:
<?php
echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
echo "test " . (2+2); // This will print test 4
Not only precedence, type juggling also plays a major part here.
Detailed:
echo "2 + 2 = " . 2+2; // This will print 4
1. The string "2 + 2 = " becomes "2 + 2 = 2" (indeed precedence).
2. Trying to add a number to this string casts it to 2 (the first number),
and adds 2, so gives 4.
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
The string "2 + 2 = " gets echoed seperately from the integer resulting from
adding 2+2
echo "test " . 2+2; // This will print 2
1. The string "test " becomes "test 2".
2. Trying to add the number 2 to "test 2" casts the string to an integer
(0), and adding 2 gives indeed 2
Variable variables, without strict type, are a blessing in some cases, in
others it''s immensely irritating. You just have to keep an eye on it :-).
Reading material:
http://nl3.php.net/manual/en/languag...e-juggling.php
http://nl3.php.net/manual/en/language.operators.php
Table 15-1
When in doubt, cast the piece of code to a certain type by (type) (e.g.
(bool), (string), (int) etc.).
Grtz
--
Rik Wasmus
这篇关于对不起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!