PHP Constant & Magic Constant Quiz

Test your knowledge of PHP constants and magic constants, such as define(), const, and special constants like FILE and METHOD, with this quiz.

Last Updated :
Discuss
Comments

Question 1

What is a constant in PHP?

  • A variable that changes

  • A fixed value that cannot change

  • A function

  • A type of loop

Question 2

Which of the following is used to define a constant in PHP?

  • const

  • define()

  • Both a and b

  • None of the above

Question 3

How do constants in PHP differ from variables?

  • Constants can change, variables cannot

  • Variables start with $, constants do not

  • Variables are global, constants are not

  • None of the above

Question 4

Which of the following constants is predefined by PHP?

  • PHP_VERSION

  • PHP_TIMEZONE

  • PHP_MAX_INT

  • PHP_LANGUAGE

Question 5

Which magic constant provides the current line number of the file?

  • __DIR__

  • __FILE__

  • __LINE__

  • __METHOD__

Question 6

What does __TRAIT__ return?

  • The class name

  • The trait name

  • The method name

  • The current directory

Question 7

Which of these is NOT a PHP magic constant?

  • __LINE__

  • __FILE__

  • __CLASS__

  • __VARIABLE__

Question 8

What does __NAMESPACE__ return?

  • The current namespace

  • The current function name

  • The current line number

  • The name of the class

Question 9

What is the best practice for naming constants in PHP?

  • Use lowercase letters

  • Use uppercase letters with underscores

  • Use camelCase

  • No specific convention

Question 10

Which magic constant returns the directory of the file?

  • __CLASS__

  • __DIR__

  • __METHOD__

  • __LINE__

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion