PHP Interview questions

Total available count: 33
Subject - PHP Frameworks
Subsubject - PHP

How to define constants?

We can define a constant by using the define()- function or by using the const keyword outside a class definition as of PHP 5.3.0. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the below paragraph. Once a constant is defined, it can never be modified or undefined.

As opposed to defined constants using define(), constants defined using the keyword const must be declared at the top level scope because they are defined at compile-time. This means that they cannot be declared inside functions, if statements, loops or try/ catchblocks.

Defining Constants using define() :

define("CONSTANT", "Slightbook.");
echo CONSTANT; // outputs "Slightbook."
echo Constant; // outputs "Constant" and issues a notice.

Defining Constants using the const keyword:

const CONSTANT = 'Slightbook';


Next 5 interview question(s)

What is need for using static variables?
What is the difference between global vs $GLOBALS?
What is Void?
What is the difference between gettype() vs str replace?
What is NaN?