# Numerical string comparison - octal and hex.

- Thread: https://externals.io/message/46695
- Original archive: https://news-web.php.net/php.internals/46695
- Started by Richard Quadling on 2010-01-13T16:02:35+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #46695 — Richard Quadling — 2010-01-13T16:02:35+00:00

URL: https://externals.io/message/46695#46695

Hello.

In looking at numer vs string comparison, I think I've found an oddity.

You can successfully compare hex strings ...

var_dump(255 == '0xff');

but not octal strings ...

var_dump(63 == '077');


Is this a bug (lack of octal support) or an unexpected feature (hex support)?


Neither formats are documented in [1].


Regards,

Richard.

[1] http://docs.php.net/manual/en/language.types.string.php#language.types.string.conversion
-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

---

## #46698 — hack988 hack988 — 2010-01-13T16:21:17+00:00

URL: https://externals.io/message/46695#46698 · In reply to: #46695

I think it's not an but!
you must read carefuly about "Single quoted" and "Double quoted"

2010/1/14 Richard Quadling <rquadling@googlemail.com>:
> Hello.
>
> In looking at numer vs string comparison, I think I've found an oddity.
>
> You can successfully compare hex strings ...
>
> var_dump(255 == '0xff');
>
> but not octal strings ...
>
> var_dump(63 == '077');
>
>
> Is this a bug (lack of octal support) or an unexpected feature (hex support)?
>
>
> Neither formats are documented in [1].
>
>
> Regards,
>
> Richard.
>
> [1] http://docs.php.net/manual/en/language.types.string.php#language.types.string.conversion
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling

---

## #46699 — Richard Quadling — 2010-01-13T16:26:15+00:00

URL: https://externals.io/message/46695#46699 · In reply to: #46698

2010/1/13 hack988 hack988 <hack988@dev.htwap.com>:
> I think it's not an but!
> you must read carefuly about "Single quoted" and "Double quoted"
>
> 2010/1/14 Richard Quadling <rquadling@googlemail.com>:
> > […]

Not sure you've got what I'm saying ...

<?php
var_dump(255 == '0xff');
var_dump(63 == '077');
var_dump(255 == "0xff");
var_dump(63 == "077");
?>

outputs ...

bool(true)
bool(false)
bool(true)
bool(false)

Single/double quotes don't come into the issue.

-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

---

## #46700 — Rasmus Lerdorf — 2010-01-13T16:49:06+00:00

URL: https://externals.io/message/46695#46700 · In reply to: #46695

Richard Quadling wrote:
> Hello.
> 
> In looking at numer vs string comparison, I think I've found an oddity.
> 
> You can successfully compare hex strings ...
> 
> var_dump(255 == '0xff');
> 
> but not octal strings ...
> 
> var_dump(63 == '077');
> 
> 
> Is this a bug (lack of octal support) or an unexpected feature (hex support)?

We don't have octal strings in PHP.  Only native octal.
eg.
var_dump(63 == 077);

Why?  Mostly because octal strings is more likely to cause incorrect
behaviour than both regular numeric strings and hex strings.

-Rasmus

---

## #46701 — Richard Quadling — 2010-01-13T16:53:07+00:00

URL: https://externals.io/message/46695#46701 · In reply to: #46700

2010/1/13 Rasmus Lerdorf <rasmus@lerdorf.com>:
> Richard Quadling wrote:
> > […]
>
> We don't have octal strings in PHP.  Only native octal.
> eg.
> var_dump(63 == 077);
>
> Why?  Mostly because octal strings is more likely to cause incorrect
> behaviour than both regular numeric strings and hex strings.
>
> -Rasmus
>

Thank you.

Should [1] be updated to include hex strings and a note about octal strings?

Richard.


[1] http://docs.php.net/manual/en/language.types.string.php#language.types.string.conversion
-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
