Home Interview Questions and Answers PHP Interview Questions and Answers For Freshers part-2

PHP11.What are the rules for determine the “truth” of any value not already of the Boolean type?
Here are the rules for determine the “truth” of any value not already of the Boolean type −

If the value is a number, it is false if exactly equal to zero and true otherwise.

If the value is a string, it is false if the string is empty (has zero characters) or is the string “0”, and is true otherwise.

Values of type NULL are always false.

If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value.

Valid resources are true (although some functions that return resources when they are successful will return FALSE when unsuccessful).

Don’t use double as Booleans.

12.What is NULL?
NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −

$my_var = NULL;
The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −

$my_var = null;
A variable that has been assigned NULL has the following properties:

It evaluates to FALSE in a Boolean context.

It returns FALSE when tested with IsSet() function.

13.How will you define a constant in PHP?
To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $.

14.What is the purpose of constant() function?
As indicated by the name, this function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.

<?php
define(“MINSIZE”, 50);
echo MINSIZE;
echo constant(“MINSIZE”); // same thing as the previous line
?>
Only scalar data (boolean, integer, float and string) can be contained in constants.

15.What are the differences between PHP constants and variables?
There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.

Constants cannot be defined by simple assignment, they may only be defined using the define() function.

Constants may be defined and accessed anywhere without regard to variable scoping rules.

Once the Constants have been set, may not be redefined or undefined.

16.What are PHP magic constants?
PHP provides a large number of predefined constants to any script which it runs known as magic constants.

17.What is the purpose of _LINE_ constant?
_LINE_ − The current line number of the file.

18.What is the purpose of _FILE_ constant?
_FILE_ − The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, _FILE_ always contains an absolute path whereas in older versions it contained relative path under some circumstances.

19.What is the purpose of _FUNCTION_ constant?
_FUNCTION_ − The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

20.What is the purpose of _CLASS_ constant?
_CLASS_ − The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

You may also like

Leave a Comment