PHP Variables Interview Questions and Answers

How to define a Constant in PHP?

Constant can be declared using define() function in PHP. To retrieve the value of a constant, we can specify its name.

Note: Unlike with variables, we do not need to use doller ($) to output constant value.


How to check if a variable is empty in PHP?

The empty() function is an in-built function in PHP which is used to check whether a variable is empty or not.

Example of empty function:


Here is the list of values, that are returned as an empty

  • “” (an empty string)
  • 0(as an integer)
  • 0.0(o as a float)
  • “0”(o as a string)
  • Null
  • False
  • array() (an empty array)

How can you pass a variable by reference in PHP?

By using an ampersand (&) sign in-front of the variable, we can pass the variable by reference.

Why or when to use variable by reference ?

When PHP function arguments are passed by value, and value is changed within function, it does not change variable value outside the function. To allow a function to modify its argument value outside the function, they must be passed by reference.

Example:

If we do not use value by reference (&), then the output will be: ‘This is a sample line,’