PHP Constants MCQ Questions and Answers

1. Which function is used to define a constant in PHP?

a) set_constant()
b) const()
c) define()
d) constant()

Answer:

c) define()

Explanation:

The define() function is used to define a constant in PHP.

2. Once a constant is defined in PHP, can its value be changed?

a) Yes
b) No
c) Only if it's not a system constant
d) Only within the same script

Answer:

b) No

Explanation:

The value of a constant in PHP cannot be changed once it is set.

3. Which of the following is the correct way to check if a constant is defined in PHP?

a) is_defined()
b) defined()
c) const_defined()
d) is_constant()

Answer:

b) defined()

Explanation:

The defined() function is used to check whether a constant is defined.

4. How are constants accessed in PHP?

a) Using the $ symbol
b) Directly by their name without any $ symbol
c) Using the const() function
d) Using the define() function

Answer:

b) Directly by their name without any $ symbol

Explanation:

Constants are accessed directly by their name without any $ symbol.

5. What is the output of the following PHP code if the constant 'MAX' is not defined? defined('MAX') ? 'Yes' : 'No';

a) Yes
b) No
c) Error
d) NULL

Answer:

b) No

Explanation:

The defined() function returns false if the constant is not defined, so the output will be 'No'.

6. Which of the following is true about magic constants in PHP?

a) They are predefined by PHP
b) Their value changes depending on their location
c) Both a) and b)
d) None of the above

Answer:

c) Both a) and b)

Explanation:

Magic constants are predefined by PHP, and their value changes based on where they are used.

7. What does the PHP constant PHP_VERSION represent?

a) The current PHP version
b) The PHP installation path
c) The maximum PHP script execution time
d) The PHP configuration settings

Answer:

a) The current PHP version

Explanation:

The PHP_VERSION constant contains the current version of PHP.

8. Which magic constant returns the function or method name from which it is called?

a) __FUNCTION__
b) __METHOD__
c) __CLASS__
d) __LINE__

Answer:

b) __METHOD__

Explanation:

The __METHOD__ magic constant returns the name of the method, whereas __FUNCTION__ returns the name of the function.

9. Can you create an array as a constant in PHP?

a) Yes, in all PHP versions
b) No, it's not possible in PHP
c) Only in PHP 7.0 and later
d) Only in PHP 5.6 and earlier

Answer:

c) Only in PHP 7.0 and later

Explanation:

As of PHP 7.0, it is possible to define an array as a constant using the define() function.

10. What is the scope of a constant in PHP?

a) Global
b) Local
c) Static
d) Both Global and Local

Answer:

a) Global

Explanation:

Constants in PHP are automatically global and can be accessed from any part of the script.


Comments