PHP Interview Questions & Answers for experienced

PHP Interview Questions & Answers for experienced are provided here. TutorialsClass helps you to test your knowledge and enhance your experience.
Read PHP Interview Questions & Answers for experienced candidates to get your dream job.

How can we submit a form without a submit button?

Java script submit() function is used for submit form without submit button. For example: document.formname.submit()

<form action="page2.php" name="contactform">// Form elements</form>

What is PHP floor() function?

With the help of the floor() function, we can convert a numeric value to it’s next lower integer value.


Example of floor() function in PHP:

<?php
$a=-145.13;
$result=floor($a);
echo "Floor value of $a is $result";    // Output: Floor value of -145.13 is -145
?>

What is the difference between mysqli_fetch_array() and mysqli_fetch_object()?

Both PHP functions are used to fetch data from MYSQL database, but there is a difference between mysqli_fetch_array() and mysqli_fetch_object() output format.

With the help of mysqli_fetch_array() function, we can fetch a query’s result data row as an array.

Example of mysqli_fetch_array() Function:

<?php
$conn=mysqli_connect("localhost","username","password","db");
$query="SELECT students, marks FROM class";
$result=mysqli_query($conn,$query);
while ($arr=mysqli_fetch_array($result))
{
    echo $arr['students']." ".$arr['marks'];
    echo "<br />";
}
mysqli_close($conn);
?>

While mysqli_fetch_object() function, we can fetch a query’s result data row as an object.

Example of mysqli_fetch_object() Function:

<?php
$conn=mysqli_connect("localhost","username","password","db");
$query="SELECT students, marks FROM class";
$result=mysqli_query($conn,$query);
while ($obj=mysqli_fetch_object($result))
{
    echo $obj->students." ".$obj->marks;
    echo "<br />";
}
mysqli_close($conn);
?>

How can you pass a variable by reference in PHP?

By using an ampersand (&) sign in-front of the variable, we can pass the variable by reference.

Why or when to use variable by reference ?

When PHP function arguments are passed by value, and value is changed within function, it does not change variable value outside the function. To allow a function to modify its argument value outside the function, they must be passed by reference.

Example:

<?php
function add_line(&$line)
{
    $line.= 'and extra words added.';
}
 
$sample_line= 'This is a sample line, ';
add_line($sample_line);
echo $sample_line;    // outputs 'This is a sample line,  and extra words added.'
?>

If we do not use value by reference (&), then the output will be: ‘This is a sample line,’

How you can force maximum execution time error in PHP?

We can use sleep function to consume PHP execution time and it will throw PHP error.

<?php
  
  for($i=0; $i<10; $i++)
  {
    echo $i;
    sleep(300); //delay the exicution of the PHP script
  }
  ?>
Tutorials Class - Output Window

maximum execution time error in php

How to increase maximum execution time in PHP and WordPress

We mostly see that the maximum execution time of a PHP Script is 30 seconds. If a script takes more than 30 seconds to execute, it will throw the following Fatal error.
Fatal error: Maximum execution time of 30 seconds exceeded in ….

We can increase maximum execution time in PHP and WordPress by using various methods. In the following examples, we have increased maximum execution time to 360 seconds (6 minutes).


Method 1: By adding the code in a PHP file:

We can set the maximum execution time by adding this code in a PHP File. You must add this code in that file which is included earlier in all other pages. Then this function will increase maximum execution time in all those files.
For example, put this code in the header (header.php), or configuration (config.php) if available.
set_time_limit(360);


Method 2: Inside the .htaccess file:

We can do the same thing inside the .htaccess file as well. Before editing this file, you make sure to have a backup of .htaccess.
php_value max_execution_time 360


Method 3: Inside the php.ini file:

We can also set the maximum execution time by adding this code in the php.ini file.
max_execution_time = 360


Method 4: Inside wp-config.php file in WordPress:

If you are using WordPress then you can place this code in wp-config.php file.
set_time_limit(360);


Note: Note: These methods will only work if you are allowed to change PHP configuration by your Hosting Server.