note 123455 deleted from function.is-numeric by crell
| From: | crell@php.net | Date: | Mon, 18 Apr 2022 14:53:15 +0000 |
| Subject: | note 123455 deleted from function.is-numeric by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218270@lists.php.net to get a copy of this message | ||
Note Submitter: Ray.Paseur sometimes uses Gmail
----
intval() appears to be helpful with hex strings:
<?php // is_hex_numeric.php
/**
* PHP 7.0.0+ Strings in hexadecimal (e.g. 0xf4c3b00c) notation
* are no longer regarded as numeric strings, i.e. is_numeric() returns FALSE now.
* See: http://php.net/manual/en/function.intval.php
*/
error_reporting(E_ALL);
var_dump( PHP_VERSION ); // string(6) "7.1.25"
var_dump( PHP_INT_SIZE ); // int(8)
$x = 0xf4c3b00c; // --> Assignment of hex value
var_dump($x); // int(4106465292)
var_dump( is_numeric($x) ); // bool(true)
$y = '0x' . dechex($x); // --> Convert hex value to a string
var_dump($y); // string(10) "0xf4c3b00c"
var_dump( is_numeric($y) ); // bool(false)
$z = intval($y, 0); // --> intval() with base zero recognizes hex notation
var_dump($z); // int(4106465292)
var_dump( is_numeric($z) ); // bool(true)
var_dump($x == $z); // bool(true)