J.BizMai
J.BizMai

Reputation: 2777

Why the function empty seems to empty the result?

I have a weird result in PHP.

if( $mandatory_param === "name" ){
    var_dump( $data );
    exit();
}

//output : array(6) { ["name"]=> string(4) "plan" ["class"]=> string(33) "Path\Plan" ["dbtable"]=> string(5) "plans" ["getter"]=> string(7) "plan_id" ["editable"]=> string(4) "true" ["slug"]=> string(2) "pl" } 

But when I try the empty function to test the array, the result change even if the first output show this : ["name"]=> string(4) "plan"

if( $mandatory_param === "name" && empty( $data[ $mandatory_param ] ) ){
  var_dump( $data );
  exit();
}

//Output : array(0) { }

Why ? The empty() function seems to empty my array not to check if it´s empty.

Upvotes: 0

Views: 38

Answers (1)

MatsLindh
MatsLindh

Reputation: 52792

Without the complete code it's impossible to say for sure, but to make an educated guess:

When you don't have the empty() check, the exit() is triggered the first time you call the piece of code / function, and $data is set.

When you add empty, the previous call that ended up inside the if-statement does not do so any longer (since name is set for that array), and thus, doesn't produce any output or a call to exit(so the code keeps running).

The code then runs until the test is performed with an array that doesn't have $data['name'] set, performs a var_dump (on what is now an empty array) and exits.

Your call to empty does not remove anything, you're just dumping a different set of data later in your application's run.

Upvotes: 2

Related Questions