Friday 14 April 2017

php interview Question with answer

An    Interview , in   PHP . best he answered

Raj  is having  well hands-on experince in php , He  is getting into php technical interview round

 Interviewer started  by asking .

 "  What are different  areas , where  PHP  can be used ?"

Raj replied :

     There are three main areas where PHP scripts are used.

              1. Server-side scripting :  Php  run on  the web server, with a connected PHP installation. You can access the PHP program output with a web browser .

      2. Command Line Scripting : PHP Script will  also  run without any server or browser with help of  PHP parser .
            3 . Writing desktop applications : PHP can also  be used  to create a desktop application with a  graphical user interface .We can  use PHP-GTK  to write such programs.

Interviewer : 

 "Explain file handling in php  and all  their diffrent operation   like(reading , writing updating etc..)in detail"

Raj replies : 

PHP has several functions for     Creating,    Reading,    Open and Closing    and Editing Files.

PHP readfile() Function  , The readfile() function reads a file and  writes  it to the output buffer. Example is given below

  PHP Open File - fopen() . A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.Example is given below.
  PHP Read File - fread().  The fread() function reads from an open file.  The first parameter of fread() contains the name of  the file to read from and the second parameter specifies the maximum number of bytes to read. Example is given below

     PHP Close File - fclose(). The fclose() function is used to close an open file. Example  is given below.

   PHP Read Single Line - fgets() . The fgets() function is used to read a single line from a file. Example is given below

PHP Check End-Of-File - feof(). The feof() function checks if the "end-of-file" (EOF) has been reached.Example is given below

PHP Read Single Character - fgetc() .The fgetc() function is used to read a single character from a file. Example is given below.

PHP Write to File - fwrite() .The fwrite() function is used to write to a file.Example is given below

Interviewer next question :

"Whats the difference between mysql_connect  and  mysql_pconnect  ?"

Raj Replied : 

mysql_connect - Open a connection to a MySQL Server. Connection will be closed as soon as the execution  of the script ends, unless it's closed earlier by  explicitly calling mysql_close().Example  is given below.

While mysql_pconnect   Establishes a persistent connection to a MySQL server. When a persistent connection is requested,  PHP checks if there's already an identical persistent  connection (that remained open from earlier) - and if it exists, it uses it.
Example is given below.

Interviwer next question :

Explain PHP MySql functions :  mysql_fetch_array , mysql_fetch_assoc ,  mysql_fetch_row , mysql_fetch_object and their  differences.

Raj replied :

mysql_fetch_array    Fetch a result row as an associative array, a numeric array, or both

     mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] ) 
result_type is a constant and can take the following values: 

Example is given below.

mysql_fetch_assoc    Fetch a result row as an associative array 

mysql_fetch_assoc ( resource $result )

Example  is given below

mysql_fetch_row    Fetches one row of data from the  result associated with the specified result identifier. The row is returned as an array.  

mysql_fetch_row ( resource $result )

Example is given below

 mysql_fetch_object    Fetch a result row as an object 
mysql_fetch_object( resource $result )

Example is given below 

Interviewer next question :

 What is difference between COOKIES  and SESSION. Explain both.

Raj replied :

   A COOKIE is a small piece of text stored on a user's  computer by their browser. Each time the users' web browser interacts with a web server  it will pass the cookie information to the web server.

This means that cookies that relate to will not be sent  to
   Common uses for COOKIES are authentication, storing of site preferences, shopping cart items,and server session identification.

 A SESSION can be defined as a server-side storage of  information that is desired to persist throughout the  user's interaction with the web site or web application. 

Instead of storing large and constantly changing  information via COOKIES in the user's browser, only a unique identifier is stored on the client side  (called a "session id(SSID)"). 

This session id is passed to the web server every time  the browser makes an HTTP request (ie a page link or AJAX request). 

The web application pairs this session id with it's internal database and retrieves  stored variables for use by the requested page.

Interviewer  next question :

 What is Sql injection and how  it can be prevented? 

