PHP decoct() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The decoct() function converts a decimal number into an octal (base 8) string.

Octal only uses the digits 0 to 7, and you will most often see it in Unix file permissions like 755 or 644.

Syntax

Call decoct() with the values you need. Start with this line.

PHP
You give decoct() one whole number and it returns a string made of octal digits.
CallWhat it does
decoct(8)Returns "10", because 8 is one group of eight and zero left over.
decoct(64)Returns "100".
decoct(0644)Returns "644", a common file permission.

A number written with a leading zero in PHP source code, like 0755, is already an octal literal. decoct() turns the stored integer back into the octal text.

Library declarations
PHP
decoct(int $num): string

Parameters

ParameterDescription
numRequired. The decimal integer you want to convert to octal.

Return Value

Return ValueDescription
stringThe octal representation of num as a string with no leading zero.

PHP Examples

Example 1 — Convert decimal numbers to octal

Try a few small numbers to see how base 8 works.

PHP
<?php
echo decoct(8) . PHP_EOL;
echo decoct(64) . PHP_EOL;
echo decoct(15) . PHP_EOL;

How It Works

  1. decoct(8) returns "10" because 8 equals 1 group of 8 plus 0.
  2. decoct(64) returns "100" because 64 is 8 x 8.
  3. decoct(15) returns "17", which means 1 x 8 + 7.
  4. Every result is printed on its own line using PHP_EOL.

Example 2 — Zero, strings and the return type

Check what decoct() returns for zero and for numeric strings.

PHP
<?php
echo decoct(0) . PHP_EOL;
echo decoct("64") . PHP_EOL;
echo gettype(decoct(8)) . PHP_EOL;

How It Works

  1. decoct(0) returns the string "0".
  2. A numeric string like "64" is converted to an integer first (in normal non-strict mode).
  3. gettype() shows the result is a string, not an integer.
  4. If you need a number back, use octdec() or cast the string with (int).

Example 3 — Show file permissions in octal

Display a permission value the way Linux tools usually show it.

PHP
<?php
$permissions = 0644;

echo "Permissions: " . decoct($permissions) . PHP_EOL;
echo "Directory: " . decoct(0755) . PHP_EOL;

How It Works

  1. 0644 is an octal literal, so PHP stores it as the integer 420.
  2. Printing 420 directly would not look like a permission code.
  3. decoct() converts 420 back to the string "644".
  4. The same idea turns 0755 into "755" for a directory.

Frequently Asked Questions

It converts a decimal integer to its octal (base 8) form and returns it as a string.
Mostly for Unix file permissions such as chmod("file.txt", 0644). Octal makes the read, write and execute bits easy to read.
No, it returns a string. Use octdec() to turn the octal string back into a decimal number.
decoct() is a shortcut for base 10 to base 8. base_convert() can convert between any bases from 2 to 36 but works with strings.
It returns the string "0".

Next: deg2rad()

The deg2rad() function converts an angle from degrees into radians.

deg2rad() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful