How to Converts an octal number to a decimal number in PHP ?
There are multiple methods to convert an octal number to a decimal number in PHP:
Method 1: Using octdec() function The octdec() function is a built-in PHP function that converts an octal number to a decimal number. It takes an octal number as a string parameter and returns its decimal equivalent.
Example:
$octal_num = "17"; // Octal number
$decimal_num = octdec($octal_num); // Convert octal to decimal
echo $decimal_num; // Output: 15
Method 2: Using base_convert() function The base_convert() function is another built-in PHP function that can be used to convert an octal number to a decimal number. It takes two parameters - the number to be converted and the base of the number (in this case, 8 for octal).
Example:
$octal_num = "17"; // Octal number
$decimal_num = base_convert($octal_num, 8, 10); // Convert octal to decimal
echo $decimal_num; // Output: 15
Method 3: Using manual conversion We can also manually convert an octal number to a decimal number by multiplying each digit of the octal number with its corresponding power of 8 and adding the results.
Example:
$octal_num = "17"; // Octal number
$decimal_num = 0;
$len = strlen($octal_num);
for ($i = 0; $i < $len; $i++) {
$decimal_num += intval($octal_num[$i]) * pow(8, $len - $i - 1);
}
echo $decimal_num; // Output: 15