// EDIT: The end goal is to detect whether user-submitted value is a string, or an integer. Because the value is obtained from form input, it is cast as string even when the value provided is an integer number.
I am performing a simple comparison to detect if a string variable is equal to its integer value. The value is passed from an HTML form field.
When the string value is a single letter, it evaluates to true, when it should not.
if ( ($val == (int)$val ) && strlen( $val ) == strlen( (int)$val ) ){
}
I've also tried using intval() rather than casting int variable type.
It is always evaluating as true. But r does not == 0, so how can this be?
$val = 'r';
echo 'Does $val = (int)$val? ' . ($val == (int)$val);
echo '<br/><br/>';
echo '$val was: ' . $val . '<br/>';
echo '(int)$val was: ' . (int)$val;
Output:
Does $val = (int)$val? 1
$val was: r
(int)$val was: 0
==, values are implicitely cast. it's the same as(int)$val == (int)$val. thing is: what are you even trying to achieve with your code? - Franz Gleichmannuser_id = $val) or whether it's string (user_nameLIKE '%val%'`) - Patrick Moore($val == (int)$val)be($val = (int)$val)? or are you trying to use a ternary operator here? - Funk Forty Ninerif (is_numeric($searchval))when I do a string or ID entry search. - IncredibleHat