PHP find output programs (basics) | set 1

Find the output of PHP programs | Basics | Set 1: 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
    int $x = 15;
    int $y = 20;
    int $z = 0;
    
    $z = $x + $y * 2;
    
    echo $z;
?>

Output:

PHP Parse error:  syntax error, unexpected '$x' (T_VARIABLE) 
in /home/main.php on line 2

Explanation:

The above program will generate syntax error because we cannot use explicit data types like int, float, char in PHP. In PHP, the data-type of a variable depends on data stored in it. If we store integer number then the variable becomes integer type or if we store floating-point number then it becomes float type.

Question 2:

<?php
    $PI = 3.14F;
    $RAD = 5;
    
    $AREA = $PI * $RAD * $RAD;
    echo $AREA;
?>

Output:

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

Explanation:

The above program will generate syntax error because we used 'F' in the suffix of $PI value, which is not allowed in PHP.

Question 3:

<?php
    $PI = 3.14;
    $RAD = 5;
    
    $AREA = $PI * $RAD * $RAD;
    echo $area;
?>

Output:

PHP Notice:  Undefined variable: area in /home/main.php on line 6

Explanation:

The above program will generate error notice because variables are case sensitive in PHP, we used $area instead of $AREA in the echo statement.

Question 4:

<?php
    $PI = 3.14;
    $RAD = 5;
    
    $AREA = $PI * $RAD * $RAD;
    echo "Area of circle: " + $AREA;
?>

Output:

78.5

Explanation:

In the above program, we declared two $PI and $RAD initialized with 3.14 and 5 respectively. Now we evaluate the below expression:

$AREA= $PI * $RAD * $RAD;
$AREA=3.14*5*5;
$AREA=15.70*5;
$AREA=78.5;

Then we used '+' operator in the echo statement for string concatenation. But '+' operator is not used for concatenation, here we need to use '.' Operator for concatenation like below statement.

echo "Area of circle: ".$AREA;

So that echo statement will print the only value of $AREA, instead of a complete string.

Question 5:

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

Output:

PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO) 
in /home/main.php on line 6

Explanation:

The above program will generate syntax error because we semicolon ';' is missing in the below statement.

$C  = "Hello"





Comments and Discussions!

Load comments ↻






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