Home > Blockchain >  Value of type bool is not callable PHP
Value of type bool is not callable PHP

Time:10-03

I'm having a bit of trouble with this simple rock paper scissors game I'm making.

So I want to make a the computer chose a random of the 3. This is how to code looks.

128 $array = array("Rock","Paper","Scissors");
129 $shuf = shuffle($array);
130 $computer = $shuf(0);

When I fire the website up using xampp I get an error messeage looking like this.

Fatal error: Uncaught Error: Value of type bool is not callable in on line 130

Any solution to this? Thanks!

CodePudding user response:

The docs says that shuffle modifies the input array and returns true on success and false on failure. So, you need to check its result and if so, perform your logical operations on the input array, that was passed by reference and modified while the function was being executed:

$shuf = array("Rock","Paper","Scissors");
if (shuffle($shuf)) { //success
    $computer = $shuf[0]; //$shuf was modified during the shuffling
} else {
    //the shuffling failed, you can handle failure here
}

CodePudding user response:

Gotcha! Thanks a lot folks. It works now :D

  • Related