Monday, April 25, 2011

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?

From stackoverflow
  • 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.

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

    Do this:

    for($i = 5; $i < 5; $i++)
    
  • 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 $

    Daan : Did you also remove the 'int' declaration, or only added the $?
    Gumbo : Variables *must* always begin with a $.
    Mark : 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 : 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.php perhaps you should start learning variables and echos before you jump into arrays
  • 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 $.

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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.