PHP MATHEMATICS [MATH] FUNCTIONS - 1

What Will I Learn?

  • PHP Math Functions

Requirements

  • PHP
  • Linux

Difficulty

  • Basic

Tutorial Contents

You can also perform advanced mathematical operations outside of the four operations using the PHP language. You can use mathematical functions added to the PHP kernel to make operations easier. For example, if you need to write a function to find a binary number base in any statement, you can use the simple (Math) operations can be easily accomplished using functions. Now let's examine the mathematical functions in order and perform examples to find out how they are used.

Advanced Calculations

The PHP language has functions that are ready to perform many mathematical calculations other than 4 operations. The functions for which many of the required mathematical operations are performed are given in the following tablature explanations.

FunctionExplanation
abs()Returnsthe absolute value of the count.
sqrt()Calculate the square root of the count.
pow()The count calculates the rank at the entered rank.
fmod()Gives the remainder (mod) of the count divided by the number entered.
log()The natural logarithm performs the account.
log10()Perform logarithm calculations at base 10.
exp()Calculates the exponential of the entered number.
hypot()The two sides calculate the hypotenuse of the specified right triangle.
<?php
echo 'Absolute value : ' . abs (-20) . ' 
'
; echo ' Square root of 16 : ' . sqrt (16) . '
'
; echo ' 3^4 (3 base 4 ) : ' .pow (3 , 4 ) . '
'
; echo ' Part 5 of 12 : ' . fmod ( 12 , 5 ) . '
'
; echo ' log ( 6 ) : ' . log ( 6 ) . ' < br > ' ; echo ' 10 base log ( 6 ) : ' . log10 (6) . '
'
; echo 'e base -2 : ' . exp (-2 ). '
'
; echo 'hypotenuse of triangles of length 3 and 4 : ' .hypot (3,4).'
'
; ?>

Examples of how to use the functions in the table are given above. Control which results the functions return in which cases by changing the values.

Calling and Rounding

When working with floating point (float, double) data types, you may sometimes want to get the desired number of digits and the remainder to be discarded. When rounding is performed, you can decide whether the count is to be truncated or rounded up or down. The list of functions that can be used for rounding operations is given in the following table.

ceil()If there is a next part, it is discarded and the number is rounded up.
floor()If there is a next part from the benefit, it is discarded and the number is rounded down.
round()If the first number is 5 and above, it is rounded up, otherwise it is rounded down.
<?php
$pi=pi();
echo $pi . '
'
; #Result: 3.14159265359 echo ceil ($pi) . '
'
; #Result: 4 echo floor ($pi) . '
'
; #Result: 4 echo round ($pi, 3); #Result: 3.142 ?>

In the above example, truncation and rounding functions have affected the number of pi.

In the example, down is performed with ceil() and floor(). In a somewhat different way, we want round() to take 3 digits after the call, and the next digits to be discarded.

NOTE: In PHP 5.3.0 and higher, parameter 3 can also be entered for the round () function. With this newly added feature, whatever the value of the next number is, the rounding is done as specified in the software.
With this development, the round() function

PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN
PHP_ROUND_HALF_EVEN
PHP_ROUND_HALF_ODD

values.

Conversiors

In this section, we will examine the conversions between numbers. We will learn how to convert a radian expression to a base of binary numbers in degrees or decimal numbers. The conversion functions that come with the php language are given in the table below.

FunctionExplanation
base_convert()Any board converts the expressed number to another number base.
bindec()Converts a binary number base to a decimal number base.
decbin()Converts a decimal number base to a binary number base.
hexdec()Converts hexadecimal number base to decimal number base.
dechex()Converts a decimal number base to a hexadecimal number base.
octdec()Octal converts a number base to a decimal base.
decoct()Converts the decimal number base to octal number base.
deg2rad()Transforms the expression entered in degrees into the radian type.
rad2deg()Transforms the expression entered as radians to degrees.
<?php
$number = 12 ;
echo base _convert ($number , 10 , 8 ) ; # Result: 14
?> 

In the above example, 12 values have been loaded into the $ number variable. Using the base converter function, $number is converted from decimal number base to octal number base.

<?php
echo bindec (1111000) . '
'
; #Result: 120 echo decbin (120) . '

'
; #Result: 1111000 echo hexdec ('1 A') . '
'
; #Result: 26 echo dechex (44) . '

'
; #Result: 2c echo octdec (14 ). ' < br>' ; #Result: 12 echo decoct (12) . '

'
; #Result : 14 echo deg 2 rad (90) . '
'
; #Result : 1.57079632679 echo rad 2 deg (1.57079632679) . '
'
; #Result : ~90 ?>

Above are examples of other conversions.
Thank you for reading. The first part of our Mathematical Functions series is over. In the second part, we will process Trigonometry and Random Numbers.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center