octdec
Converts a string of octal digits to its decimal integer value.
octdec(string $octal_string): int|float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$octal_string | string | The octal string to convert. Characters outside the range 0-7 are silently ignored. |
Return value
int|floatThe decimal representation of octal_string, as a float when the value is too large for the platform's int type.
Changelog
- PHP 7.4.0Passing invalid characters now emits a deprecation notice. The result is still computed as if those characters were not present.
See also
octdec() converts a string of octal digits into its decimal value. It takes a string, not a number, and that is the whole point: an octal value written in a config file, a form field or a JSON document arrives as text, and treating it as an integer would already have destroyed the base-8 reading.
When to reach for it
Almost always file permissions. Functions like chmod() and mkdir() take an integer mode, but humans write and store modes as "0644". octdec('0644') gives you the 420 those functions expect, whereas passing the string straight through would be read as the decimal 644 and set entirely the wrong bits. In source code you would write the literal 0o644 instead — octdec() is for values that arrive at runtime.
Invalid input is ignored, not rejected
Characters outside 0–7 are skipped and the remaining digits are still converted, so octdec('9') is 0 and octdec('19') is 1. Since PHP 7.4 that raises a deprecation notice, but you still get a number back rather than an error. Validate untrusted input with preg_match('/^[0-7]+$/', $s) first.
Values too large for an int come back as a float, which is why the return type is int|float.
Example
When to use it
- Turning a permission string like '0644' from a config file into the integer chmod() expects.
- Reading octal escape or mode values out of legacy data formats and Unix tooling output.
- Round-tripping modes stored as text with decoct() when writing them back.
- Extracting owner, group and other permission bits from a stored mode string.
Frequently asked questions
How do I convert an octal permission string to an integer in PHP?
Use octdec('0644'), which returns 420 — the value chmod() and mkdir() expect. Passing the string '0644' directly would be read as decimal 644 and set the wrong bits.
What is the opposite of octdec?
decoct(), which turns a decimal integer into an octal string. octdec(decoct($n)) returns $n unchanged.
Does octdec validate its input?
No. Digits outside 0-7 are silently skipped, so octdec('19') returns 1. Since PHP 7.4 that emits a deprecation notice, but the call still succeeds, so validate with a regex if the input is untrusted.
More examples
Round-tripping with decoct()
Octal 10 is decimal 8 and octal 100 is decimal 64, since each position is worth eight times the one to its right. decoct() is the exact inverse, so octdec(decoct($n)) returns the original integer unchanged — useful when a mode has to be stored as text and read back later.
<?php
foreach (['1', '7', '10', '17', '100', '777'] as $oct) {
printf("octal %-4s = %4d decimal\n", $oct, octdec($oct));
}
echo decoct(493), "\n";
var_dump(octdec(decoct(1234)));Very large values return a float
Twenty-one octal sevens is exactly 2^63 - 1, which fits an int and equals PHP_INT_MAX. Adding one more digit needs 66 bits, so octdec() returns a float instead and you lose exact integer precision. Check with is_int() if the result has to stay integral.
<?php
var_dump(octdec('777777777777777777777')); // 21 octal digits
var_dump(octdec('7777777777777777777777')); // 22 digits
var_dump(PHP_INT_MAX);Splitting a permission mode into its parts
Once the string is an integer, each group of three permission bits is one octal digit, so shifting right by 6, 3 and 0 and masking with 7 pulls out the owner, group and other values. That is exactly why Unix modes are written in octal in the first place.
<?php
$perms = ['0644' => 'rw-r--r--', '0755' => 'rwxr-xr-x', '0600' => 'rw-------'];
foreach ($perms as $oct => $symbolic) {
$n = octdec($oct);
printf("%s %-9s decimal %3d owner=%o group=%o other=%o\n",
$oct, $symbolic, $n, ($n >> 6) & 7, ($n >> 3) & 7, $n & 7);
}
Discussion