15 PHP INTERVIEW QUESTIONS

While both functions have a specific file, there is a fatal error on require()if the file is not included. On the other hand, the include statement only proceeds to the next step of the execution.

How can we get the client’s IP address?

PHP

1

$_SERVER["REMOTE_ADDR"];

What’s the difference between unset() and unlink()

unlink() performs the function of deleting a file passed on to it from the file system while unset() sets a variable to “undefined”

Provide an answer for the output of the code below:

PHP

1

2

3

4

$a='1';

$b=&$a;

$b="2$b";

echo$a.", ".$b;

The answer is 21, 21

Name the main error types in PHP

Notices: these are simple errors that occur during script execution. This would occur when you access an undefined variable.

Warnings: this is a bigger error than notices. However, it does not stop the script execution.

Fatal: if this type of error occurs, it causes a termination to the script execution. This would happen if one tried to access the property of a nonexistent file.

How can you enable error reporting in PHP?

You have to check whether “display_errors” is equal “on” in the php.ini, or you may declare “ini_set(‘display_errors’, 1)” in your script. After that, include error_reporting(E_ALL)” in your code which will display all error messages that occurred during script execution.

Define traits

They are a mechanism that allows a developer to create reusable code in languages like PHP which do not allow multiple inheritance.

Can the value of a constant change during the script’s execution?

Once it has been declared during a PHP execution, it would be impossible to change the value of a constant.

Is it possible to extend aFinaldefined class?

No, it is not possible. A Final class prevents child class and method overriding.

Name the __construct() and __destruct() methods in a PHP class

Objects in PHP have inbuilt Constructor and Destructor methods. The Constructor method is used immediately after the creation of a new instance of the class and it initializes class properties. There are no parameters taken in Destructor.

How can we get the number of elements in an array?

By using the count() function. It is used to return the number of elements in an array.

How would you declare a function that receives one parameter name hello?

The function must print hello if indeed hello is true. But if the function fails to receive hello or hello is false then the function must print bye.

PHP

1

2

3

4

php

functionshowMessage($hello=false){

echo($hello)?'hello':'bye';

}

What if you receive a form submitted by a post to subscribe to a newsletter. This form has a single field, an input text field named email. How would you validate whether the field is empty? Print a message “The email cannot be empty” in this case.

PHP

1

2

3

4

5

<?php

if(empty($_POST['email'])){

echo"The email cannot be empty";

}

?>

Define the 3 scope levels found in PHP

Private – this is visible only in its own class

Public – Visible to any other code accessing the class

Protected – Visible only to classes parent(s) and classes that extend the current class

What is the output of the following code?

PHP

1

2

$i=016;

echo$i/2;

The output is 7. The first zero is indicative of an octal number in PHP. This means that the number evaluates to the decimal number 14 instead of 16.