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 6225981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:56:37+00:00 2026-05-24T08:56:37+00:00

My code <?php var_dump(‘0xD’ * 1 ); var_dump((int)’0xD’); var_dump(intval(‘0xD’)); var_dump((float)’0xD’); Actual result: int(13) int(0)

  • 0

My code

<?php
var_dump('0xD' * 1 );
var_dump((int)'0xD');
var_dump(intval('0xD'));
var_dump((float)'0xD');

Actual result:

int(13)
int(0)
int(0)
float(0)

Why result of first two conditions are not same? Can you provide me correct documentation?

All that I found is

  • Type Casting: Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:…
  • Converting to integer: From strings:
    See String conversion to numbers
  • String conversion to numbers When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

So, As far as I understand, docs say that casting to int directly and by putting in numeric context should be same. What is my mistake?

ADDED: Try to check first and second code (and outputs) before answering.

  • 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-24T08:56:38+00:00Added an answer on May 24, 2026 at 8:56 am

    '0xD' is a string. The documentation clearly specifies how a string is "casted" into an integer value (picking the (int) '0xD'; example here):

    When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

    If the string does not contain any of the characters ‘.’, ‘e’, or ‘E’ and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

    The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an ‘e’ or ‘E’ followed by one or more digits.

    The initial porition of the string '0xD' that is a number is 0, hence the it’s (int) 0.

    Don’t mix this with writing code. If you write that in code it’s not a string, it’s the hexa-decimal representation/notation of an integer number (Demo):

    <?php
    
    $value = eval("return 0xD;");
    
    var_dump($value); # int(13)
    

    This does not fully answer why the expression '0xD' * 1 results in (int) 13, but explains everything else so far and which two rules of integer value interpretation apply in PHP.

    I assume that there is a slight difference between casting to integer and integer context in PHP. If the string representing a hexadecimal number it is used in integer/float context:

    '0xD' * 1
    

    In this expression, the string '0xD' will get evaluated as number because of the * operator and the 1 operand (see Type Juggling). This is not Type Casting, because there is no cast language construct.

    It looks like that in this expression’s case, the interpretation of the string as an integer is done literally (as specified) but is not the same as when you do a conversion.

    One could argue it remains undocumented when conversion applies and when literal interpretation applies. However, using type casting can get you around any such problems (Demo):

    <?php
    
    $value = '0xD';
    
    var_dump($value); # string(3) "0xD"
    var_dump($value * 1); # int(13)
    var_dump((int) $value * 1); # int(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this PHP code: // total is 71.24 (float) $value = $total * 100;
Two Questions How can I debug PHP code remotely which is lying on the
i have this code: <?php var_dump(strpos($url, cashgold.)+9) ?> <?php var_dump(strpos($url, '/', 8)) ?> <?php
So here is my code: <?php $url = http://www.site.com/whcms/includes/api.php; # This is not the
At the moment my code (PHP) has too many SQL queries in it. eg...
This PHP code... 207 if (getenv(HTTP_X_FORWARDED_FOR)) { 208 $ip = getenv('HTTP_X_FORWARD_FOR'); 209 $host =
The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128x128.
Optimization of PHP code via runtime benchmarking is straight forward. Keep track of $start
I am writing PHP code where I want to pass the session id myself
I wrote a PHP code like this $site=http://www.google.com; $content = file_get_content($site); echo $content; But

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.