PHP Coding Questions and Answers – Constants

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Constants”.

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

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

a) PHP is a scripting language
b)

GREETING
GREEtING
advertisement
advertisement

c) GREETING
d)

PHP is a scripting language
PHP is a scripting language
View Answer
Answer: d
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?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. <?php
  2. define("GREETING", "PHP is a scripting language");
  3. echo $GREETING;
  4. ?>

a) $GREETING
b) no output
c) PHP is a scripting language
d) GREETING
View Answer

Answer: b
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.
advertisement

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

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

a)

PHP is a scripting language PHP is a scripting language
advertisement

b)

GREETING_TESt PHP is a scripting language

c)

PHP is a scripting language GREETING_TEST

d)

GREETING_TESt GREETING_TEST
View Answer
Answer: a
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?

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

a) 0.01.0
b) 01
c) No output
d) ERROR
View Answer

Answer: d
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?

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

a) PHP is a scripting language
b) __LINE__
c) 3
d) ERROR
View Answer

Answer: c
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?

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

a) IF:42
b) No output
c) IF:
d) ERROR
View Answer

Answer: d
Explanation: Keyword like IF cannot be used as constant names.

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

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

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

Answer: b
Explanation: Constants can be set as values for other constants.

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

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

a)

448
384

b)

0700
0800

c) ERROR
d) No output
View Answer

Answer: a
Explanation: Anything starting from 0 is evaluated as an octal.

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

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

a) test
b) testtest
c) testvalue
d) error, constant value cannot be changed
View Answer

Answer: c
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?

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

a) test
b) error
c) myObject::CONSTANT
d) no output
View Answer

Answer: b
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.