PHP Programming Technical Interview Questions

What is PHP?

PHP is a type of scripting language, also known as interpreted programming. It is primarily used for web development, but is also commonly used for general-purpose programming. PHP originally stood for ‘Personal Home Page’, but now stands for ‘PHP: Hypertext Preprocessor’. It was developed by Rasmus Lerdorf in 1994.

It is an open-source programming language which means that it can be used by anyone free of cost. It can also be ported for use across many platforms, including Linux, Unix, Mac OS and Windows. Its primary use is to make webpages more dynamic, by allowing users to embed PHP code directly into HTML code. Alternately it can also be combined with various web template systems, web content management systems and web frameworks.

PHP is executed at the server side, which means that it executes on the web server where the webpage is created, rather than on the computer accessing the page. Another advantage of PHP is that it can access more than 20 different databases, including MySQL. Additionally, it can also manipulate the information on those databases based on input from an online user.

How to embedded PHP code in HTML code?

In order to embedded PHP code, the tags <?php and ?> are used. The <?php is the opening command, whereas ?> is the closing command. The code is embedded directly into HTML code. This is possible because PHP has been designed to directly interact with HTML.

Sample of PHP code in HTML Code:

<html>
<head>
<title>Encoding PHP in HTML</title>
</head>
<body>
This is normal HTML code
<?php
  
               // php code here
               for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li>
Back into normal HTML
</body>
</html>

What are the methods available in form submitting?

The command <form> allows one to send the form data to a web server. The <form method=""> command tells the browser how to send form data to a web server. Here, there are two methods to send the form across: GET and POST. Each has their own set of advantages and disadvantages. GET is primarily used for short forms that do not contain sensitive information. POST is primarily useful to submit sensitive information, such as passwords.

What are the differences between GET and POST methods in form submitting?

GET and POST are two different methods that can be used to submit form data to a web server. Each has their own set of advantages and disadvantages.

<form method="GET">

Specifying the value of GET means that the browser will add the form contents to the end of the URL. This allows the browser to cache the results of the form submission, as well as allows the user to bookmark the page once the form has been submitted. However, it also allows the entire contents of the submission to be visible in the URL. This is why GET is primarily used for short forms that do not contain sensitive information. GET can also be only used to send ASCII data and only about 3000 characters.

<form method="POST">

Specifying the value of POST means that the browser will send the data to the web server to be processed. It allows one to add data to a database. It is primarily useful to submit sensitive information, such as passwords. It also does not allow one to bookmark the results of a form submission. However, if the form is submitted twice, it will result in a duplicated entry which can be problematic in some scenarios.

How can we submit from without a submit button?

There may be times where one would have to submit a form without having access to a submit button. In these cases, there are four main ways to submit the form:

  • Submitting it by clicking on a link
  • Submitting it by selecting an option from drop down box by utilizing the onChange command
  • By using JavaScript, specifically the command: document.form.submit();
  • By using PHP, specifically the command: header(“location:page.php”);

By using the header Location tag in PHP allows one to send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

How can we get the browser properties using PHP?

There may be times where one finds themselves in a situation having the need to look up the browser properties. One can do this via the use of PHP code, specifically the Echo command.

The following code will return an array of the browser name, version and platform.

<html>
<body>

<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>

</body>
</html>
What is a Session?

In PHP, a session is a way to store data and information. It allows the same information to be used across various pages. Sessions provide a simple way to store data for individual users against a unique session ID. They can be used to store information in between page requests.

A session creates a file in a temporary directory on the server. This is where registered session variables and their values are stored. This data will be available to all pages on the site during that visit. This is similar to a cookie; however, unlike a cookie, the information is not stored on the user’s computer, but rather on the server thus making the data safer.

The location of the temporary file is determined by a setting in the php.ini file called session.save_path. A new session can be started by using the session_start() function.

How can we register the variables into a session?

A session is a simple and effective way to store information. It works primarily like cookies, where user information is saved. However, unlike a cookie, the information is not stored on the user’s computer, but rather on the server thus making the data safer.

In order for the information to be saved, a session must be started. A new session can be started by using the session_start() function.

A variable can be registered into a session using the $_SESSION function.

For example:

<?php

// Starting session

session_start();


// Storing session data

$_SESSION["firstname"] = "Peter";

$_SESSION["lastname"] = "Parker";

?>
How do you destroy a particular or all Sessions?

A session can be used to store information, which can then be used across various pages. The concept works similarly like a cookie; however, unlike a cookie, the information is not stored on the user’s computer, but rather on the server thus making the data safer.

A new session can be started by using the session_start() function. A variable can be registered into a session using the $_SESSION function.

Once the user logs out, the session is automatically closed. However, if the user wishes to remove a certain session data, it can be done so by using the unset $_SESSION function.

For Example:

// Removing session data

if(isset($_SESSION["lastname"])){

    unset($_SESSION["lastname"]);

}

?>

However, if the user wants to destroy a particular or all Sessions, it can be done so via the session_destroy() function.

// Destroying session

session_destroy();

?>
How many ways we can pass the variable through the navigation between the pages?

There are three ways in which the variable can be passed through the navigation between the pages:

1. By starting a session and registering the variables that need to be passes. As a session can be used between pages, the variables can then be accessed on other pages. On the other pages, the session can be started again and the variables accessed.

2. Another option is to use query string to pass the variables.

3. The third option is with the use of a form, i.e. $_POST or $_GET depending on the typr of form used. The form can be used to pass variables that have been used in the form to the page specified with action attribute.

What are the different functions in sorting an array?

There are six different functions that can be used to sort an array. All of which sort the array in different manners. Some sort based on the array keys, whereas others by the values. It should be noted that all of these functions act directly on the array variable itself, as opposed to returning a new sorted array.

The six functions are as follows:

  • sort() – which sorts arrays in ascending order
  • rsort() – which sorts arrays in descending order
  • asort() – which sorts associative arrays in ascending order, according to the value
  • ksort() – which sorts associative arrays in ascending order, according to the key
  • arsort() – which sorts associative arrays in descending order, according to the value
  • krsort() – which sorts associative arrays in descending order, according to the key
How can we know the total number of elements of Array?

Sometimes it might be necessary to know the total number of elements of Array. There is a function that can be used to easily determine that number. In order to find the total number of elements of Array, one can use the count() Function.

The count() Function is simple enough, it returns the number of elements in an array.

What type of headers does PHP support?

A header is used to talk between a browser and a server. The primary function of a header() function is to send a  raw HTTP header to a client. However, it should be noted that a header() must be called before any actual output is sent.

In that regard, PHP supports the following header:

  • header() – sends HTTP response headers
  • Location header – redirects the browser
  • Refresh header – adds a delay before  redirecting the browser
  • Content-Type header – tells the browser what type of data the server is about to send
How can we extract string ‘abc.com’ from a string ‘http://info@abc.com’ using regular _expression of php?

To extract the string 'abc.com' from string ‘http://[email protected]’ we are going to use preg_match. Preg_match is used for matching strings with regular expressions.
Its syntax is:

int preg_match( string $pattern , string $subject [,array &$matches [, int $flags = 0 [, int$offset = 0 ]]] )

We use preg_match with the first two parameters if we are simply checking for matching. If we want to extract the matched string, then we used the third parameter.
In our case, the syntax will be:

preg_match(“/.*@(.*)$/”,”http://[email protected]”,$result);
echo $result[1];
How can we connect a database using php?

To connect the database using php we use the following code.

$conn = mysqli_connect("localhost","username","password","db_name");

where:
localhost = Host name
username = MySQL username
password = MySQL password
db_name = Database name to connect.
 

Explain include(), include_once, require() and require_once?

Include(), include_once, require() and require_once are different functions that often used in PHP coding. 

  • include(): The include() function is used to insert a php file into the current php file. In execution, if an error occurs, the include() function gives a warning and the script still gets executed.
  • include_once(): The include_once() function inserts the php file into the current php file only once. The php file will be called only once and any more calling of that file will be ignored.
  • require(): Require() function is similar to include() function. Only difference is that, if an error occurs in execution, it treats it as a fatal error and the script stops execution.
  • require_once(): The require_once() includes the php file into the current php file only once.
What are the different types of errors in php?

An error is a coding mistake. In php there are 4 different types of errors:

  • Notice: It is used to give out a warning or information to the user. This error does not stop the execution of the script. This occurs when we try to access undefined variables.
  • Warning: Warning errors do not stop execution of the script. Warning error is given whenever a call is made to a missing file or to undefined constants.
  • Fatal Errors: When a piece of code is written to do something that cannot be done, Fatal error occurs. If you are trying to call undefined functions it gives out fatal error. Fatal error stops the execution of the script.
  • Parse Errors: If there is a mistake in syntax in the script, then the output is Parse error. A parse error also stops the execution of the script.
What are the Formatting and Printing Strings available in PHP?

The formatting and printing strings available in PHP are printf() and sprintf(). These can be used to format the string in different ways. When we need to format a string and print it, printf() function is used. If we have to format and return the string sprintf() is used.

The format specifiers are:

  • %b - binary
  • %c - ASCII character
  • %d - signed decimal number
  • %e - scientific number
  • %u - unsigned decimal number
  • %f - float with local settings
  • %F - float without local settings
  • %o - octal number
  • %x - lowercase hexadecimal
  • %X - uppercase hexadecimal
     

Comments

thank you for this solution it is helpful

thank u very much

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.