0

I have read Best way to clear a PHP array's values and nobody suggested

$array = false;

I have used it in my code to recycle an array and it seems to work fine.

Are there any reasons why it shouldn't be used?

Dave White
  • 141
  • 7
  • 1
    You can try `$array = array();`, This will allow user to avoid warnings for all array related function performed with your array variable. Hence I hope this is the best method – Ajith Jan 06 '20 at 11:15
  • you mean, [this](https://stackoverflow.com/a/28961839/7611062) is said by no one? – treyBake Jan 06 '20 at 11:15
  • 2
    Does this answer your question? [Best way to clear a PHP array's values](https://stackoverflow.com/questions/10261925/best-way-to-clear-a-php-arrays-values) – Mark Jan 06 '20 at 11:15
  • Yes, sorry, I mean false not null. – Dave White Jan 06 '20 at 11:15
  • @MarkOverton He has already shared that link in Question. – Amanjot Kaur Jan 06 '20 at 11:16
  • 1
    I mean it won't reset the array, it will just change the datatype to a boolean with value false – treyBake Jan 06 '20 at 11:16
  • @Ajith that does not answer my question. – Dave White Jan 06 '20 at 11:16
  • You can check https://stackoverflow.com/questions/5491605/empty-arrays-seem-to-equal-true-and-false-at-the-same-time – Amanjot Kaur Jan 06 '20 at 11:18
  • `$array = [];`. You now have an empty array. Best not to type juggle, why would an array be false (boolean)? – delboy1978uk Jan 06 '20 at 11:24
  • @delboy1978uk thanks, yes it's now empty and the code works. Any reason I shouldn't be using it? – Dave White Jan 06 '20 at 11:31
  • 1
    its just good practice to not type juggle, and to be more strict. for instance `5 == '5'` is true, but `5 === '5'` is false – delboy1978uk Jan 06 '20 at 11:34
  • 2
    @DaveWhite "Are there any reasons why it shouldn't be used?" mainly because you no longer have an array in that variable, you have a boolean instead. So if you then pass this variable to any other code which is expecting to be able to do array-like operations on it, you'll have problems. PHP is loosely-typed, but that doesn't mean you should change the types of your variables arbitrarily. If you want an empty array then create an empty array, not a boolean. – ADyson Jan 06 '20 at 11:39

0 Answers0