PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 7, 2022

[FIXED] strange question about PHP convert string to int

 January 07, 2022     integer, php, string     No comments   

Issue

im trying to convert string to integer but return 0 or wrong number. Below is the code i have tested. Anyone know why? Thanks

<?php
echo gettype(0x55)."\n"; //type is integer
echo 0x55."\n"; //this is correct
echo (int)"0x55"."\n"; //why 0?
echo intval("0x55"); //why 0?

return

integer 85 0 0


Solution

By default, intval() parses base 10, so it ignores the 0x prefix.

If you specify the base as 0, it will determine the base dynamically from the string prefix: 0x means hex, 0 means octal.

So use intval("0x55", 0).

DEMO

I don't think there's any equivalent for the typecast syntax (int).



Answered By - Barmar
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing