Skip to content
php interview questions and answers

PHP Interview questions and Answers

Today, this article will walk you through the most commonly asked PHP interview questions and answers for freshers and experienced in the industry.

Q1. Would you initialize your strings with single quotes or double quotes?

Since the data inside the single-quoted string is not parsed for variable substitution, it’s always a better idea speed-wise to initialize a string with single quotes, unless you specifically need variable substitution.

Q2. What is the difference between characters 23 and x23?

The first one is octal 23, the second is hex 23.

Q3. How we can combine two variables together?
 $var1 = 'Welcome to ';
 $var2 = 'developerdiary.in;
 $var 3 = $var1.$var2;
Q4. For printing out strings, there are echo, print and printf. Explain the differences.

echo : is the most primitive of them, and just outputs the contents following the construct to the screen

print : is also a construct (so parentheses are optional when calling it), but it returns TRUE on successful output and FALSE if it was unable to print out the string. However, you can pass multiple parameters to echo, like:
and it will output the string “Welcome to Developerdiary!” print does not take multiple parameters. It is also generally argued that echo is faster, but usually the speed advantage is negligible, and might not be there for future versions of PHP.

printf : is a function, not a construct, and allows such advantages as formatted output, but it’s the slowest way to print out data out of echo, print and printf.

Q5. Explain the difference between $message and $$message?

$message is a regular variable, which has a fixed name and fixed value, whereas a $$message is a reference variable, which stores data about the variable. The value of $$message can change dynamically as the value of the variable changes.

Q6. Explain magic constants in PHP?

Magic constants start and end with double underscores and are predefined constants that change their value based on context and usage.

There are 9 magic constants in PHP:

__LINE__ , __FILE__, __DIR__ , __FUNCTION__ , __CLASS__ , __TRAIT__ , __METHOD__ , __NAMESPACE__ , ClassName::class

Q7. Explain the isset() function?

The isset() function checks if the particular variable has a value other than NULL. The function returns Boolean value false if the variable is not set or return true if the variable is set. The function can check multiple values: isset(var1, var2, var3…)

Example

<?php
$a = 0;
// True because $a is set
if (isset($a)) {
  echo "Variable 'a' is set.<br>";
}
$b = null;
// False because $b is NULL
if (isset($b)) {
  echo "Variable 'b' is set.";
}
?>
Output : Variable 'a' is set.
Q8. What’s the difference between md5(), crc32() and sha1() crypto on PHP?

The major difference is the length of the hash generated. CRC32 is, evidently, 32 bits, while sha1() returns a 128 bit value, and md5() returns a 160 bit value. This is important when avoiding collisions.

Q9. So if md5() generates the most secure hash, why would you ever use the less secure crc32() and sha1()?

Crypto usage in PHP is simple, but that doesn’t mean it’s free. First off, depending on the data that you’re encrypting, you might have reasons to store a 32-bit value in the database instead of the 160-bit value to save on space. Second, the more secure the crypto is, the longer is the computation time to deliver the hash value. A high volume site might be significantly slowed down, if frequent md5() generation is required.

Q10. What does a special set of tags <?= and ?> do in PHP?

The output is displayed directly to the browser.

<?php
$message = "I am a developer diary";
<?= $message ?>
?>
Output : I am a developer diary
Q11. What’s the difference between include and require?

It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

Q12. How do you define a constant?

You can define constant in php using define() directive, like define (“MYCONSTANT”, 100);

Q13. Explain various PHP string functions?

PHP allows for many string operations. Some popular string functions are:

echo() : output one or more string

explode() : break string into array

Example

$mystr = "welcome to developer diary"
explode(" ", $mystr)

ltrim() : removes extra characters or spaces from the left side of the string

parse_str() : Parses a query string into variables

str_replace() : replaces specified characters of a string

strlen() : calculates length of the string

Example

strlen("welcome");

result = 7
Q14. Will comparison of string “10” and integer 11 work in PHP?

Yes, internally PHP will cast everything to the integer type, so numbers 10 and 11 will be compared.

Q15. Explain the ternary conditional operator in PHP?

Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : is executed, otherwise, the expression following : is executed.

Syntax:  CONDITION ? True : False

Q16. What’s the special meaning of __sleep and __wakeup?

 __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.

Q17. What is NULL in PHP?

NULL is a special data type in PHP used to denote the presence of only one value, NULL. You cannot assign any other value to it.

NULL is not case sensitive in PHP and can be declared in two ways as shown below:

$var = NULL;
Or
$var = null;

Both of the above syntaxes work fine in PHP.

Q18. What does the phrase ‘PHP escape’ mean?

PHP escape is a mechanism that is used to tell the PHP parser that certain code elements are different from PHP code. This provides the basic means to differentiate a piece of PHP code from the other aspects of the program.

Q19. What is the meaning of break and continue statements in PHP?

Break: This statement is used in a looping construct to terminate the execution of the iteration and to immediately execute the next snippet of code outside the block of the looping construct.

Continue: This statement is used to skip the current iteration of the loop and continue to execute the next iteration until the looping construct is exited.

Q20. What are some of the popular frameworks in PHP?

There are many frameworks in PHP that are known for their usage. Following are some of them:

  • CodeIgniter
  • CakePHP
  • Laravel
  • Yii 2
Q21. How to create a database connection and query in PHP?

Following is the code to create connected between php and database

$connection = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
 die("Connection error: " . $conn->connect_error);
}
$sql = "CREATE DATABASE USERS";
if ($conn->query($sql) === TRUE) {
 echo "Database successfully created";
} else {
 echo "Error while creating database: " . $conn->error;
}
Q22. Explain the difference between GET and POST requests.

Any PHP developer needs to have an adequate understanding of the HTTP protocol. The differences between GET vs POST are an indispensable part of the HTTP protocol learning. Here are the major differences between the two requests:

  • GET allows displaying the submitted data as part of the URL. This is not the case when using POST as during this time, the data is encoded in the request.
  • The maximum number of characters handled by GET is limited to 2048. No such restrictions are imposed on POST.
Q23. How is the comparison of objects done in PHP?

The operator ‘==’ is used for checking whether two objects are instanced using the same class and have the same attributes as well as equal values. To test whether two objects are referring to the same instance of the same class, the identity operator ‘===’ is used.

Q24. How is typecasting achieved in PHP?

The name of the output type needs to be specified in parentheses before the variable that is to be cast. Some examples are:

  • (array) – casts to array
  • (bool), (boolean) – casts to Boolean
  • (double), (float), (real) – casts to float
  • (int), (integer) – casts to integer
Q25. What is the use of the final class and the final method in PHP?

The ‘final’ keyword, if present in a declaration, denotes that the current method does not support overriding by other classes. This is used when there is a requirement to create an immutable class.

Note: Properties cannot be declared as final. It is only methods and classes that get to be final.

Next up on this core PHP interview questions and answers blog, let us take a look at the intermediate questions.

Q26. What is the difference between overloading and overriding in PHP?

Function Overriding : In this both child and parent classes have some function and a different number of arguments.

Function Overloading : Same function name and perfoming various tasks according to number or arguments.

You can deep learn about function overloading and overriding with example.

Conclusion

I believe that these PHP interview questions and answers would help you understand what kind of questions may be asked to you in an interview, and by going through these PHP interview questions, you can prepare and crack your next interview in one go. And I will try to update interview questions here more. So you can get more into