octdec() function in PHP

The octdec() function converts an octal number to its decimal equivalent. This function is useful when working with octal representations in PHP.

Syntax

octdec(octal_string)

Parameters

  • octal_string − A string containing the octal number to convert. Can include negative sign for negative numbers.

Return Value

The octdec() function returns the decimal equivalent of the specified octal number as an integer.

Example 1: Basic Octal to Decimal Conversion

Here's how to convert a simple octal number to decimal −

<?php
    echo octdec("120");
    echo "<br>";
    echo octdec("170");
?>
80
120

Example 2: Converting Negative Octal Numbers

The function also handles negative octal numbers −

<?php
    echo octdec("-120");
    echo "<br>";
    echo octdec("-77");
?>
-80
-63

Example 3: Using with decoct() Function

You can combine octdec() with decoct() to convert decimal to octal and back −

<?php
    $decimal = 170;
    echo "Original decimal: " . $decimal . "<br>";
    echo "Converted to octal: " . decoct($decimal) . "<br>";
    echo "Back to decimal: " . octdec(decoct($decimal));
?>
Original decimal: 170
Converted to octal: 252
Back to decimal: 170

Conclusion

The octdec() function provides a simple way to convert octal numbers to decimal format in PHP. It handles both positive and negative values and is commonly used in number base conversion operations.

Updated on: 2026-03-15T07:29:28+05:30

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements