Top PHP Interview Questions And Answers (For ALL)


Q #1) What is PHP?
Answer:
PHP is one of the popular server-side scripting languages for developing a web application.
The full form of PHP is Hypertext Preprocessor. It is used by embedding HTML for creating dynamic content, communicating with a database server, handling session etc.
Q #2) Why do we use PHP?
Answer:
There are several benefits of using PHP. First of all, it is totally free to use. So anyone can use PHP without any cost and host the site at a minimal cost.
It supports multiple databases. The most commonly used database is MySQL which is also free to use. Many PHP frameworks are used now for web development, such as CodeIgniter, CakePHP, Laravel etc.
These frameworks make the web development task much easier than before.
Q #3) Is PHP a strongly typed language?
Answer:
No. PHP is a weakly typed or loosely typed language.
Which means PHP does not require to declare data types of the variable when you declare any variable like the other standard programming languages C# or Java. When you store any string value in a variable then the data type is the string and if you store a numeric value in that same variable then the data type is an Integer.
Sample code:
1
$var = “Hello”; //String
2
$var = 10; //Integer
Q #4) What is meant by variable variables in PHP?
Answer:
When the value of a variable is used as the name of the other variables then it is called variable variables. $$ is used to declare variable variables in PHP.
Sample code:
1
$str = “PHP”;
2
$$str = ” Programming”; //declaring variable variables
3
echo “$str ${$str}”; //It will print “PHP programming”
4
echo “$PHP”; //It will print “Programming”
Q #5) What are the differences between echo and print?
Answer:
Both echo and print method print the output in the browser but there is a difference between these two methods.
echo does not return any value after printing the output and it works faster than the print method. print method is slower than the echo because it returns boolean value after printing the output.
Sample code:
1
echo “PHP Developer”;
2
$n = print “Java Developer”;
Q #6) How can you execute PHP script from the command line?
Answer:
You have to use PHP command in the command line to execute a PHP script. If the PHP file name is test.php then the following command is used to run the script from the command line.
php test.php
Q #7) How can you declare the array in PHP?
Answer:
You can declare three types of arrays in PHP. They are numeric, associative and multidimensional arrays.
Sample code:
1
//Numeric Array
2
$computer = array(“Dell”, “Lenavo”, “HP”);
3
//Associative Array
4
$color = array(“Sithi”=>”Red”, “Amit”=>”Blue”, “Mahek”=>”Green”);
5
//Multidimensional Array
6
$courses = array ( array(“PHP”,50), array(“JQuery”,15), array(“AngularJS”,20) );
Q #8) What are the uses of explode() and implode() functions?
Answer:
explode() function is used to split a string into an array and implode() function is used to make a string by combining the array elements.
Sample code:
1
$text = “I like programming”;
2
print_r (explode(” “,$text));
3
$strarr = array(‘Pen’,’Pencil’,’Eraser’);
4
echo implode(” “,$strarr);
Q #9) Which function can be used to exit from the script after displaying the error message?
Answer:
You can use exit() or die() function to exit from the current script after displaying the error message.
Sample code:
1
if(!fopen(‘t.txt’,’r’))
2
exit(” Unable to open the file”);
Sample code:
1
if(!mysqli_connect(‘localhost’,’user’,’password’))
2
die(” Unable to connect with the database”);
Q #10) Which function is used in PHP to check the data type of any variable?
Answer:
gettype() function is used to check the data type of any variable.
Sample code:
1
echo gettype(true).”; //boolean
2
echo gettype(10).”; //integer
3
echo gettype(‘Web Programming’).”; //string
4
echo gettype(null).”; //NULL

Leave a Reply