This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Constants”.
1. What will be the output of the following PHP code?
<?php
define("GREETING", "PHP is a scripting language", true);
echo GREETING;
echo "<br>";
echo GREETING;
?>
a) PHP is a scripting language
b)
GREETING GREEtING
c) GREETING
d)
PHP is a scripting language PHP is a scripting languageView Answer
Explanation: Here’, we have defined a constant GREETING. Since the third parameter is true in define(“GREETING”, “PHP is a scripting language”, true), GREETING also becomes case insensitive.
2. What will be the output of the following PHP code?
<?php
define("GREETING", "PHP is a scripting language");
echo $GREETING;
?>
a) $GREETING
b) no output
c) PHP is a scripting language
d) GREETING
View Answer
Explanation: As you know that Constants do not need a $ before them, they are referenced by their variable names itself. In the given example “GREETING” is a constant. However, if we prefix a $ symbol, we are turning it into a variable, and since there is no variable named GREETING, it won’t echo anything.
3. What will be the output of the following PHP code?
<?php
define('GREETING_TEST', 'PHP is a scripting language', true);
echo GREETING_TESt;
$changing_variable = 'test';
echo constant('GREETING_' . strtoupper($changing_variable));
?>
a)
PHP is a scripting language PHP is a scripting language
b)
GREETING_TESt PHP is a scripting language
c)
PHP is a scripting language GREETING_TEST
d)
GREETING_TESt GREETING_TESTView Answer
Explanation: Since the 3rd argument of define() is true, GREETING_TEST becomes case insensitive. So, the “echo GREETING_TESt” will echo “PHP is a scripting language”.
echo constant(x) outputs the value of x, and x here is the concatenation of GREETING_ and $changing_variable converted into uppercase, which results in GREETING_TEST. Next, echo constant(GREETING_TEST) will echo “PHP is a scripting language”.
4. What will be the output of the following PHP code?
<?php
class Constants
{
define('MIN_VALUE', '0.0');
define('MAX_VALUE', '1.0');
public static function getMinValue()
{
return self::MIN_VALUE;
}
public static function getMaxValue()
{
return self::MAX_VALUE;
}
}
echo Constants::getMinValue();
echo Constants::getMaxValue();
?>
a) 0.01.0
b) 01
c) No output
d) ERROR
View Answer
Explanation: In a class constants should be defined const MIN_VALUE = 0.0;const MAX_VALUE = 1.0; instead.
5. What will be the output of the following PHP code?
<?php
define("__LINE__", "PHP is a scripting language");
echo __LINE__;
?>
a) PHP is a scripting language
b) __LINE__
c) 3
d) ERROR
View Answer
Explanation: __LINE__ is a magical constant that gives the current line number and cannot be used as a variable/constant name.
6. What will be the output of the following PHP code?
<?php
define('IF', 42);
echo "IF: ", IF;
?>
a) IF:42
b) No output
c) IF:
d) ERROR
View Answer
Explanation: Keyword like IF cannot be used as constant names.
7. What will be the output of the following PHP code?
<?php
define("NEW_GOOD_NAME_CONSTANT", "I have a value");
define("OLD_BAD_NAME_CONSTANT", NEW_GOOD_NAME_CONSTANT);
echo NEW_GOOD_NAME_CONSTANT;
echo OLD_BAD_NAME_CONSTANT;
?>
a) I have a value
b) I have a valueI have a value
c) ERROR
d) I have a valueNEW_GOO_NAME_CONSTANTS
View Answer
Explanation: Constants can be set as values for other constants.
8. What will be the output of the following PHP code?
<?php
define('GOOD_OCTAL', 0700);
define('BAD_OCTAL', 0600);
print GOOD_OCTAL;
print '<br>';
print BAD_OCTAL;
?>
a)
448 384
b)
0700 0800
c) ERROR
d) No output
View Answer
Explanation: Anything starting from 0 is evaluated as an octal.
9. What will be the output of the following PHP code?
<?php
define("VAR_NAME","test");
${VAR_NAME} = "value";
echo VAR_NAME;
echo ${VAR_NAME};
?>
a) test
b) testtest
c) testvalue
d) error, constant value cannot be changed
View Answer
Explanation: ${VAR_NAME} creates a new variable which is not same as VAR_NAME.
10. What will be the output of the following PHP code?
<?php
class myObject { }
define('myObject::CONSTANT', 'test');
echo myObject::CONSTANT;
?>
a) test
b) error
c) myObject::CONSTANT
d) no output
View Answer
Explanation: Class constants cannot be defined outside class.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 250+ Multiple Choice Questions and Answers on PHP.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check Information Technology Books
- Check PHP Books
- Practice Programming MCQs
- Check MCA Books
- Apply for Programming Internship