Mathematics7 min readExamples run on PHP 8.5.10

octdec()

Learn how PHP 8.4 octdec() turns octal strings such as file permission modes into integers, how large values become floats, and which input mistakes to avoid.

Signature

octdec(string $octal_string): int|float

Purpose

octdec() converts a number written in base 8 into its base 10 value. Octal uses only the digits 0 to 7. Each position is worth eight times the position to its right. The octal string 755 therefore means 7 × 64 + 5 × 8 + 5, which is 493.

You will meet octal most often with Unix-style permission modes such as 644 or 755. Those values often arrive as strings from configuration, a form field or a data file. octdec() turns the string into an ordinary PHP number you can compare, add or pass to other functions.

Signature

Parameters

octal_string is the only parameter and it has no default. It must be a string made of octal digits. A leading zero, as in 0644, is fine because a leading zero adds nothing to the value.

The manual says invalid characters are silently ignored in the computation. Since PHP 7.4.0, supplying them is deprecated, so PHP emits a deprecation notice. The result is still computed as if those characters were not there. The digits 8 and 9, a minus sign, spaces and letters are all invalid in this context.

Return value and failure cases

The function returns the decimal value as an int. If the number is too large for the platform's integer type, it returns a float instead. A float can lose precision for very large values, so do not treat it as an exact integer.

octdec() does not throw an exception for malformed text. The failure mode is quiet: bad characters are dropped, you get a deprecation notice, and the number you receive may not be what you meant. If your input comes from users, validate it first, for example with a pattern that allows only the digits 0 to 7.

Example 1: basic conversion and a round trip

The first example converts 77 and then shows that octdec() reverses decoct().

Example
<?php
echo octdec('77') . "\n";
echo octdec(decoct(45)) . "\n";
Output
63
45

Example 2: permission modes

Permission strings are a typical real use. Mapping a list of mode strings gives the integer values behind them. The leading zero in 0600 has no effect.

Example
<?php
$modes = ['644', '755', '0600'];
$values = array_map('octdec', $modes);
echo json_encode($values);
Output
[420,493,384]

Example 3: values near the integer limit

On a 64-bit build, the largest signed integer is 2^63 − 1. In octal that is 21 sevens, so it still fits in an int. A 1 followed by 21 zeros is 2^63, which no longer fits, so PHP returns a float.

Example
<?php
$max = octdec('777777777777777777777');
$over = octdec('1000000000000000000000');
echo json_encode([$max, is_float($over)]);
Output
[9223372036854775807,true]

This example assumes a 64-bit integer size, which is the normal case for current PHP builds.

Edge cases

  • Leading zeros are harmless: octdec('0017') equals octdec('17').
  • An empty string has no digits, so the result is 0.
  • Overflow produces a float. Check with is_float() if exactness matters.
  • Negative numbers are not supported. A - is an invalid character, so it is ignored with a deprecation notice and you get the positive value.
  • Digits 8 and 9 are also invalid. Treating '789' as input drops the 8 and the 9 instead of failing loudly.
  • Input type is a string. Passing an integer literal like 755 works only because PHP converts it to the string '755'. An octal literal such as 0755 in source code is already the number 493, and converting that to a string gives '493', which contains an invalid digit.

Common mistakes

  1. Passing a number that is already decimal. Writing octdec(0755) first evaluates the literal to 493 and then reads the text 493, which holds the invalid digit 9. Pass the string '0755' or '755'.
  2. Expecting an exception on bad input. Only a deprecation notice is raised, and the computation continues. Validate before converting.
  3. Ignoring float results. A very long octal string gives an imprecise float. Do not use it as an exact bit mask.
  4. Confusing it with decoct(). octdec() goes from octal text to a number. decoct() goes from a number to octal text.
  5. Using it for other bases. For binary or hexadecimal use bindec() or hexdec(). For arbitrary bases use base_convert().
  • decoct() converts decimal to octal.
  • bindec() converts binary to decimal.
  • hexdec() converts hexadecimal to decimal.
  • base_convert() converts between arbitrary bases.

Recap

octdec(string $octal_string): int|float reads a base 8 string and returns its decimal value. Keep the input to the digits 0 to 7, remember that invalid characters are ignored with a deprecation notice, and expect a float when the value outgrows the integer type.

Tested examples

Basic conversion and a round trip with decoct()
<?php
echo octdec('77') . "\n";
echo octdec(decoct(45)) . "\n";
Output
63
45
Converting permission mode strings
<?php
$modes = ['644', '755', '0600'];
$values = array_map('octdec', $modes);
echo json_encode($values);
Output
[420,493,384]
Values at and beyond the 64-bit integer limit
<?php
$max = octdec('777777777777777777777');
$over = octdec('1000000000000000000000');
echo json_encode([$max, is_float($over)]);
Output
[9223372036854775807,true]