octdec()
Learn the PHP octdec() function: convert octal numbers to decimal, with syntax, return values, file-permission examples, gotchas, and live demos.
The octdec() function converts an octal (base-8) number, given as a string, into its decimal (base-10) equivalent. This page covers its syntax, return value, the most common real-world use (Unix file permissions), and the gotchas to watch for.
What is the octdec() Function?
octdec() is a built-in PHP function that takes an octal number written as a string and returns the matching decimal integer.
A quick refresher on number systems:
- Octal (base-8) uses only the digits
0–7. Each position is a power of 8. - Decimal (base-10) is the everyday system, using digits
0–9.
For example, the octal string "17" means 1 × 8 + 7 × 1 = 15 in decimal. octdec() does that math for you so you never have to compute powers of 8 by hand.
It is the inverse of decoct(), which converts a decimal number back into an octal string.
Syntax
octdec(string $octal_string): int|float$octal_string— the octal value to convert, as a string (for example"755").- Return value — the decimal equivalent. The result is an
int, but PHP returns afloatautomatically when the number is too large to fit in a platform integer.
How to Use the octdec() Function
Pass the octal value as a string and octdec() hands back the decimal number:
Here the octal string "10" equals 1 × 8 + 0 = 8 in decimal, so the script prints 8. Note that octal "10" is not ten — it is eight, because there is no digit 8 or 9 in base-8.
Converting Several Values
Looking at a few conversions side by side makes the base-8 pattern clearer:
<?php
echo octdec("7"), "\n"; // 7 (7 × 1)
echo octdec("10"), "\n"; // 8 (1 × 8 + 0)
echo octdec("17"), "\n"; // 15 (1 × 8 + 7)
echo octdec("100"), "\n"; // 64 (1 × 64)
echo octdec("777"), "\n"; // 511 (7 × 64 + 7 × 8 + 7)
?>Real-World Use: File Permissions
The most common reason to reach for octdec() is Unix file permissions. Functions like chmod() expect an integer, but permissions are almost always written and discussed in octal (755, 644, and so on). Passing a permission string straight through fails silently, so convert it first:
<?php
$octalPermission = "755";
// Convert the octal permission string to the integer chmod() needs
$mode = octdec($octalPermission);
echo "Octal 755 = decimal {$mode}\n"; // Octal 755 = decimal 493
// chmod("script.sh", $mode); // applies rwxr-xr-x
?>This prints Octal 755 = decimal 493. A common bug is calling chmod("file", 755) with a plain decimal literal — that sets the wrong permission bits. Either write the octal literal 0755 or build the value with octdec("755").
Things to Watch Out For
octdec() quietly ignores any character that is not a valid octal digit (0–7). So octdec("90") does not raise an error — it strips the invalid 9 and 0 is left, returning 0. Validate your input before converting if it might contain bad characters.
A few more points to keep in mind:
- Input must be octal digits only. Digits
8and9are not valid in base-8 and are discarded. - Pass a string, not a literal. Writing
octdec(017)first interprets017as an octal literal (decimal 15) before converting it again — rarely what you want. Useoctdec("17"). - Large values return a float. Very large octal strings exceed
PHP_INT_MAX, sooctdec()returns a floating-point number instead of an integer.
Related Functions
decoct()— the inverse: decimal to octal.hexdec()/dechex()— convert between hexadecimal and decimal.bindec()/decbin()— convert between binary and decimal.intval()— parse a string to an integer in an arbitrary base (intval($str, 8)is another way to read octal).
Conclusion
The octdec() function turns an octal string into a decimal integer, which is essential whenever you work with file permissions or any base-8 data in PHP. Remember to pass the value as a string, validate input that might contain non-octal characters, and reach for decoct() when you need to convert in the opposite direction.