Raj replied:

 SQL Injection is the placement of malicious code in SQL statements, via web page input.

             SQL Injection is one of the most common web hacking techniques.

  For Example The original purpose of the below code was to create  an SQL statement to select a user, with a given user id.

 If there is nothing to prevent a user from entering "wrong" input, the user can enter some "smart" input  like this: , UserId: 105 OR 1=1

Then, the SQL statement will look like this: , SELECT * FROM Users WHERE UserId = 105 OR 1=1; 

 The SQL above is valid and will return ALL rows from the "Users" table, since OR 1=1 is always TRUE.

 A hacker might get access to all the user detail,  that will be very dangerous.


 SQL Injection  can prevented by  using
       (1). mysqli_real_escape_string

     The mysqli_real_escape_string() function escapes  special characters in a string for use in an SQL statement.     

 Another way preventing SQL Injection  is by  using
  using prepared statment

     Example is given below 

Interviewer  next question :

What is   CrossSite  Scripting ?

Raj replied:

Cross-Site Scripting (XSS) attacks are a type of  injection, in which malicious scripts are injected into   trusted web sites. 
        Example is given below 

The end user’s browser has no way to know that the script should  not be trusted, and will execute the script , because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser and passes  to attcker  . 
          These scripts can even rewrite the content of the HTML page. 
 Example is given below

Interviewer   Next Question :

Explain  Rest Services .

Raj replied :

REST defines a set of architectural principles by which you can design Web services that focus on a system's resources, including how resource states  are addressed and transferred over HTTP by a wide range of clients written in different languages.

REST Web service follows four basic design principles:
    * Use HTTP methods explicitly.
    * Be stateless.
    * Expose directory structure-like URIs.
    * Transfer XML, JavaScript Object Notation (JSON), 
           or both.

Interviewer next question :

Explain  SOAP.

Raj replied :

 SOAP  stands for Simple Object Access Protocol.

SOAP provides the envelope for sending  Web Services messages over the Internet/Internet.  It is part of the set of standards specified by      the W3C

The SOAP envelope contains two parts:

*  An optional header providing information on authentication,   encoding of data, or how a recipient of a     SOAP message should process the    message.

*  The body that contains the message. These messages can be defined using the WSDL specification.

Interviewer  next question

 What is difference  between include  and require ?

Raj replied:

With the include or require statement. We used to insert the content of one PHP/html file into  another PHP file (before the server executes it)

    In  include - (if source file missing it'll give warning    and continue execution),    while in require - (if source file missing it'll give         error  and halt execution),)

      With the include or require statement. We used to insert the content of one PHP/html file into  another PHP file (before the server executes it)

      In  include - (if source file missing it'll give warning    and continue execution),    while in require - (if source file missing it'll give  error  and halt execution),)

Interviewer Next Question :

Explain session_set_save_handler   ?

Raj replied :

To maintain the session data, we can use session_set_save_handler function.

This function provide 6 callback functions which call automatically.

   openFunction will be called automatically when session start.
   closeFunction will be called automatically when session end.
    readFunction will be called automatically when you read the session.
  writeFunction will be called automatically when you write in the session.
  destroyFunction will be called automatically when you destroy in the session.
   gcFunction will be called automatically when session inactive for long time.

Interviewer Next Question :

 How to convert string to array   and array to string in php?

Raj replied:

 Explode convert string to array ,   example given below

Implode convert array to string   ,    example given below


Interviewer  Next Question :

  How to scrape the data from website using CURL?

Raj replied :

 To scrap the data from website, Website must be  public and open for scrapable. In the blow code, Just update the CURLOPT_URL  to which websites data you want to scrap.

Interviewer Next Question :

How to explode a string using multiple delimiters("," and "|" )?

Raj replied:

  To explode a string with multiple delimeters , we use preg_split

 $string='php, interview | questions , and | answers ';
$output = preg_split( "/(\,|\|)/", $string );
print_r($output );

  Raj replied to  all question    very well



  1. This comment has been removed by the author.

  2. But the web application developers still need focus on the readability and reusability of the PHP code to maintain and update the web applications quickly in future.

  3. This article is an appealing wealth of informative data that is interesting and well-written. I commend your hard work on this and thank you for this information. You’ve got what it takes to get attention. hoc tieng anh qua bai hat


Search Any topic , section , query