Strange PHP error





  • I have this:

    srand(time());	//seeding the random generator
    	
    	//generate alt to the capcha
    	$digits[] = array(rand()%9,rand()%9);
    	$operator = rand()%3;
    	//0	=	addition
    	//1	=	subtraction
    	//2	=	mutiplication
    	$answer = -1;
    	switch($operator)
    	{
    		case 0:
    			$answer = digits[0] + digits[1];     /where error is showing
    			$operator = '+';		//abuse of softcasting I know.
    			break;					//wonders if it will still work with me changing the switch variable in the switch struct?
    		case 1:
    			$answer = digits[0] - digits[1];
    			$operator = '-';
    			break;
    		case 2:
    			$answer = digits[0] * digits[1];
    			$operator = '*';
    			break;
    		default:
    			die("Someones been fucking with my scripts");
    	}
    

    And it's telling me, Parse error: syntax error, unexpected '['
    Can someone help me with it? I don't see how that's an error!



  • @malfist said:



    I have this:

    srand(time());	//seeding the random generator

    //generate alt to the capcha
    $digits[] = array(rand()%9,rand()%9);
    $operator = rand()%3;
    //0 = addition
    //1 = subtraction
    //2 = mutiplication
    $answer = -1;
    switch($operator)
    {
    case 0:
    $answer = digits[0] + digits[1]; /where error is showing
    $operator = '+'; //abuse of softcasting I know.
    break; //wonders if it will still work with me changing the switch variable in the switch struct?
    case 1:
    $answer = digits[0] - digits[1];
    $operator = '-';
    break;
    case 2:
    $answer = digits[0] * digits[1];
    $operator = '*';
    break;
    default:
    die("Someones been fucking with my scripts");
    }

    And it's telling me, Parse error: syntax error, unexpected '['
    Can someone help me with it? I don't see how that's an error!

     

    all variables in php need a dollar sign before them.  



  • shit

    headdesk 



  • Please, some mod, delete this thread and save me the horror!



  • Fixed that, now it's telling me:

    Fatal error: Unsupported operand types

    @the second case, first statement. 

     

    edit: line: $answer = $digits[0] - $digits[1]; 

    second edit: it's on the first case now. 



  • sorry i'm not psychic.

    But here.

     

        $digits    = array(rand(0,10),rand(0,10));
        $operators = array('','-','+');
        $operator  = $operators[rand(0,count($operators)-1)];
        switch ($operator) {
            case '+':
                    $answer = $digits[0] + $digits[1];
                break;
            case '-':
                    $answer = $digits[0] - $digits[1];
                break;
            case '
    ':
                    $answer = $digits[0] * $digits[1];
                break;
        }
       
       
        print $digits[0]." $operator ".$digits[1]." = ";
        print $answer;
     

     

    - edit -

    spotted it

    you wrote

    $digits[] = array(rand(),rand()) 

    which means you put a array in a array, because you can implicitly add items to a array by assigning something to $my_variable[]   



  • God damnit! The second errors I was complaining about is occurring in an import! I should really get some sleep...



  • @malfist said:

    God damnit! The second errors I was complaining about is occurring in an import! I should really get some sleep...

    If this isn't your first hour/day of php, yes you probably should,



  • No, but it's the first phping I've done in maybe 6 months or so.

    I'm going to bed, no more coding for me tonight. 



  • Once it took me over 15 mintues to see the error in:

    for(i=0; i<something; i++) { /PHP/

    It was more than halfway into a 1MB-of-PHP-code project, and possibly around my 2000th line for the day. I actually ended up calling a colleague when I realized I wouldn't be able to find the error on my own.



  • @malfist said:

    Please, some mod, delete this thread and save me the horror!

    Sorry, can't do that, unless you offer me a pallet of cheap Chinese Nike shoes.



  • @ammoQ said:

    @malfist said:
    Please, some mod, delete this thread and save me the horror!
    Sorry, can't do that, unless you offer me a pallet of cheap Chinese Nike shoes.

    You forgot the corpse of the guy who sold them to you. Unless you meant the other type of shoes.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.