tags:

views:

75

answers:

2

Hi all,

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

foreach(range('A','Z') as $char) // line 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);
+12  A: 

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.

zaf
+4  A: 

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.

codaddict