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() :

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

Defining Constants using the const keyword:

<?php
const CONSTANT = 'Slightbook';
echo CONSTANT;
?>

 




Next 5 interview question(s)

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