PHP Multiple Choice Questions on “Constants”.
1. What will be the output of the following PHP code?
-
-
define("GREETING", "PHP is a scripting language", true);
-
echo GREETING;
-
echo "
" -
echo GREETING;
-
?>
a) PHP is a scripting language
b)
GREETING GREEtING
c) GREETING
d)
PHP is a scripting language PHP is a scripting language
Answer: d
Clarification: Since the third parameter is true in define(“GREETING”, “PHP is a scripting language”, true) is true GREETING becomes case insensitive.
2. What will be the output of the following PHP code?
-
-
define("GREETING", "PHP is a scripting language");
-
echo $GREETING;
-
?>
a) $GREETING
b) no output
c) PHP is a scripting language
d) GREETING
Answer: b
Clarification: Constants do not need a $ before them, they are referenced by their variable names itself.
3. What will be the output of the following PHP code?
-
-
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
c) PHP is a scripting language
d)
PHP is a scripting language GREETING_TEST
Answer: a
Clarification: echo constant(x) output x, and x here is the concatenation of GREETING_ and $changing variable with. operator.
4. What will be the output of the following PHP code?
-
-
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
Answer: d
Clarification: 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?
-
-
define("__LINE__", "PHP is a scripting language");
-
echo __LINE__;
-
?>
a) PHP is a scripting language
b) __LINE__
c) 2
d) ERROR
Answer: c
Clarification: __LINE__ is a magical constant that gives the current line number and cannot be used a variable/constant name.
6. What will be the output of the following PHP code?
-
-
define('IF', 42);
-
echo "IF: ", IF;
-
?>
a) IF:42
b) No output
c) IF:
d) ERROR
Answer: d
Clarification: Keyword like IF cannot be used as constant names.
7. What will be the output of the following PHP code?
-
-
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
Answer: b
Clarification: Constants can be set as values for other constants.
8. What will be the output of the following PHP code?
-
-
define('GOOD_OCTAL', 0700);
-
define('BAD_OCTAL', 0600);
-
print GOOD_OCTAL;
-
print '
'; -
print BAD_OCTAL;
-
?>
a)
448 384
b)
0700 0800
c) ERROR
d) No output
Answer: a
Clarification: Anything starting from 0 is evaluated as an octal.
9. What will be the output of the following PHP code?
-
-
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
Answer: c
Clarification: ${VAR_NAME} creates a new variable which is not same as VAR_NAME.
10. What will be the output of the following PHP code?
-
-
class myObject { }
-
define('myObject::CONSTANT', 'test');
-
echo myObject::CONSTANT;
-
?>
a) test
b) error
c) myObject::CONSTANT
d) no output
Answer: b
Clarification: Class constants cannot be defined outside class.
here is complete set of 250+ Multiple Choice Questions and Answers on PHP.
