Tuesday 29 November 2011

Math Function In PHP


Ø       abs()

  • This function is used to find the absolute value. It means that the negative value is converted into positive value. There is no fix return type. If the value is in integer then the return type is integer and if it is float then the return type is float.

Syntax

                   number abs(mixed $number)

Parameter
Description
$number
The numeric value to process.

Example

<?php
          echo abs(-5.2)."<br/>";
          echo abs(-5);
?>

Output  

5.2
5

Ø       ceil()

  • This function will round up to the nearest integer value. The return type is integer.

Syntax

                   float ceil(float $value)

Parameter
Description
$value
The value to round.

Example

<?php
          echo ceil(4.5)."<br/>";
?>

Output 

5

Ø       floor()

  • This function will return the next lowest integer value by rounding down value if necessary.

Syntax

                   float floor(float $value)

Parameter
Description
$value
The value to round.

Example

<?php
          echo floor(4.5)."<br/>";
?>

Output 

4

Ø       round()

  • This function will round the floating value. If the value contain the digit that is greater than 5 then the digit 1 will be carried forward to the digit before it.

Syntax

                   float round(float $val [,integer $precision])

Parameter
Description
$val
The value to round.
$precision
The optional number of decimal digit to round, the default to zero.

Example

<?php
          echo round(4.5)."<br/>";
          echo round(4.2)."<br/>";
?>

Output  

5
4

Ø       fmod()

  • This function will act as an operator module which is used to find the reminder between the two digits.

Syntax

                   float fmod(float $x, float $y)

Example

<?php
          echo fmod(5,4)."<br/>";
?>

Output  

1

Ø       min()

  • min() returns the numerically lowest of the parameter values.
  • If the first parameter is an array, min returns the lowest value in that array.

Syntax

                   mixed min(mix $arg1,mix arg2……..mix agrn)
                   mixed min(array $values)

Example

<?php
          echo min(5,4)."<br/>";
          $val = min(array(2,4,8),array(1,5,6));
          print_r($val);
?>

Output  

4
Array ( [0] => 1 [1] => 5 [2] => 6 )

Ø       max()

  • max() returns the numerically highest of the parameter values.
  • If the first parameter is an array, min returns the highest value in that array.

Syntax

                   mixed max(mix $arg1,mix arg2……..mix agrn)
                   mixed max(array $values)

Example

<?php
          echo max(5,4)."<br/>";
          $val = max(array(2,4,8),array(1,5,6));
          print_r($val);
?>

Output  

5
Array ( [0] => 2 [1] => 4 [2] => 8 )

Ø       pow()

  • Return base raised to the power of exp. If possible, this function will return an integer.

Syntax

                   number pow(number $base, number $exp)

Parameter
Description
$base
The base to use
$exp
The exponent

Example

<?php
          echo pow(2,3);
?>

Output

          8

Ø       sqrt()

  • Returns the square root of arg.

Syntax

                   float sqrt(float $arg)

Example

<?php
          echo sqrt(100);
?>

Output

          10

Ø       rand()

  • generates a random integer.

Syntax

                   int rand([int min,int max])

  • if called without the optional min,max argument rand() returns a pseudo random integer between 0 and RAND_MAX.

Example

<?php
          echo rand()."<br/>";
          echo rand(5,15);
?>

Output

2275
7

Posted By : Hemangi Zala

No comments:

Post a Comment