# PHP Exercises

These mentioned below are some of the basic PHP Exercises mainly Maths Problems that can be solved with the help of PHP.

## Performing Math Operations

PHP has several built-in functions that help you perform anything from simple additions or subtraction to advanced calculations. You’ve already seen how to perform basic mathematical operations in the PHP operators chapter. Let’s check out one more example:

## Find the Absolute Value of a Number

The absolute value of an integer or a float can be found with the abs() function, as demonstrated in the following example:

## Round a Fractional Value Up or Down

The ceil() function can be used to round a fractional value up to the next highest integer value, whereas the floor() function can be used to round a fractional value down to the next lowest integer value, as demonstrated in the following example:

## Find the Square Root of a Number

You can use the sqrt() function to find the square root of a positive number. This function returns a special value NAN for negative numbers. Here’s an example:

## Generate a Random Number

The rand() function can be used to generate a random number. You can optionally specify a range by passing the min, max arguments, as shown in the following example:

If rand() function is called without the optional min, max arguments, it returns a pseudo-random number between 0 and getrandmax().

The getrandmax() function shows the largest possible random value, which is only 32767 on the Windows platform. So, if you require a range larger than 32767, you may simply specify the min and max arguments.

## Convert Decimal Numbers to Binary and Vice Versa

The decbin() function is used to convert a decimal number into a binary number. Whereas its counterpart the bindec() function converts a number from binary to decimal.

## Convert Decimal Numbers to Hexadecimal and Vice Versa

The dechex() function is used to convert a decimal number into hexadecimal representation. Whereas, the hexdec() function is used to converts a hexadecimal string to a decimal number.

Convert Decimal Numbers to Octal and Vice Versa

The decoct() function is used to convert a decimal number into an octal representation. Whereas, the octdec() function is used to converts an octal number to a decimal number.

Convert a Number from One Base System to Another

The base_convert() function can be used to convert a number from one base system to another. For example, you can convert decimal (base 10) to binary (base 2), hexadecimal (base 16) to octal (base 8), octal to hexadecimal, hexadecimal to decimal, and so on.

This function accepts three parameters: the number to convert, the base it’s currently in, and the base it’s to be converted to. The basic syntax is as follows:

base_convert(number, frombase, tobase);

Here, the number can be either an integer or a string representing an integer. Both frombase and tobase have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, where a means 10, b means 11 and z means 35. Here’s a simple example to show how this function works:

If you liked our content PHP Exercises, then you will also likeĀ PHP Functions

## Leave a Reply