PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] Why the function empty seems to empty the result?

 October 28, 2022     is-empty, php     No comments   

Issue

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.


Solution

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.



Answered By - MatsLindh
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing