PHP Interview Questions - Set 6

PHP Interview Questions

This page contains PHP interview questions and answers.

Q1: What are the types of array available in PHP?

There are three types of array in PHP.

  • Indexed array
  • Associative array
  • Multidimensional array

Click here to learn more about arrays in PHP.

Q2: Create an index array having 5 elements in PHP

In the following code we have an index array having 5 elements.

$arr = ['a', 'b', 'c', 'd', 'e'];

We can create the same array using array() function.

$arr = array('a', 'b', 'c', 'd', 'e');

Q3: Create an associative array of your choice in PHP and print the content using any loop

In the following example we have an associative array $assocArr and we are using foreach loop to print its content.

$assocArr = array(
  'name' => 'Yusuf Shakeel',
  'points' => 9.1,
  'status' => 'ACTIVE'
);

foreach($assocArr as $key => $value) {
  echo "<p>{$key}: {$value}</p>";
}

Q4: What is T_PAAMAYIM_NEKUDOTAYIM?

T_PAAMAYIM_NEKUDOTAYIM is a scope resolution operator represented by double colon ::.

In the following code we are using the scope operator.

class MyMathClass {
  const PI = 3.14;
}

echo MyMathClass::PI;

Paamayim Nekudotayim is in Hebrew and means double colon.

Q5: Where is session data stored in PHP?

Session data is generally saved in file on server inside temporary temp directory.

PHP creates a unique session id and saves the file on the server and shares the session id with the client by creating a cookie in the user browser.

By default PHP session is named PHPSESSID.

Each time a client connects with the server it sends the session id with the request. The server then checks the session id and verifies the existence of the session on the server.

Q6: What is the default session timeout in PHP?

By default, session exists for 1440 seconds (24 minutes) on the server.

But this value can be modified in the php.ini file by changing the session.gc_maxlifetime directive.

Q7: Print the directory path where the session is saved in PHP

To get and set the current session path in PHP we have to use the session_save_path function.

The following PHP code will print the current session path.

echo session_save_path();

The following PHP code will set the current session path.

session_save_path('/var/local/temp/session');

Q8: What are the data types in PHP?

Following are the list of 9 primitive data types in PHP.

Scalar types:

  • boolean
  • integer
  • float
  • string

Compound types:

  • array
  • object
  • callable

Special types:

  • resource
  • NULL

Q9: How will you increase or decrease the execution time of PHP code?

For this we have to set the max_execution_time directive in the php.ini file.

In the following example we have set the max execution time to 600 seconds.

ini_set('max_execution_time', 600);

Q10: Can we write multiple inheritance code in PHP?

NO. Multiple inheritance is not supported by PHP.

We can only extends one class. But we can try implementing multiple interface.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT