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 within the single-quoted string is not parsed for variable substitution, it’s always a better idea to initialize a single-quoted string 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 only displays the content on the screen after construction

print : is also a construct (so the parentheses are optional when invoked), but it returns TRUE on successful output and FALSE on failing to print the string. However, you can pass multiple parameters to echo, such as: and it will return the string “Welcome to Developerdiary!” print doesn’t need multiple parameters. It’s also often argued that Echo is faster, but the speed advantage is generally negligible and may not exist for future versions of PHP.

printf :is a function, not a construct, and offers benefits like formatted output, but is the slowest way to print data from echo, print, and printf.

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

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

Q6. Explain magic constants in PHP?

Magic constants start and end with double underscores and are predefined constants that change their value depending 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 whether the given variable has a value other than NULL. The function returns a Boolean value of false if the variable is not set, or true if the variable is set. The function can search for 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 main difference is the length of the generated hash. CRC32 is obviously 32 bits, while sha1() returns a 128-bit value and md5() returns a 160-bit value. This is important to avoid collisions.

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

Crypto utilization in PHP is simple, however that doesn’t imply it’s free. First off, depending on the data that you’re encrypting, you would possibly have motives to save a 32-bit cost in the database in place of the 160-bit cost to shop on space. Second, the extra steady the crypto is, the longer is the computation time to supply the hash cost. A excessive quantity web page is probably substantially slowed down, if common md5() technology 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 manage failures. If the file isn’t always found through require(), it’ll cause a fatal error and halt the execution of the script. If the file isn’t always found by include(), a warning will be issued, however 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 the whole thing to the integer type, so numbers 10 and 11 may be compared.

Q15. Explain the ternary conditional operator in PHP?

The expression before ? evaluates if true then the expression before : is executed, otherwise the expression is executed after :.

Syntax:  CONDITION ? True : False

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

__sleep returns the array of all variables to be stored while __wakeup retrieves them.

Q17. What is NULL in PHP?

NULL is a special data type in PHP used to indicate the presence of a single value, NULL. You cannot assign a different 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 used to tell the PHP parser that certain code elements are different from the PHP code. This provides the basic way to distinguish PHP code from other aspects of the program.

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

Break: This statement is used in a loop construct to stop executing the iteration and immediately execute the next code snippet outside the loop construct block.

Continue: This statement is used to skip the current iteration of the loop and continue executing the next iteration until the loop construction terminates.

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 you to view the data sent as part of the URL. This is not the case when using POST, since during this time the data in the request is encrypted.
  • 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 to check if two objects are instantiated with the same class and have the same attributes and values. To test whether two objects refer 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, indicates that the current method does not support overriding by other classes. It is used when an immutable class needs to be created.

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.

More Interview Questions

PHP OOPS Interview Questions And Answers (2022)

Latest MySQL Interview Questions And Answers

JavaScript Interview Questions And Answers

CodeIgniter Interview Questions And Answers

Node.Js Interview Questions And Answers

Laravel Interview Questions And Answers

Conclusion

I think these PHP interview questions and answers would help you understand what kind of questions you might be asked in an interview and by reading these PHP interview questions you can prepare and find out your next interview in no time . And I’ll try to keep updating the interview questions here. How to get more information