-2

I have few lines of code that I think will work normally in any language. But this is not working in PHP. In my case I want to print numbers in ascending order. The code I have written is below:

$i = 0;
printf("<p>Numbers in Ascending Order : ");
for (;++i <= 10;) {
    printf("%3d", $i);
    printf("\n\n");
} 

But I get a syntax error which is given below:

Parse error: syntax error, unexpected '<=' (T_IS_SMALLER_OR_EQUAL), expecting

Why is PHP displaying an error message like this ?

dwerty
  • 17
  • 1
  • 9

4 Answers4

2

You are missing $ in for loop variable

it should be:

$i = 0;
printf("<p>Numbers in Ascending Order : ");
for (;++$i <= 10;) {
        ^
    printf("%3d", $i);
    printf("\n\n");
} 
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

In php all variable names start with $ character. On your loop there is plain i. Add $ sign and it will work.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
daFool
  • 1
  • 1
  • 2
0

You missed $ in your variable.Replace ++i with ++$i.It should be like this:

$i = 0;
printf("<p>Numbers in Ascending Order : ");
for (;++$i <= 10;) {
    printf("%3d", $i);
    printf("\n\n");
} 
Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
0

The $ sign is missing from the i variable in the loop. In PHP the $ sign goes everywhere not just in variable decleration

    $i = 0;
    printf("<p>Numbers in Ascending Order : ");
    for (;++$i <= 10;) {
       printf("%3d", $i);
       printf("\n\n");
    } 
dimlucas
  • 5,040
  • 7
  • 37
  • 54