![]() Perhaps writing this will save me (and hopefully others) from this mistake in the future. I’ve lost count the number of times I’ve been bitten by this over the years. If you’ve been writing PHP long enough you’ve likely run into this problem, it’s an easy one to get tripped on. That will remove any trailing spaces from $test_me before the empty check, so strings that consist of just spaces (or space like characters) will truly become empty in the PHP sense. Such exceptions are caught with PHP catch block. And, try block contains code with the feature of PHP exception handling by throwing exceptions that occurred while executing PHP scripts. Not the end of the world, just means you have to write it like this: PHP try-catch is implemented with try and catch blocks, as usual. In other words, the following will not work: empty(trim($name)). Note: empty() only checks variables as anything else will result in a parse error. The empty function can only check variables, thankfully this is noted on the empty docs page: So how do you deal with this? PHP provides an rtrim function to remove trailing spaces (and space line items) and you might be tempted to try this:īut that won’t work. To people that looks empty, to PHP, not so much.Ī good approach, until $a_thing includes our friend the string of spaces. Unfortunately it didn’t expect the condition where the value might just be a string full of spaces. If empty returned TRUE then continue was called to skip work on that item and go on to the next. An empty check was being done inside a foreach() loop to see if any work on that item in the array really needed to be done. This is exactly what happened in the bug I saw. If you said ‘This is empty!’, well, I can understand why you’d think that, but that turned out to be the wrong answer. If you said ‘NOT empty’ was the result, you are correct. Where $test_me is set to a string that contains a single space character. So with all that in mind, what would you expect from the following code: Var $var (a variable declared, but without a value in a class) ![]() The following things are considered to be empty: Returns FALSE if var has a non-empty and non-zero value. The empty doc page mentions what PHP considers “empty”: Since PDOException returns the error code as a string, you need a constructor like the one below if you wish to rethrow the PDOException as a custom exception. The empty function on the other hand looks at the value of a variable to see if it has been set to something that could be considered “empty”. Other than the NULL check isset doesn’t make any determinations on what value the variable holds. This is the relevant RFC, which was voted favourably 48-1. The isset function only checks to see if a specific variable was been created and has a value other than NULL. 48.8k 46 174 261 Add a comment 6 Answers Sorted by: 39 Starting with PHP 8, it is possible to use a non-capturing catch. I just came across a bug in some code that was using empty() to check for something so I thought this was a good time to remind others about this.įirst a quick refresher course on isset() and empty(). It can be really easy to get tripped up by PHP’s empty and isset functions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |