PHP find output programs (define Constant) | set 1

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

Question 1:

<?php
    define("ABC", "Hello World");
    echo $ABC;
?>

Output:

PHP Notice:  Undefined variable: ABC in /home/main.php on line 3

Explanation:

The above program will generate PHP Notice because there is no need to use $ symbol with a constant defined by define() function.

Question 2:

<?php
    define(A, 10);
    define(B, 20);
    
    $C = a + b;
    
    echo $C;
?>

Output:

0

Explanation:

In the above program, we defined 2 constants A and B initialized with 10 and 20 respectively. Here, we used 'a' and 'b' instead of 'A' and 'B'. Here, we defined case sensitive constants, then the value of variable $C will be 0.

Question 3:

<?php
    define(A, 10, true);
    define(B, 20, true);
    
    $C = a + b;
    
    echo $C;
?>

Output:

30

Explanation:

In the above program, we defined 2 case in-sensitive constants A and B initialized with 10 and 20 respectively using built-in function define(). Here, we used 'a' and 'b' instead of 'A' and 'B'. Here we defined case in-sensitive constants, then the value of variable $C will be 30.

Question 4:

<?php
    define(A, "Hello World");
    define(B, 20, true);
    
    $C = A + B;
    
    echo $C;
?>

Output:

20

Explanation:

The above program will print 20 on the webpage. In the above program, we defined two variables A and B, initialized with "Hello World" and 20 respectively.

In the above expression, we used the '+' operator, which will not concatenate the string constant with integer constant, for concatenation we need to use dot '.' operator. Then the value of the $C variable will be 20.

Question 5:

<?php
    define(A, "Hello World");
    define(B, 20, true);
    
    $C = a . b;
    
    echo $C;
?>

Output:

a20

Explanation:

The above program will print "a20" on the webpage. In the above program, we defined a case sensitive constant 'A' and defined an in-case sensitive constant 'B'.

$C = a .b;

In the above statement, we used 'a' instead of 'A'. That's why the value A will not be used, and the final value of variable $C will be "a20" on the webpage.





Comments and Discussions!

Load comments ↻





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