I'd like to separate two things.
If a $_POST["something"] was sent, BUT empty.
OR
If a $_POST["something"] was not sent.
Is there a way?
I'd like to separate two things.
If a $_POST["something"] was sent, BUT empty.
OR
If a $_POST["something"] was not sent.
Is there a way?
You might be tempted to use empty(), but it blows up in your face if your possible values include a 0. Best to use something like:
if (isset($_POST['something'])) { // the form field was sent ... if ($_POST['something'] === '') { // the form field was sent, but is empty } } This works, as ALL data coming out of the _GET/_POST/_REQUEST superglobals is a string, regardless of whether it's a number or not.
==='' and for using strlen()look towards isset and empty.
http://php.net/manual/fr/function.isset.php
http://php.net/manual/fr/function.empty.php
Edit : The comments below are true. Empty should be used for empty strings, not numeric values, float values or a zero in a string​​. However, here's a function if you need to accept these as valid values:
function is_blank($value) { return empty($value) && !is_numeric($value); } empty() will not work if the post variable is set to "0" or several other values, according to your link.0. 0 == '' is TRUE, as is $x = 0; empty($x);i use the php function isset() to check if a POST was sent or not
if ($_POST["something"]) will return true if there is such field
if $_POST["something"] == "" will do the rest
'' == 0 evalutes to TRUE. 0 might be a valid value, and blindly using $_POST['something'] when it isn't set will issue warnings.