I get input values via POST, some of them might be ID’s referring to other things, and some start at 0. When choosing something with ID 0, or something without a value, is there a method like intval() that returns something more helpful than 0 on failure to parse? Or can I somehow differentiate the result of intval() from the failure to parse?
Example:
echo intval(null); // 0
echo intval("0"); // 0
You can use the
filter_var()function to determine the difference:You can also add flags to specifically accept hexadecimal and octal values, but I wouldn’t recommend that for your case.
Btw, in the more likely case that the variable comes from
$_POST, you can also usefilter_input():The reason I’m using
is_int()on the result offilter_inputis because when nothing is posted,nullis returned; usingis_int()guards against this issue.Edit
If the question is really just about
nullvs'0'you can just compare$var !== null: