samedi 21 août 2021

Why is the foo() function executed while not having been called?

I'm kind of new to PHP so please excuse the simplicity of my question in case it was, I have a function foo() that always returns true, now my question is that while I have only checked the true being of foo() and not called the foo() function directly, how possibly did it execute then? And is only PHP like this or it is the same say in JavaScript?

 $x = 10;
function foo() {
    global $x;
    if($x = 10) {
        return true;
    }
    return false;
}

if(foo()) {
    echo 'Done foo() function!';
} else {
    echo 'Not done foo() function...';
}

Aucun commentaire:

Enregistrer un commentaire