PHP Tutorial

PHP Examples

PHP Practice

PHP find output programs (Math Functions) | set 1

Find the output of PHP programs | Math Functions | Set 1: Enhance the knowledge of PHP Math Functions concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 25, 2021

Question 1:

<?php
    $radius = 5;
    
    $area = PI() * $radius * $radius;
    $cir = 2 * pi() * $radius;
    
    print ($area . '<br>');
    print ($cir);
?>

Output:

78.539816339745
31.415926535898

Explanation:

In the above program, we created a variable $radius which is initialized with 5. Here, we used PI() function that returns 3.1415926535898.

Here, we calculated the area of circle and circumference of the circle. And now evaluate the expression.

$area = PI() * $radius * $radius;
$area = 3.1415926535898 * 5 * 5; 
$area = 78.539816339745;

And,

$cir = 2 * pi() * $radius;
$cir = 2 * 3.1415926535898 * 5;
$cir = 31.415926535898;

After calculating the area and circumference of the circle, we print values of $area, $cir on the webpage.

Question 2:

<?php
    $list = (1, 2, 3, 4, 5);
    
    $mid = max($list) + min($list);
    
    print ($mid);
?>

Output:

PHP Parse error:  syntax error, unexpected ',' 
in /home/main.php on line 2

Explanation:

The above program will generate the compile-time error because we declared a variable $list and tried to assign a list of values to it, it is not the correct way. To assign a list of values to a single variable, we need to create an array. The proper way is given below:

$list = array(1,2,3,4,5);

Question 3:

<?php
    $list = array(1,2,3,4,5);
    
    $mid = (max($list) + min($list))/2;
    
    print($mid);
?>

Output:

3

Explanation:

In the above program, we created an array of integer values, that contains the value (1,2,3,4,5). After that we used two built-in PHP function min() and max().

The max() function is used to get the highest value from the array that is 5, and min() function is used to get the lowest value from array that is 1.

$mid = (5+1)/2;
$mid = 3;

Then the final value of $mid that is 3, will be printed on the webpage.

Question 4:

<?php
    $mid = (max(1, 2, 3, 4, 5) + min(1, 2, 3, 4, 5)) / 2;
    print ($mid);
?>

Output:

3

Explanation:

In the above program, we used two built-in PHP function min() and max().

The max() function is used to get the highest value from the specified list that is 5, and min() function is used to get the lowest value from the specified list that is 1.

$mid = (5+1)/2;
$mid = 3;

Then the final value of $mid which is 3, will be printed on the webpage.

Question 5:

<?php
    $A = 2;
    $B = - 5;
    $C = 1;
    
    $D = (abs($B) + sqrt($B * $B - 3 * $A * $C)) / (2 * $A);
    
    print ($D);
?>

Output:

2.3397247358852

Explanation:

In the above program, we created two variables $A, $B, and $C initialized with 3, -5, and 1 respectively. Here, we used two built-in function abs() and sqrt().

The abs() function always returns the positive or absolute value, and sqrt() is used to calculate the square root of a specified number.

Now evaluate the expression:

$D = (abs($B) + sqrt($B*$B-3*$A*$C)) / (2 *$A);
$D = (5 +sqrt(-5*-5-3*3*1))/6;
$D = (5 +sqrt(25-9))/6;
$D = (5 +4)/6;
$D = 9/6;
$D = 1.5;

Then the final value of $D will be printed on the webpage.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.