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:
- null is considered an empty string
- a white space only string is considered empty
- that "0" is not considered empty
This is what I’ve got so far:
$question = trim($_POST['question']); if ("" === "$question") { // Handle error here }
There must be a simpler way of doing this?
A literal answer to the question from the post title is quite obvious and straightforward:
But for input variables, which you’re actually asking about, it would be both superfluous and insufficient.
A
$_POSTarray element cannot be null, hence checking for null would be superfluous. At the same time, any outside variable can be absent, which will raise an error and you probably want to avoid it. Therefore, given you need a$questionvariable, the code would be