PHP Interview Questions

There is given PHP interview questions and answers that have been asked in many companies. Let's see the list of top PHP interview questions.

What is the use of mysqli_real_escape_string() function?

mysqli_real_escape_string() function is used to escape special characters from the string for using a SQL statement

$DBconnection = mysqli_connect("localhost","username","dbname");
$productName = mysqli_real_escape_string($con,$_POST['proname']);
$ProductType = mysqli_real_escape_string($con,$_POST['protype']);
.

How will you calculate days between two dates in PHP?

Calculating days between two dates in PHP

$date1 = date("Y-m-d");
$date2 = "2015-10-02";
$days = ( strtotime($date1) - strtotime($date1))/(60*60*24);
echo $days;
.

Which operator is used to combine string values in PHP?

Two or more string values can be combined by using ‘.’ operator.

$val1 = "Software ";
$val2 = "Testing";
echo $val1.$val2; // The output is “Software Testing”
.

How is a constant defined in a PHP script?

Defining a Constant in PHP

define('CONSTANT_NAME',value);
.

How to get length of an array in PHP ?

PHP count function is used to get the length or numbers of elements in an array

<?php
// initializing an array in PHP
$array=['a','b','c'];
// Outputs 3 
echo count($array);
?>
.

The value of the variable input is a string 1,2,3,4,5,6,7. How would you get the sum of the integers contained inside input?

<?php
echo array_sum(explode(',',$input));
?>
The explode function is one of the most used functions in PHP, so it’s important to know if the developer knows this function. There is no unique answer to this question, but the answer must be similar to this one.  .

What is cURL in PHP ?

cURL is a library in PHP that allows you to make HTTP requests from the server.

What is the difference between “= =” and “= = =” operators.

“= = =” is called strictly equivalent operator that is used to check the equivalency of two values by comparing both data types and values.

Ex : 10 and “10” are equal by values but are not equal by data type. One is a string and one is a number. So, if the condition will be false and print “n is not equal to 10”.

$n = 10;
if ($n === "10")
echo "n is equal to 10";
else
echo "n is not equal to 10"; //This will print
.

Which function you can use in PHP to open a file for reading or writing or for both?

You can use fopen() function to read or write or for doing both in PHP.

$file1 = fopen("myfile1.txt","r"); //Open for reading
$file2 = fopen("myfile2.txt","w"); //Open for writing
$file3 = fopen("myfile3.txt","r+"); //Open for reading and writing
.

Can you extend a Final defined class?

No, you cannot extend a Final defined class. A Final class or method declaration prevents child class or method overriding..