Bug #71099 [Com]: Basic assertion failing in PHP 7

From: Date: Sat, 12 Dec 2015 01:27:41 +0000
Subject: Bug #71099 [Com]: Basic assertion failing in PHP 7
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197821@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71099&edit=1 ID: 71099 Comment by: cdtreeks at gmail dot com Reported by: cdtreeks at gmail dot com Summary: Basic assertion failing in PHP 7 Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: CentOS 7 PHP Version: 7.0.0 Block user comment: N Private report: N New Comment: I hadn't come across filter_input() before but that's exactly what I was after. Thanks Previous Comments: ------------------------------------------------------------------------ [2015-12-12 00:08:58] rasmus@php.net This is really what the filter functions are for. if (filter_var($_GET['val_1'], FILTER_VALIDATE_INT)) { ... } or better: if (filter_input(INPUT_GET, 'val_1', FILTER_VALIDATE_INT)) { ... } ------------------------------------------------------------------------ [2015-12-12 00:05:37] cdtreeks at gmail dot com ...or I suppose I could always do: <?php if (is_numeric($val_1) && !is_float($val_1)) { ... } ------------------------------------------------------------------------ [2015-12-12 00:00:39] cdtreeks at gmail dot com Perhaps it would have helped to mention the context here as I'm trying to validate $_POST data. For example a user submitting a value of 100 in a form. When PHP receives this it'll be a string. For strict form validation, I was trying to make sure that int fields were only even processing an int response. is_int() can't be used as when being used on a string, you'll get false. Is numeric can help, but this would also return true for a float. Seems the best way around it is a bit of a hack: <?php <?php $val_1 = 'test_string'; // Value is 'test_string' $val_2 = (string) (int) $val_1; // Value is '0' - This MUST be cast back to a string after typecasting the value to an int if ($val_1 == $val_2) { // This now works as expected as we're comparing two strings } else { ... } ------------------------------------------------------------------------ [2015-12-11 23:51:41] rasmus@php.net You want === there otherwise you are comparing an int to a string and type coercion is going to make both sides int. The integer value of your string is 0 so 0 == 0. ------------------------------------------------------------------------ [2015-12-11 23:51:24] nikic@php.net This is how PHP has always worked, nothing to do with PHP 7. The "logic" behind it is that an integer to string comparison will cast the string to an integer, thus giving you the result you observe. I think this is generally agreed to be not very useful, but also unlikely to change due to BC concerns. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=71099 -- Edit this bug report at https://bugs.php.net/bug.php?id=71099&edit=1

« previous php.bugs (#197821) next »