Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
log
math
communitycreator

What is log() in PHP?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The log() function in PHP calculates the logarithm of a number to the base. log() calculates the natural logarithm of a number if the base is not specified by the user.

The following illustration shows the mathematical representation of the log() function.

Mathematical representation of the log() function

Syntax

float log(number, base)

Parameters

This function requires two parameters:

  • A number for which the logarithm is to be calculated.
  • The base of the logarithm, which is an optional parameter.

Return value

log() returns the logarithm of a number to the base sent as a parameter.

The log() function calculates the natural logarithm of a number if the base is not provided, i.e., the default base is e.

  • If number is a negative number, then log() returns NaN.
  • If number is 0, then log() returns -INF.
  • If base is 0, then log() will yield a Warning.

Code

<?php
#log without base 
echo("log(10): ");
echo (log(10));
echo("\n");
#log with base 
echo("log(2,2): ");
echo (log(2,2));
?>	

<?php
#error outputs
echo("log(0): ");
echo (log(0));
echo("\n");
echo("log(-1): ");
echo (log(-1));
echo("\n");
echo("log(0,0): ");
echo (log(0,0));
?>

RELATED TAGS

php
log
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring