Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this code:

$numPage = 10;
for($i = 0; $i < $numPage; $i++) {
 echo "Current Page: ".$i+1."/$numPage<br>";

}

why I am getting this error:

Parse error: syntax error, unexpected '"', expecting ',' or ';' on line 4

I concatenate correctly outside of the string, what am I doing wrong? Thanks

share|improve this question

closed as too localized by NikiC, cryptic ツ, PeeHaa, Charles, Gordon Mar 12 at 20:43

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

7 Answers

up vote 3 down vote accepted

Simply add parantheses: echo "Current Page: ".($i+1)."/$numPage<br>";

share|improve this answer
thanks it works – Tech4Wilco Oct 19 '11 at 18:01
Glad I could help – deviousdodo Oct 19 '11 at 18:02

Special case. The tokenizer expected a float constant here:

 +1."

This is not seen as a one and the concatenation dot, but as the leading 1. of a 1.003 for example.
Note that you don't need any whitespace or extra parens if you wrote:

 echo "Current Page: ".$i+1.0."/$numPage<br>";

The error message is a little misleading.


Note, that this just avoids the parsing error. This might still lead to an invalid result.

share|improve this answer
1  
You still need the parenthesis ;) But still +1 for noticing that this is actually a lexing issue ;) – NikiC Oct 19 '11 at 18:03
@NikiC You don't need parenthesis (to get it to parse -- "working" is another story). A space before the . would "fix" the syntax error as well. +1 for the explanation of why it is a syntax error: moral of the story: write readable code -- it matters to the computer as well ;-) – user166390 Oct 19 '11 at 18:45

+ and . have the same operator precedence and are left associative in PHP, so PHP interprets it as:

echo (("Current Page: ".$i)+1)."/$numPage<br>";

What you want is:

echo "Current Page: ".($i+1)."/$numPage<br>";

Or:

echo "Current Page: ", $i+1, "/$numPage<br>";
share|improve this answer
+1 For explaining the precedence. However, this does not explain the syntax error -- the fixed code just happens to bypass the issue. This contains no syntax error: echo "Current Page: " . $i+1 . "/$numPage<br>"; even if it will fail to "work" as expected ;-) – user166390 Oct 19 '11 at 18:47

Check "1." it may be treating it as a float. Use ($i+1) instead.

share|improve this answer

Spacing out the concatenation seems to work.

<?php
$numPage = 10;
for($i = 0; $i < $numPage; $i++) {
 echo "Current Page: " . $i+1 . "/$numPage<br>";

}
?>
share|improve this answer

Write it like that:

$numPage = 10;

for($i = 0; $i < $numPage; $i++)
{
    echo "Current Page : " . ($i + 1) . " " . $numPage . "<br />";
}
share|improve this answer

Try this:

echo "Current Page: ".($i+1)."/$numPage<br>";

Order of operations doesn't apply just to math, but programming as well. Your code was saying:

  1. append $i to the string
  2. then add 1, converting the string to an integer (0) and resulting in integer (1)
  3. then you were attempting to concatenate a string to an integer (1)
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.