php check for empty string
PHP: Check if variable is type of string AND is not empty string?
I need to check if the passed variable is type of string, and it is not empty. I have the following function:
The function works fine.
My question: Is there a way to improve function performance without effecting the results?
I’m talking about «micro optimization» (I use this function very heavily).
EDIT:
For those who are asking:
Note: a non-empty string = a string which if tested with strlen() function it would return > 0
5 Answers 5
Here is a simple little benchmarking script you can modify to see what works best. I just tried a few variations of the same thing, the first one is the fastest by a small margin, but they are basically all the same. And there isn’t really a simpler way for you to write it.
You can inline the entire thing:
This might be a tiny bit more performant or not, but mostly is reads a lot more sensibly.
Apart from these points, there’s not a lot more to simplify. If you want to do exactly this check of is string and is not empty, then this is the check to do. If you’re saying you’re using this a lot, perhaps you need to either embrace the dynamically typed nature of PHP and type juggling more (requires you to know exactly when what might be cast to what though), or you need to have fewer ingress points for your data where you do validation and can rely more on your done validation throughout your app.
Better way to check variable for null or empty string?
Since PHP is a dynamic language what’s the best way of checking to see if a provided field is empty?
I want to ensure that:
This is what I’ve got so far:
There must be a simpler way of doing this?
10 Answers 10
Old post but someone might need it as I did 😉
Use PHP’s empty() function. The following things are considered to be empty
For more details check empty function
I’ll humbly accept if I’m wrong, but I tested on my own end and found that the following works for testing both string(0) «» and NULL valued variables:
Which could also be reversed to test for success as such:
Beware false negatives from the trim() function — it performs a cast-to-string before trimming, and thus will return e.g. «Array» if you pass it an empty array. That may not be an issue, depending on how you process your data, but with the code you supply, a field named question[] could be supplied in the POST data and appear to be a non-empty string. Instead, I would suggest:
There is no better way but since it’s an operation you usually do quite often, you’d better automatize the process.
Most frameworks offer a way to make arguments parsing an easy task. You can build you own object for that. Quick and dirty example :
Symfony use that kind of sugar massively.
But you are talking about more than that, with your «// Handle error here «. You are mixing 2 jobs : getting the data and processing it. This is not the same at all.
There are other mechanisms you can use to validate data. Again, frameworks can show you best pratices.
Create objects that represent the data of your form, then attach processses and fall back to it. It sounds far more work that hacking a quick PHP script (and it is the first time), but it’s reusable, flexible, and much less error prone since form validation with usual PHP tends to quickly become spaguetti code.
‘0’ as a string with empty() in PHP
I want a 0 to be considered as an integer and a ‘0’ to be considered as a string, but empty() considers the ‘0’ as a string in the example below,
How can I ‘make’ empty() to take ‘0’s as strings?
12 Answers 12
You cannot make empty() take it. That is how it was designed. Instead you can write an and statement to test:
The following things are considered to be empty:
You have to add an additional other check.
You can’t with only empty(). See the manual. You can do this though:
Basically, empty() does the same as:
But it doesn’t trigger a PHP notice when the variable is not set.
PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty(), and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.
isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.
isset — Determine if a variable is set and is not NULL
In other words, it returns true only when the variable is not null. empty()
empty — Determine whether a variable is empty
In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable. is_null()
is_null — Finds whether a variable is NULL
In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.
The table in picture is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool (false).
Also I have made a custom function for checking all stuff: