tags:

views:

140

answers:

5
+2  Q: 

PHP Parse error

<?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?

+1  A: 

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.

gargantaun
still giving same error
+1  A: 

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

Do this:

for($i = 5; $i < 5; $i++)
Stiropor
+1  A: 
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 $

Mark
still giving me same error
Did you also remove the 'int' declaration, or only added the $?
Daan
Variables *must* always begin with a $.
Gumbo
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 beecho $this_is_my_array[$i].' ';but, indices 2,3,4 will be empty anyway...technically undefined
Mark
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 http://ca3.php.net/manual/en/function.array-pop.phpperhaps you should start learning variables and echos before you jump into arrays
Mark
+1  A: 
Bryson
A: 

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] ;
}
?>
Armadillo