Wednesday, 26 July 2017

PHP interview questions and answers for 4 year experience

PHP interview questions and answers for 4 year experience

Question: Difference between include and require?
include: It include a file, It does not find through an warning.
require: It include a file, It does not find through an fatal error, process exit.


Question: How to get IP Address of client?
$_SERVER['REMOTE_ADDR'];



Question: How to get IP Address of Server?
$_SERVER['SERVER_ADDR'];



Question: What is output of following?
$a = '10';
$b = &$a;

$b = "2$b";

echo $a;
echo $b;

Output
210
210



Question: What are the main error types?
  1. Notices: Simple, non-critical errors that are occurred during the script execution.Ideally we should fix all notices.
    echo $undefinedVar;
    
  2. Warning: more important than Notice but it is also non-critical errors that are occurred during the script execution. It should be fixed.
    include "non-ExistFile.php";
    
  3. Error: critical errors due to which script execution stopped. It must be fixed.
    require "non-ExistFile.php";
    



Question: How to enable/disable error reporting?
Enable error
error_reporting(E_ALL);

Disable error
error_reporting(0);



Question: What are Traits?
Traits are a mechanism that allows you to create reusable code in PHP where multiple inheritance is not supported. To create a Traits we use keyword trait.
Example of Traits
trait users {
    function getUserType() { }
    function getUserDescription() {  }
    function getUserDelete() {  }
}

class ezcReflectionMethod extends ReflectionMethod {
    use users;    
}

class ezcReflectionFunction extends ReflectionFunction {
    use users;
}



Question: How to extend a class defined with Final?
No, You can't extend. A class declare with final keyword can't be extend.


Question: What is full form of PSRs?
PHP Standards Recommendations


Question: What is the difference between $message and $$message?
$message is a simple variable whereas $$message is variable whose name is stored in another variable ($message).


Question: How to destroy the session?
session_destroy



Question: How do you execute a PHP script from the command line?
  1. Get the php.exe path from server (My PHP path is : D:\wamp\bin\php\php5.5.12)
  2. In environment variable, Add this path path under PATH variable.
  3. Re-start the computer.
  4. Now, you can execute php files from command file.
    php hello.php



Question: How to repair the session?
REPAIR TABLE tablename 
REPAIR TABLE tablename QUICK 
REPAIR TABLE tablename EXTENDED 



Question: Are Parent constructors called implicitly inside a class constructor?
No, You need to call explicitly.
parent::constructor($value);



Question: What does $_GLOBALS means?
It is associate variable which have all the global data of GET/POST/SESSION/_FILES/ENV/SERVER/GLOBALS etc.
Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (    
            [_gu] => 1cf7097e-eaf0-486d-aa34-449cf1164ba8
            [_gw] => 2.127103(sc~1,s~oru42c,a~1)127418(sc~1,s~oru42c)u[~0,~0,~0,~0,~0]v[~ev3o1,~4,~0]a()
            [PHPSESSID] => ki18pnad1knttqv5i6233sjem7
        )

    [_FILES] => Array
        (
        )

    [_ENV] => Array
        (
        )

    [_REQUEST] => Array
        (
        )

    [_SERVER] => Array
        (            
            [REQUEST_TIME_FLOAT] => 1501064295.228
            [REQUEST_TIME] => 1501064295
        )

    [GLOBALS] => Array
 *RECURSION*
    [_SESSION] => Array
        (
        )

)



Question: How is it possible to parse a configuration file?
parse_ini_file('config.ini');


Question: What is the difference between characters \034 and \x34? \034 is octal 34 and \x34 is hex 34.


Question: Explain soundex() and metaphone().?
soundex() function calculates the soundex key of a string. A soundex key is a four character long alphanumeric strings that represents English pronunciation of a word.
$str= "hello";
Echo soundex($str);

metaphone() the metaphone() function calculates the metaphone key of a string. A metaphone key represents how a string sounds if pronounced by an English person.

Question: How to start displaying errors in PHP application ? Add following code in PHP.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

OR
Add following code in .htacess
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag  log_errors on




Question: What is stdClass?
It is PHP generic empty class.
stdClass is used to create the new Object. For Example
$newObj = new stdClass();
$newObj->name='What is your name?';
$newObj->description='Tell me about yourself?';
$newObj->areYouInIndia=1;
print_r($newObj);

Output
stdClass Object
(
    [name] => What is your name?
    [description] => Tell me about yourself?
    [areYouInIndia] => 1
)



Question: What is output of below?
$a = '1';
$b = &$a;
$b = "2$b";
echo $a.", ".$b;

output
21, 21


Question:What is the difference between GET and POST?
  1. GET displays the submitted data as part of the URL whereas post does not show.
  2. GET can handle a maximum of 2048 characters, POST has no such restrictions
  3. GET allows only ASCII data, POST has no restrictions, binary data are also allowed.
  4. Normally GET is used to retrieve data while POST to insert and update.



Question:What is traits and give example?
Traits are a mechanism for code reuse in single inheritance languages such as PHP.
class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();


output
Hello World