3/26/15

PHP Numeric


Don’t think that PHP’s power is limited to strings only.

The language has over 50 built-in functions for working with numbers, ranging from simple formatting functions to functions for arithmetic, logarithmic, and trigonometric manipulations.
Some of these important functions are
FunctionWhat it Does
ceil()Rounds a number up
floor()Rounds a number down
abs()Finds the absolute value of anumber
pow()Raises one number to the power of another
exp()Finds the exponent of a number
rand()Generates a random number
bindec()Converts a number from binary to decimal
decbin()Converts a number from decimal to binary
decoct()Converts a number from decimal to octal
octdec()Converts a number from octal to decimal
dechex()Converts a number from decimal to hexadecimal
hexdec()Converts a number from hexadecimal to decimal
number_format()Formats number with grouped thousands and decimals
printf()Formats a number using a custom specification

Eg i
 <?php
       $num=19.7
       echo ceil($num);
  ?>
    Output : 20
In the above example
Initialize variable $num with value=19.7 , output will become 20.
because this function round value up.
Eg ii
 <?php
       $num=19.7
       echo floor($num);
  ?>
    Output : 19
in the above example
variable $num = 19.7,and the output will become 19.
Because this function round value down.
Eg iii
 <?php
      $num =-19.7
      echo abs($num);
  ?>
    Output : 19
In the above example
declare variable ($num) value=19.7 and the output will 19.7
Because abs( ) returns the absolute of given number.
Eg iv
 <?php
     echo pow(4,3);
  ?>
    Output : 64
In the above example.
Pass pow( ) function inside echo with value(4,3).
Its multiply (value=4). three times and the result is 64.
Eg v
 <?php
    echo rand(10,99);
  ?>
    Output : 55
In the above example
Pass rand( ) function With value from( 10 to 99 ).
it will display any random value lies from 10 to 100.
when we refresh the page on every refresh it show random value like. 22,33 ,44,56 and so on.
Eg vi
 <?php
    echo bindec(1000);
  ?>
    Output : 8
In the above example
bindec( ) function pass inside echo statement with binary value = 1000.
So output will become 8 because bindec( ) function convert binary number into decimal number.
Eg vii
 <?php
   echo decbin(8);
  ?>
    Output : 1000
In the above example
decbin( ) function Pass inside echo statement with decimal value = 8.
So output will become. 1000

0 comments:

Post a Comment

SUBSCRIBE TO NEWSLETTER

FIND US ON FACEBOOK

FIND US ON Twitter

FIND US ON GOOGLE+