| by Dhananjay Yadav | 2 comments

PHP Advanced Interview Questions and Answers For Experienced

How we can set default time-zone in PHP?

 

Using library function date_default_timezone_set() we can set default time-zone:

<?php
	date_default_timezone_set("America/New_York");
	echo "Time in Default Zone is : " . date("h:i:sa");
?>

What is difference between == and === ?

 

Both are the comparison operator. The first one (==) is ‘Equal to’ and second one (===) is ‘identical to’. ‘Equal to’ is used to compare the values of variables. ‘Identical to’ is used to compare values as well as type of variables.

For Example :

<?php 
	$x = 1250;
	$y = '1250';

	if($x==$y){
		echo 'X and Y are Equal';
	}else{
		echo 'X and Y are Not equal';
	}

	if($x===$y){
		echo 'X and Y are Identical';
	}else{
		echo 'X and Y are Not Identical';
	}
?>

What is difference between != and !== ?

 

Both are the comparison operator. The first one (!=) is ‘Not Equal’ and second one (!==) is ‘Not identical’. ‘Not equal’ is used to compare the values of variables. ‘Not Identical’ is used to compare values as well as type of variables.

For Example :

<?php 
	$x = 1250;
	$y = '1250';

	if($x!=$y){
		echo 'X and Y are Not equal';
	}else{
		echo 'X and Y are Equal';
	}

	if($x!==$y){
		echo 'X and Y are Not Identical';
	}else{
		echo 'X and Y are Identical';
	}
?>

 

How do I get PHP errors to display?

 

Use the following code snippet to on error reporting in PHP script:

<?php 
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);

But we can not display PHP parse error this way. We need to modify php.ini file and need to ‘on’ the display error:

display_errors = on

What is difference between Notice Errors and Warning Errors in PHP?

 

Notice and Warning are the Non fatal Errors and Not let the script halted.
Notice is an Advisory message regarding what you should prefer to do with this line of code. For example using an undefined variable is a Notice.
notice-error-phpWarning is the message saying that you are going wrong and that may cause an error in future so , you may correct that. For example including a not existing script will throw a Warning.
warning-error-php

What is the use of break and continue in PHP loop?

 

Break : It terminates the loop and exit from the iteration.

Continue : It escape the current iteration and starts new iteration in the loop.

What are common types of errors that occurs in Php execution?

 

Php has Identified numbers of Error detection categories. Following are the common errors in PHP:

E_ERROR : A fatal error and halts the script from further execution.

E_WARNING : Run time warning that detects something is wrong that you need to correct to avoid future errors.

E_PARSE : Compilation error when the code is parsed. We need to on the error reporting in php.ini file to get this.

E_NOTICE : Run time notice to notify that you should go follow the suggested message.

E_CORE_ERROR : It is a fatal error that halts the script and occurs at the initial startup of PHP.

E_CORE_WARNING : Warnings that occurs at the initial startup of PHP.

E_COMPILE_ERROR : Fatal error on compilation occurs at the initial startup of PHP.

E_USER_ERROR : User-generated error message.

E_USER_WARNING : User-generated warning message.

E_USER_NOTICE : User-generated notice message.

E_STRICT : Run-time notices.

E_RECOVERABLE_ERROR : Catchable fatal error indicating a dangerous error

E_ALL : Catches all errors and warnings

Difference between mysql_connect and mysql_pconnect?

 

When mysql_pconnect() is used to create and maintain a persistent connection to the database that means a SQL link that do not close when the execution of your script ends where as mysql_connect() create a new connection each time when it called. When a persistent connection is created and again a connection request is called the mysql_pconnect() return the earlier established connection rather than creating a new instance of connection.

What do you understand by &$name ?

 

Using the reference of the variable rather than a copy of that. If we pass the reference of a variable in a function as an argument and the value is changed then the value of original variable outside the function is also changed. This is passing by reference.