|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-12-04 14:26 UTC] spam2 at rhsoft dot net
Description: ------------ declare(strict_types=1); is nice but not helpful when you get from sql-queries *anything* as string and so need to use (int)$row['id_where_i_know_db_type'] on the database server you have int, longint, smallint, tinyint which are clearly int - but why is that information lost and anything casted to a string? PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 08:00:02 2025 UTC |
> Integers in database may not match PHP internal > date types. e.g. PHP's int is only signed 32 bits > int under 32 bit architecture but how is that different to $row['id'] = (int)$row['id'] which you now must do manually becaus eotherwise sooner or later a function in a class with declare(strict_types=1); will end in a fatal error? __________________________ inlcude('functions.php'); $row = mysqli_fetch_result($result); foo($row['id']); is a fatal error currently without foo((int)$row['id']) __________________________ functions.php: <?php declare(strict_types=1); function foo(int $id) ( ) ?>If there is any chance, that an integer retrieved from a database won't fit into a PHP int, the proper way to deal with this would be something like: <?php if ($row['id'] >= PHP_INT_MIN && $row['id'] <= PHP_INT_MAX) { foo((int) $row['id']); } else { // uhm, we can't cast $row['id'] to int … }