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.

Why we use extract() in PHP?

The extract() function imports variables into the local symbol table from an array. It uses variable names as array keys and variable values as array values. For each element of an array, it creates a variable in the current symbol table. Following is the syntax.

extract(array,extract_rules,prefix);
Let’s see an example.

$varArray=array("course1"=>"PHP","course2"	=>"JAVA" , "course3"=>"HTML");
extract ($varArray ) ; 
echo "$course1, $course2, $course3" ;

.

What Is PDO in PHP?

PDO stands for <PHP Data Object>.

It is a group of PHP extensions that give a core PDO class and database, specific drivers. It provides a vendor-neutral, lightweight, data-access abstraction layer. Thus, in spite of what database we tend to use, the function to issue queries and fetch data will be the same.

It focuses on data access abstraction rather than database abstraction.

PDO needs familiarised options within the core of PHP 5. Therefore, it’ll not run with earlier versions of PHP.

PDO divides into two components.

  • The core that provides the interface.
  • Drivers to access explicit driver.
.

How can we connect to a MySQL database from a PHP script?

To be able to connect to a MySQL database, we must use mysqli_connect() function as follows:

$database=mysqli_connect("HOST","USER_NAME","PASSWORD");
mysqli_select_db($database,"DATABASE_NAME");
.

How can we encrypt and decrypt a data presented in a table using MySQL?

You can use functions: AES_ENCRYPT() and AES_DECRYPT() like:

AES_ENCRYPT(str,key_str);
AES_DECRYPT(crypt_str,key_str);
.

How many ways we can we find the current date using MySQL?

We can find current date using MySQL in different ways, they are:

SELECT CURDATE();
SELECT CURRENT_DATE();
SELECT CURTIME();
SELECT CURRENT_TIME();

.

How can we find the number of rows in a result set using PHP?

Here is how you can find the number of rows in a result set in PHP:

$result=mysql_query($any_valid_sql,$database_link);
$num_rows=mysql_num_rows($result);
echo“$num_rows rows found”;
.

What are the other commands to know the structure of a table using MySQL commands except EXPLAIN command?

DESCRIBE table_name;
.

How many values can the SET function of MySQL take?

MySQL SET function can take zero or more values, but at the maximum it can take 64 values.

.

What is the difference between mysql_fetch_object and mysql_fetch_array?

MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array

.

How can we take a backup of a mysql table and how can we restore it?

Create a full backup of your database:

shell >mysqldump tab =/path/to/some/diropt db_name
Or
shell >mysqlhotcopy db_name /path/to/some/dir

The full backup file is just a set of SQL statements, so restoring it is very easy:

shell >mysql "."Executed"; mysql_close($link2);
.