up vote 2 down vote favorite
share [g+] share [fb]
<?php
$this_is_my_array = array("John","Johan");
for(int i = 5; i < 5; i++){
echo "$this_is_my_array[i] ";
}
//Adding name Markus
array_push($this_is_my_array,"Markus");
for(int i = 5; i < 5; i++){
echo "$this_is_my_array[i] ";
}
//Removing name from array
$this_is_my_array2= array_pop($this_is_my_array);
for(int i = 5; i < 5; i++) {
echo "$this_is_my_array2[i] ";
}

I just playing/learning php but this code gives me error.

PHP Parse error: parse error, expecting ';' in C:\main.php php on line 3

What im going wrong?

link|improve this question
feedback

5 Answers

Variable i should be $i. Lose the quotes when outputing the array, not needed.

Do this:

for($i = 5; $i < 5; $i++)
link|improve this answer
feedback
for(int i = 5; i < 5; i++){

should be

for($i = 5; $i < 5; $i++){

You're mixing in C-style syntax. Variables should always begin with $

link|improve this answer
still giving me same error – Sir you are a idoit Mar 3 '09 at 8:29
Did you also remove the 'int' declaration, or only added the $? – Daan Mar 3 '09 at 8:32
Variables must always begin with a $. – Gumbo Mar 3 '09 at 9:53
you know...aside from the parse error there's also a logic error we all missed. $i = 5; $i < 5? that loop will never run. it should start from 0. you also have an error on line 4, should be echo $this_is_my_array[$i].' '; but, indices 2,3,4 will be empty anyway...technically undefined – Mark Mar 3 '09 at 23:33
also... $this_is_my_array2 will only contain the last element in $this_is_my_array, not a copy of the array minus the last element. read the documentation ca3.php.net/manual/en/function.array-pop.php perhaps you should start learning variables and echos before you jump into arrays – Mark Mar 3 '09 at 23:37
feedback

For starters, your variable i should be $i, with a dollar sign. That's the bulk of your error. But there's a few other problems. Try this.

echo("start");

$this_is_my_array = array("John","Johan");
for($i = 5; $i < 5; $i++){
    echo $this_is_my_array[$i];
}

//Adding name Markus
array_push($this_is_my_array,"Markus");
for($i = 5; $i < 5; $i++){
echo "$this_is_my_array[$i] ";
}
//Removing name from array
$this_is_my_array2= array_pop($this_is_my_array);
for($i = 5; $i < 5; $i++) {
echo "$this_is_my_array2[$i] ";
}


echo "end";

But look at your for loop, it says

$i = 5

while $i is less than 5, do something

But $i is already 5.

link|improve this answer
still giving same error – Sir you are a idoit Mar 3 '09 at 8:28
feedback

There is no need to specify i as an integer – if you need to explicitly typecast i as an integer, you can do it like this:

$i = (int)$i;

Also, all variables must be preceded by a dollar sign $.

link|improve this answer
feedback

Your loop will never loop because $i is always 5.

Try this to get some results:

<?php
$this_is_my_array = array("John","Johan");
for($i = 1; $i < 5; $i++){
echo $this_is_my_array[$i] ;
}
//Adding name Markus
array_push($this_is_my_array,"Markus");
for($i = 1; $i < 5; $i++){
echo $this_is_my_array[i];
}
//Removing name from array
$this_is_my_array2= array_pop($this_is_my_array);
for($i = 1; $i < 5; $i++) {
echo $this_is_my_array2[i] ;
}
?>
link|improve this answer
feedback

Your Answer

 
or
required, but never shown