PHP octdec() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The octdec() function is used to convert an octal number to decimal number equivalent. The function takes a string with octal representation as argument and retuns an integer.

For example octdec('10') returns 8.

Syntax

octdec ( string $octal_string ) : number

Parameters

Sr.NoParameter & Description
1octal_string
A string containing the octal number to be converted

Return Values

PHP octdec() function returns a decimal equivalent of given octal representation.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example converts '10' from octal to decimal number system. −

<?php
   $arg='10';
   $val=octdec($arg);
   echo "octdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

octdec(10) = 8

Example

 Live Demo

If there is Any character other than octal digit (other than 0 - 7) in the string, it is ignored. In following example '#' is ignored and rest of string is converted to decimal −

<?php
   $arg='23#4';
   $val=octdec($arg);
   echo "octdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

octdec(234) = 156

Example

 Live Demo

If all characters are other than octal digits, the functon returns 0−

<?php
   $arg='Hello';
   $val=octdec($arg);
   echo "octdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

octdec(Hello) = 0

Example

 Live Demo

The octdec() function treats the octal number inside the argument string as unsigned integer

<?php
   $arg='-20';
   $val=octdec($arg);
   echo "octdec(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

octdec(-20) = 16
raja
Published on 30-Jun-2020 10:42:30
Advertisements