Laravel
194
w3alert.com
11-01-2020
PHP provides several built-in math functions, which are used to perform mathematical tasks in the PHP program. These functions make it easier to perform mathematical operations.
There are some important math functions following
The PHP max() function is used to get the highest value from the values used in an array.
Syntax:
max( $array ); OR max( value1, value2, value3 );
Let's create a program to get the highest value with the PHP max() function.
<!DOCTYPE html> <html> <body> <?php $array1 = array( 5, 10, 15, 20 ); $array2 = array( 10, 20, 30, 40 ); echo max( $array1 ) ."<br>"; // get highest value from array echo max( $array2 ) ."<br>"; echo max( 20,35,40,55 ) ."<br>"; // get directly highest value from array values ?> </body> </html>
The PHP Max () function is used to get the lowest/minimum value from the array. And you can also pass direct values or array in PHP min() function.
Syntax:
min( $array ); OR min( value1, value2, value3 );
Let's create a program to get the lowest value with the PHP max() function.
<!DOCTYPE html> <html> <body> <?php $array1 = array( 20, 40, 60, 80 ); $array2 = array( 7, 14, 21, 28 ); echo min( $array1 ) ."<br>"; // get lowest value from array echo min( $array2 ) ."<br>"; echo min( 15,30,45,60 ) ."<br>"; // get directly lowest value from array values ?> </body> </html>
With the PHP abs () function, we can get the absolute value of a value/number. The positive form of a number is obtained by this function.
Syntax:
abs( $number );
Let's create a program to get absolute value with PHP abs() function.
<!DOCTYPE html> <html> <body> <?php $number1 = 9 ; // Positive Integer value. $number2 = -9 ; // Negative Integer value. $number3 = 2.5 ; // Positive Float value. $number4 = -3.7; // Negative Float value. # Print all absolute numbers echo abs( $number1 ) ."<br>"; echo abs( $number2 ) ."<br>" ; echo abs( $number3 ) ."<br>"; echo abs( $number4 ) ."<br>"; ?> </body> </html>
The PHP round() function is used to rounds a float/double value.
Syntax:
round( $number );
Let's create a program to get round values with PHP round value.
<!DOCTYPE html> <html> <body> <?php // Use some float/double pointer numbers $number1 = 0.30 ; $number2 = 0.75 ; $number3 = 6.89 ; $number4 = 8.21 ; // Print round values echo round( $number1 ) . "<br>" ; echo round( $number2 ) . "<br>" ; echo round( $number3 ) . "<br>" ; echo round( $number4 ) . "<br>" ; ?> </body> </html>
The PHP floor () function is used to round values to the highest integer value.
Syntax:
ceil( $number ) ;
Let's create a program to get round values to the highest integer value with PHP ceil() function.
<!DOCTYPE html> <html> <body> <?php // Use some float numbers $number1 = 0.21 ; $number2 = 9.38 ; $number3 = 5.45 ; // Print round values echo ceil( $number1 ) . "<br>" ; echo ceil( $number2 ) . "<br>" ; echo ceil( $number3 ) . "<br>" ; ?> </body> </html>
The PHP floor () function is used to round values to the lowest integer value.
Syntax:
floor( $number ) ;
Let's create a program to get round values to the lowest integer value with the PHP floor() function.
<!DOCTYPE html> <html> <body> <?php // Use some float numbers $number1 = 3.6 ; $number2 = 2.47 ; $number3 = 6.9 ; // Print round values echo floor( $number1 ) . "<br>"; echo floor( $number2 ) . "<br>"; echo floor( $number3 ) . "<br>"; ?> </body> </html>
The PHP rand () function is used to generate a random integer number by a given range. If you do not pass a value in this function it automatically generates a random number between 8 and 10 digits.
Syntax:
rand( ); OR rand ( min_value , max_value );
Let's create a program to generate random numbers with PHP rand() function.
<!DOCTYPE html> <html> <body> <?php # Assigned two varriable for Minimum and Maximum values $min_value = 5; $max_value = 20; # Print generated random integers echo rand() ."<br>"; // Automatically generate random integer values echo rand( $min_value,$max_value ) ."<br>"; // Generate integer values by a given range ?> </body> </html>
The PHP mt_rand () function acts like the rand () function. But the mt_rand function used the Mersenne Twister algorithm to generate random integer values / numbers. that function is faster than rand() function.
Syntax:
mt_rand( ); OR mt_rand ( min_value , max_value );
Let's create a program to generate random integers with PHP mt_rand() function.
<!DOCTYPE html> <html> <body> <?php # Assigned two varriable for Minimum and Maximum values $min_value = 5; $max_value = 10; # Print generated random integers echo mt_rand() ."<br>"; // Automatically generate random integer values echo mt_rand( $min_value,$max_value ) ."<br>"; // Generate integer values by a given range ?> </body> </html>
The PHP sqrt() function is used to derive the square root of a number.
Syntax:
sqrt( $number );
Let's create a program to get the square root of a number.
<!DOCTYPE html> <html> <body> <?php // Use some numbers $number1 = 16 ; $number2 = 4.6 ; $number3 = 28 ; $number4 = -36 ; // Print the square roots echo sqrt( $number1 ) . "<br>" ; echo sqrt( $number2 ) . "<br>" ; echo sqrt( $number3 ) . "<br>" ; echo sqrt( $number4 ) . "<br>" ; ?> </body> </html>
The PHP is_nan function is used to check a value. In which it is shown whether the given value is a number or not. If the given value is not a number, it returns true(1) otherwise it returns false/nothing.
Syntax:
is_nan( $number );
Let's create a program to check the given value is a number or not with PHP is_nan() function.
<!DOCTYPE html> <html> <body> <?php echo is_nan(358) . "<br>"; // value is a number. echo is_nan(acos(2)) ."<br>"; // value is a function. ?> </body> </html>
The PHP bindec () function is used to convert a binary number into a decimal number.
Syntax:
bindec( $number );
Let's create a program to convert binary numbers to decimal numbers with PHP bindec() function.
<!DOCTYPE html> <html> <body> <?php // Use some binary numbers $number1 = 0110 ; $number2 = 1010 ; $number3 = 101011100101 ; // Print decimal numbers echo bindec( $number1 ) . "<br>"; echo bindec( $number2 ) . "<br>"; echo bindec( $number3 ) . "<br>"; ?> </body> </html>
The PHP decbin () function is used to convert a decimal number into a binary number.
Syntax:
decbin( $number );
Let's create a program to convert decimal number to binary number with PHP decbin() function.
<!DOCTYPE html> <html> <body> <?php // Use some decimal numbers $number1 = 10 ; $number2 = 25 ; $number3 = 47 ; // Print binary numbers echo decbin( $number1 ) . "<br>"; echo decbin( $number2 ) . "<br>"; echo decbin( $number3 ) . "<br>"; ?> </body> </html>
The PHP decoct () function is used to convert a decimal number into an octal number.
Syntax:
decoct( $number );
Let's create a program to convert decimal numbers to the octal number with the PHP decoct() function.
<!DOCTYPE html> <html> <body> <?php // Use some decimal numbers $number1 = 120 ; $number2 = 245 ; $number3 = 196 ; // Print octal numbers echo decoct( $number1 ) . "<br>"; echo decoct( $number2 ) . "<br>"; echo decoct( $number3 ) . "<br>"; ?> </body> </html>
The PHP dechex () function is used to convert a decimal number into a hexadecimal number.
Syntax:
dechex( $number );
Let's create a program to convert the decimal numbers to the hexadecimal number with the PHP dechex() function.
<!DOCTYPE html> <html> <body> <?php // Use some decimal numbers $number1 = 2035 ; $number2 = 19057 ; $number3 = 24356 ; // Print hexadecimal numbers echo dechex( $number1 ) . "<br>"; echo dechex( $number2) . "<br>"; echo dechex( $number3 ) . "<br>"; ?> </body> </html>
The PHP hexdec() function is used to convert a hexadecimal number into a decimal number.
Syntax:
hexdec( '$number' );
Let's create a program to convert the hexadecimal number to the decimal number with the PHP hexdec() function.
<!DOCTYPE html> <html> <body> <?php // Use some hexadecimal numbers $number1 = 'FF22' ; $number2 = 'A34D' ; $number3 = 'E25D05' ; // Print decimal numbers echo hexdec( $number1 ) . "<br>"; echo hexdec( $number2) . "<br>"; echo hexdec( $number3 ) . "<br>"; ?> </body> </html>
The PHP octdec() function is used to convert a octal number into a decimal number.
Syntax:
octdec( $number );
Let's create a program to convert the octal number to decimal number with the PHP octdec() function.
<!DOCTYPE html> <html> <body> <?php // Use some octal numbers $number1 = 554 ; $number2 = 3543 ; $number3 = 345 ; //Print decimal numbers echo octdec( $number1 ) . "<br>"; echo octdec( $number2) . "<br>"; echo octdec( $number3 ) . "<br>"; ?> </body> </html>