Home »
PHP »
PHP find output programs
PHP find output programs (const Keyword) | set 1
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.
Submitted by Nidhi, on January 27, 2021
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;
}
?>
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.
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;
}
?>
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 "::".
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;
}
?>
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.
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;
}
?>
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.
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;
}
?>
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.