Q:

PHP find output programs (const Keyword) | set 1

0

Find the output of PHP programs | const Keyword | Set 1: Enhance the knowledge of PHP const Keyword by solving and finding the output of some PHP programs.

Question 1

<?php
    const COMPANY_NAME = "Includehelp";
    const WEBSITE_NAME = "Includehelp.com";
    
    $A = 10 + printf("hello");
    
    switch ($A)
    {
        case 15:
            echo $COMPANY_NAME;
        break;
        case 16:
            echo $WEBSITE_NAME;
        break;
        default:
            echo "Wrong Selection";
        break;
    }
?>

Question 2:

<?php
    class Sample
    {
        const COMPANY_NAME = "Includehelp";
        const WEBSITE_NAME = "Includehelp.com";
    }
    
    $A = 2 * 5 + pow(2, 2) + pow(8, 0);
    $obj = new Sample();
    
    switch ($A)
    {
        case 15:
            echo $obj->COMPANY_NAME;
        break;
        case 16:
            echo $obj->WEBSITE_NAME;
        break;
        default:
            echo "Wrong Selection";
        break;
    }
?>

Question 3:

<?php
    class Sample
    {
        const COMPANY_NAME = "Includehelp";
        const WEBSITE_NAME = "Includehelp.com";
    }
    
    $A = 2 * 5 + pow(2, 2) + pow(8, 0);
    $obj = new Sample();
    
    switch ($A)
    {
        case 15:
            echo $obj::COMPANY_NAME;
        break;
        case 16:
            echo $obj::WEBSITE_NAME;
        break;
        default:
            echo "Wrong Selection";
        break;
    }
?>

Question 4:

<?php
    class Sample
    {
        const COMPANY_NAME = "Includehelp";
        const WEBSITE_NAME = "Includehelp.com";
    
        define(COMPANY_NAME, "JSM");
    }
    
    $A = 2 * 5 + pow(2, 2) + pow(8, 0);
    $obj = new Sample();
    
    switch ($A)
    {
        case 15:
            echo $obj::COMPANY_NAME;
        break;
        case 16:
            echo $obj::WEBSITE_NAME;
        break;
    
        default:
            echo "Wrong Selection";
        break;
    }
?>

Question 5:

<?php
    class Sample
    {
        const COMPANY_NAME = "Includehelp";
        const WEBSITE_NAME = "Includehelp.com";
    }
    
    define(COMPANY_NAME, "JSM");
    
    $A = 2 * 5 + pow(2, 2) + pow(8, 0);
    $obj = new Sample();
    
    switch ($A)
    {
        case 15:
            echo $obj::COMPANY_NAME . ", " . COMPANY_NAME;
        break;
        case 16:
            echo $obj::WEBSITE_NAME;
        break;
        default:
            echo "Wrong Selection";
        break;
    }
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

Hello

Explanation:

In the program, we created two constants COMPANY_NAME and WEBSITE_NAME that contain "Includehelp" and "Includehelp.com" respectively.

$A = 10 + printf("hello");

In the above expression, printf() function will print "hello" and return 5, then 15 will be assigned to the variable $A. Then the "case 15" executed in the switch case. But we used $COMPANY_NAME instead of COMPANY_NAME, then it will not print anything on the webpage.

Answer  2:

Output:

PHP Notice:  Undefined property: Sample::$COMPANY_NAME 
in /home/main.php on line 14

Explanation:

In the program, we created a class Sample that contains two constants COMPANY_NAME and WEBSITE_NAME that contain "Includehelp" and "Includehelp.com" respectively.

Now evaluate the expression:

$A = 2*5+pow(2,2)+pow(8,0);
$A = 10+4+1;
$A = 15;

Then we created the object of Sample class. The value of $A is 15, that "case 15" will execute, but it will not print anything and generate a PHP notice, which is given above. Because if you want to access constant members of class then we need to use scope resolution operator "::".

Answer  3:

Output:

IncludeHelp

Explanation:

In the program, we created a class Sample that contains two constants COMPANY_NAME and WEBSITE_NAME that contain "Includehelp" and "Includehelp.com" respectively.

Now evaluate the expression:

$A = 2*5+pow(2,2)+pow(8,0);
$A = 10+4+1;
$A = 15;

Then we created the object of Sample class. The value of $A is 15, that "case 15" will execute, but it will print "Includehelp" on the webpage.

Answer  4:

Output:

PHP Parse error:  syntax error, unexpected 'define' (T_STRING), 
expecting function (T_FUNCTION) or const (T_CONST) in 
/home/main.php on line 7

Explanation:

The above program will generate syntax error, because we cannot define constant in a class using define() function like this, define() function can be called within a function or in the global scope of a PHP script.

Answer  5:

Output:

Includehelp, JSM

Explanation:

In the program, we created a class Sample that contains two constants COMPANY_NAME and WEBSITE_NAME that contain "Includehelp" and "Includehelp.com" respectively. And we define a constant COMPANY_NAME using define() function outside the class.

Now evaluate the expression:

$A = 2*5+pow(2,2)+pow(8,0);
$A = 10+4+1;
$A = 15;

Then we created the object of Sample class. The value of $A is 15, that "case 15" will execute, but it will print "Includehelp, JSM" on the webpage.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now