php interview questions for 1 year experience - the best interview questions

php interview questions for 1 year experience - the best interview questions

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 sessions, etc.

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.

Recommended reading =>> Laravel Database handling

These frameworks make the web development task much easier than before.


3) Is PHP a strongly typed language?

Answer: No. PHP is a weakly typed or loosely typed language.

This 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:

$var = "Hello"; //String
$var = 10; //Integer

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:

$str = "PHP";
$$str = " Programming"; //declaring variable variables
echo "$str ${$str}"; //It will print "PHP programming"
echo "$PHP"; //It will print "Programming"

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 the boolean value after printing the output.

Sample code:

echo "PHP Developer";
$n = print "Java Developer";

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

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:

//Numeric Array
$computer = array("Dell", "Lenavo", "HP");
//Associative Array
$color = array("Sithi"=>"Red", "Amit"=>"Blue", "Mahek"=>"Green");
//Multidimensional Array
$courses = array ( array("PHP",50), array("JQuery",15), array("AngularJS",20) );

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:

$text = "I like programming";
print_r (explode(" ",$text));
$strarr = array('Pen','Pencil','Eraser');
echo implode(" ",$strarr);

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:

if(!fopen('t.txt','r'))
exit(" Unable to open the file");
Sample code:

if(!mysqli_connect('localhost','user','password'))
die(" Unable to connect with the database");

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:

echo gettype(true).''; //boolean
echo gettype(10).''; //integer
echo gettype('Web Programming').''; //string
echo gettype(null).''; //NULL

11) How can you increase the maximum execution time of a script in PHP?

Answer: You need to change the value of the max_execution_time directive in the php.ini file for increasing the maximum execution time.

For Example, if you want to set the max execution time for 120 seconds, then set the value as follows,

max_execution_time = 120

12) What is meant by ‘passing the variable by value and reference’ in PHP?

Answer: When the variable is passed as value then it is called pass variable by value.

Here, the main variable remains unchanged even when the passed variable changes.

Sample code:

function test($n) {
$n=$n+10;
}
 
$m=5;
test($m);
echo $m;
When the variable is passed as a reference then it is called pass variable by reference. Here, both the main variable and the passed variable share the same memory location and & is used for reference.

So, if one variable changes then the other will also change.

Sample code:

function test(&$n) {
    $n=$n+10;
}
$m=5;
test($m);
echo $m;

13) Explain type casting and type juggling.

Answer: The way by which PHP can assign a particular data type for any variable is called typecasting. The required type of variable is mentioned in the parenthesis before the variable.

Sample code:

$str = "10"; // $str is now string
$bool = (boolean) $str; // $bool is now boolean
PHP does not support datatype for variable declaration. The type of the variable is changed automatically based on the assigned value and it is called type juggling.

Sample code:

$val = 5; // $val is now number
$val = "500" //$val is now string

14) How can you make a connection with MySQL server using PHP?

Answer: You have to provide MySQL hostname, username, and password to make a connection with the MySQL server in mysqli_connect() method or declaring database object of the mysqli class.

Sample code:

$mysqli = mysqli_connect("localhost","username","password");
$mysqli = new mysqli("localhost","username","password");

15) How can you retrieve data from the MySQL database using PHP?

Answer: Many functions are available in PHP to retrieve the data from the MySQL database.

Few functions are mentioned below:

a) mysqli_fetch_array() – It is used to fetch the records as a numeric array or an associative array.

Sample code:

// Associative or Numeric array
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
echo "Name is $row[0] 
";
echo "Email is $row['email'] 
";
b) mysqli_fetch_row() – It is used to fetch the records in a numeric array.

Sample code:

//Numeric array
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result);
printf ("%s %s\n",$row[0],$row[1]);
c) mysqli_fetch_assoc() – It is used to fetch the records in an associative array.

Sample code:

// Associative array
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result);
printf ("%s %s\n",$row["name"],$row["email"]);
d) mysqli_fetch_object() – It is used to fetch the records as an object.

Sample code:

// Object
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result);
printf ("%s %s\n",$row->name,$row->email);

16) What are the differences between mysqli_connect and mysqli_pconnect?

Answer:

mysqli_pconnect() function is used for making a persistent connection with the database that does not terminate when the script ends.

mysqli_connect() function searches any existing persistence connection first and if no persistence connection exists, then it will create a new database connection and terminate the connection at the end of the script.

Sample code:

$DBconnection = mysqli_connect("localhost","username","password","dbname");
// Check for valid connection
if (mysqli_connect_errno())
{
echo "Unable to connect with MySQL: " . mysqli_connect_error();
}
mysqli_pconnect() function is depreciated in the new version of PHP, but you can create a persistence connection using mysqli_connect with the prefix p.

17) Which function is used in PHP to count the total number of rows returned by any query?

Answer:

mysqli_num_rows() function is used to count the total number of rows returned by the query.

Sample code:

$mysqli = mysqli_connect("hostname","username","password","DBname");
$result=mysqli_query($mysqli,"select * from employees");
$count=mysqli_num_rows($result);

18) How can you create a session in PHP?

Answer:

session_start() function is used in PHP to create a session.

Sample code:

session_start(); //Start session
$_SESSION['USERNAME']='Fahmida'; //Set a session value
unset($_SESSION['USERNAME']; //delete session value

19) What is the use of imagetypes() method?

Answer: image types() function returns the list of supported images of the installed PHP version. You can use this function to check if a particular image extension is supported by PHP or not.

Sample code:

//Check BMP extension is supported by PHP or not
if (imagetypes() &IMG_BMP) {
    echo "BMP extension Support is enabled";
}

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

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

Sample code:

$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

Post a Comment

Previous Post Next Post