250+ TOP PHP Coding MCQs on Constants and Answers

PHP Multiple Choice Questions on “Constants”.

1. What will be the output of the following PHP code?

  1. define("GREETING", "PHP is a scripting language", true);
  2. echo GREETING;
  3. echo "
    "
  4. echo GREETING;
  5. ?>

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?

  1. define("GREETING", "PHP is a scripting language");
  2. echo $GREETING;
  3. ?>

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?

  1. define('GREETING_TEST', 'PHP is a scripting language', true);
  2. echo GREETING_TESt;
  3. $changing_variable = 'test';
  4. echo constant('GREETING_' . strtoupper($changing_variable));
  5. ?>

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?

  1. class Constants
  2. {
  3.     define('MIN_VALUE', '0.0');  
  4.     define('MAX_VALUE', '1.0');  
  5.     public static function getMinValue()
  6.     {
  7.         return self::MIN_VALUE;
  8.     }
  9.     public static function getMaxValue()
  10.     {
  11.         return self::MAX_VALUE;
  12.     }
  13. }
  14. echo Constants::getMinValue();
  15. echo Constants::getMaxValue();
  16. ?>

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?

  1. define("__LINE__", "PHP is a scripting language");
  2. echo __LINE__;
  3. ?>

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?

  1. define('IF', 42); 
  2. echo "IF: ", IF;
  3. ?>

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?

  1. define("NEW_GOOD_NAME_CONSTANT", "I have a value");
  2. define("OLD_BAD_NAME_CONSTANT", NEW_GOOD_NAME_CONSTANT);
  3.  
  4. echo NEW_GOOD_NAME_CONSTANT;
  5. echo OLD_BAD_NAME_CONSTANT; 
  6. ?>

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?

  1. define('GOOD_OCTAL', 0700);
  2. define('BAD_OCTAL', 0600);
  3. print GOOD_OCTAL;
  4. print '
    '
    ;
  5. print BAD_OCTAL; 
  6. ?>

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?

  1. define("VAR_NAME","test"); 
  2. ${VAR_NAME} = "value"; 
  3. echo VAR_NAME;
  4. echo ${VAR_NAME}; 
  5. ?>

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.

  250+ TOP MCQs on PHP Working with Dates and Answers

10. What will be the output of the following PHP code?

  1. class myObject { }
  2. define('myObject::CONSTANT', 'test');
  3. echo myObject::CONSTANT; 
  4. ?>

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top