There is no use for converting an integer to a string when you just need to display the result or for simple checking against a hardcoded known datatype value. Converting datatypes is only useful when you need to validate values strickly against a certain specific datatype.
Your issue is that you do not exactly know how to test your values. 0
, either an an integer or string value, constitutes as empty and null (when loosely compared with==
) in PHP. If you need to check any variable if it is empty or having a value, including 0
as string or integer value, then you need to adjust your operators.
For you to specifically check and disallow/allow 0
as a valid/non-valid value, you need to use strick comparison, and here datatype counts. Just quickly 0 === "0"
will be false as integer 0
is not exactly the same as string "0"
Just a note here, your conditional statement is wrong and this is where everything fails. When checking 0
strictly (===
) with NULL
or is_null
, the result will be false, and checking 0
with empty()
will return true, which will execute your ||
operator in your conditional statement.
In your usecase, you can do the following to allow integer value 0
as a valid value and all other values, and disallow an empty string
if ( !$approved_comments_count // Universal check to see if empty, will consider false as empty too
&& 0 !== $approved_comments_count // Test for integer 0
) {
echo '$approved_comments_count is a true empty string';
} else {
echo 'The value of $approved_comments_count is ' . $approved_comments_count;
}
or even better
if ( $approved_comments_count // Allows anything that does not equal empty
|| 0 === $approved_comments_count // Test for integer 0
) {
echo 'The value of $approved_comments_count is ' . $approved_comments_count;
} else {
echo '$approved_comments_count is a true empty string;
}
If 0
where to be a string, you would test that 0
as follow
'0' === $approved_comments_count
or
"0" === $approved_comments_count