Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:19:01+00:00 2026-05-23T08:19:01+00:00

Using PHP 5.3.5. Not sure how this works on other versions. I’m confused about

  • 0

Using PHP 5.3.5. Not sure how this works on other versions.

I’m confused about using strings that hold numbers, e.g., '0x4B0' or '1.2e3'. The way how PHP works with such strings seems inconsistent to me. Is it only me? Or is it a bug? Or undocumented feature? Or am I just missing some magic sentence in docs?

<?php

echo $str = '0x4B0', PHP_EOL;
echo "is_numeric() -> ", var_dump(is_numeric($str)); // bool(true)
echo "*1           -> ", var_dump($str * 1);         // int(1200)
echo "(int)        -> ", var_dump((int)$str);        // int(0)
echo "(float)      -> ", var_dump((float)$str);      // float(0)
echo PHP_EOL;

echo $str = '1.2e3', PHP_EOL;
echo "is_numeric() -> ", var_dump(is_numeric($str)); // bool(true)
echo "*1           -> ", var_dump($str * 1);         // float(1200)
echo "(int)        -> ", var_dump((int)$str);        // int(1)
echo "(float)      -> ", var_dump((float)$str);      // float(1200)
echo PHP_EOL;

In both cases, is_numeric() returns true. Also, in both cases, $str * 1 parses string and returns valid number (integer in one case, float in another case).

Casting with (int)$str and (float)$str gives unexpected results.

  • (int)$str in any case is able to parse only digits, with optional “+” or “-” in front of them.
  • (float)$str is more advanced and can parse something like ^[+-]?\d*(\.\d*)?(e[+-]?\d*)?, i.e., optional “+” or “-“, followed by optional digits, followed by optional decimal point with optional digits, followed by optional exponent which consists of “e” with optional “+” or “-” followed by optional digits. Fails on hex data though.

Related docs:

  • is_numeric() – states that “Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part“. If function, meant to test if a string holds numeric data, returns true, I expect PHP to be able to convert such string to a number. This seems to work with $str * 1, but not with casting. Why?
  • Converting to integer – states that “in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument“. After such statement, I expect both $s * 10 and (int)$s * 10 expressions to work the same way and to return the same result. Though, as shown in example, those expressions are evaluated differently.
  • String conversion to numbers – states that “Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent“. “Exponent” is “e” or “E”, followed by digits, e.g., 1.2e3 is valid numeric data. Sign (“+” or “-“) is not mentioned. It does not mention hexidecimal values. This conflicts with definition of “numeric data” used in is_numeric(). Then, there is suggestion “For more information on this conversion, see the Unix manual page for strtod(3)“, and man strtod describes additional numeric values (including HEX notation). So, after reading this, is hexidecimal data supposed to be valid or invalid numeric data?

So…

  • Is there (or, rather, should there be) any relation between is_numeric() and the way how PHP treats strings when they are used as numbers?
  • Why do (int)$s, (float)$s and $s * 1 work differently, i.e,. give completely different results, when $s is 0x4B0 or 1.2e3?
  • Is there any way to convert a string to a number and keep its value, if it is written as 0x4B0 or as 1.2e3? floatval() does not work with HEX at all, intval() needs $base to be set to 16 to work with HEX, typecasting with (int)$str and (float)$str sometimes works, sometimes does not work, so these are not valid options. I’m also not considering $n *= 1;, as it looks more like data manipulation rather than converting. Self-written functions also are not considered in this case, as I’m looking for native solution.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:19:02+00:00Added an answer on May 23, 2026 at 8:19 am

    The direct casts (int)$str and (float)$str don’t really work differently at all: They both read as many characters from the string as they can interpret as a number of the respective type.

    For “0x4B0”, the int-conversion reads “0” (OK), then “x” and stops, because it cannot convert “x” into an integer. Likewise for the float-conversion.

    For “1.2e3”, the int-conversion reads “1” (OK), then “.” and stops. The float-conversion recognises the entire string as valid float notation.

    The automatic type recognition for an expression like $str * 1 is simply more flexible than the explicit casts. The explicit casts require the integers and floats to be in the format produced by %i and %f in printf, essentially.

    Perhaps you can use intval and floatval rather than explicit casts-to-int for more flexibility, though.

    Finally, your question “is hexidecimal data supposed to be valid or invalid numeric data?” is awkward. There is no such thing as “hexadecimal data”. Hexadecimal is just a number base. What you can do is take a string like “4B0” and use strtoul etc. to parse it as an integer in any number base between 2 and 36.[Sorry, that was BS. There’s no strtoul in PHP. But intval has the equivalent functionality, see above.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not a question about a specific framework. I am using plain php
After using PHP for a while now, I've noticed that not all built-in PHP
Consider a normal PHP image upload functionality (not using AJAX) and there occurs this
I found a similar post about this but still not sure. As I am
( Updated a little ) I'm not very experienced with internationalization using PHP, it
How can we separate Logic from presentation without using any template engine (traditional php-not
Using PHP, what's the fastest way to convert a string like this: 123 to
I have a php script that looks like this: $contacts[{$firstname}] = $_POST[{$firstname}] ; $contacts[{$lastname}]
I have a PHP script that parses an array using the json_encode() method but
I need to develop a script using Wordpress PHP that will run with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.