PHP find output programs (basics) | set 2

Find the output of PHP programs | Basics | Set 2: Enhance the knowledge of PHP basic concepts by solving and finding the output of some PHP basic programs.
Submitted by Nidhi, on January 16, 2021

Question 1:

<?php
    $A = 3.14;
    $B = 5;
    $C = "Hello";
    
    var_dump($A);
    var_dump($B);
    var_dump($C);
?>

Output:

float(3.14)
int(5)
string(5) "Hello"

Explanation:

In the above program, we defined three variables $A, $B, and $C initialized with different types of values. Then we used function var_dump(), this is used to print the data type of specified variable. The type of $A is float, the type of $B is 5, and the type of $C is string.

Question 2:

<?php
    $A = 3.14;
    $B = 5;
    $C = "Hello";
    
    echo var_dump($A);
    echo var_dump($B);
    echo var_dump($C);
?>

Output:

float(3.14)
int(5)
string(5) "Hello"

Explanation:

In the above program, we defined three variables $A, $B, and $C initialized with different types of values. Then we used function var_dump(), this is used to print the data type of specified variable. The type of $A is float, the type of $B is 5, and the type of $C is string.

The echo statement is useless in the above program. Because the function var_dump() does not return anything.

Question 3:

<?php
    $P = 5000;
    $R = 5;
    $T = 2;
    
    $SI = ($P * $R * $T) / 100;
    
    print ("Simple Interest: " . $SI);
?>

Output:

Simple Interest: 500

Explanation:

In the above program, we calculated the simple interest. Here, we defined three variable $P, $R, and $T initialized with 5000, 5, and 2 respectively.

Now coming to the expression:

$SI = ($P * $R * $T)/100;
$SI = (5000*5*2)/100;
$SI = (50000)/100;
$SI = 500;

Then we used print() function to print the value of $SI with a specified string.

Question 4:

<?php
    $P = 5000;
    $R = 5;
    #$T  = 2;
    
    $SI = ($P * $R * $T) / 100;
    print ("Simple Interest: " . $SI);
?>

Output:

PHP Notice:  Undefined variable: T in /home/main.php on line 6
Simple Interest: 0

Explanation:

In the above program, we defined $P and $R initialized with 5000 and 5.

#$T=2;

The '#' symbol is used to comment the line of code. But, we used $T in the below expression.

$SI = ($P * $R * $T)/100;

The $T is not initialized, then the value of $T will be used as 0 in the expression. Then the final value of $SI will 0. Then "Simple Interest: 0" will be printed on the webpage.

Question 5:

<?php
    $P = 5000;
    $R = 5;
    $T = 2;
    
    $SI = ($P * /*$R*/ $T) / 100;
    
    print ("Simple Interest: " . $SI);
?>

Output:

Simple Interest: 100

Explanation:

In the above program, we defined $P, $R, and $T initialized with 5000, 5, and 2 respectively.

$SI = ($P*/*$R*/$T)/100;

Now coming to the above expression, here we used "/**/" to comment multiple lines. Here, we commented /*R*/ inside the above expression then the actual expression will be:

$SI = ($P*$T)/100;

Let's evaluate the expression:

$SI = (5000*2)/100;
$SI = 10000/100;
$SI = 100;

Then "Simple Interest: 100" will be printed on the webpage.





Comments and Discussions!

Load comments ↻





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