开发者

function().function() VS function()function()

开发者 https://www.devze.com 2022-12-28 09:18 出处:网络
I was writing a foreach Loop in PHP5. Here is the script: foreach(range(\'A\',\'Z\') as $char) // l开发者_高级运维ine 1

I was writing a foreach Loop in PHP5. Here is the script:

foreach(range('A','Z') as $char) // l开发者_高级运维ine 1
{ // line 2
echo strtoupper($char)strtolower($char); // line 3
} // line 4

And I got this error message Parse error: parse error, unexpected T_STRING in testing.php on line 3

I spent almost an hour to figure out I should add a dot between two functions like this:

echo strtoupper($char).strtolower($char);

So I can't tell the difference between these two lines of codes:

echo strtoupper($char).strtolower($char);
echo strtoupper($char)strtolower($char);


The '.' is a concatenation operator. It returns the concatenation of its right and left arguments.

For example:

'Hello ' . 'world!'

gives:

'Hello world!'

Having two functions next to each other without any operators is an error.


PHP's grammar expects the arguments of echo to be an expression or a set of expressions separated by comma.

Now strtoupper($char) is a complete expression by itself. After this expression PHP expects to see an expression separator which could be a comma, dot or even a semi-colon but when it finds a string (strtolower) it complains.

So

strtoupper($char)strtolower($char);

is not a valid expression but these are:

strtoupper($char),strtolower($char);
strtoupper($char).strtolower($char);
strtoupper($char);strtolower($char);

But in your case the last one does not server the purpose as it prints only the 26 uppercase letters and does not print the lowercase ones.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号