Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How to terminate the execution of a script in PHP?

Answer»

To terminate the EXECUTION of the script in PHP, the EXIT() function is used. It is a built-in function that outputs a message and then terminates the current script.

The message which you want to display is passed as a PARAMETER to the exit() function. Script termination will be done by this function after displaying the message. It is an alias of die() function. It doesn’t return any value.

Syntax: exit(message)

Where the message is a parameter to be passed as an argument. It defines a message or status.

Example: The code given below will print a message and exit the current script.

<?php$SITE = "https://www.interviewbit.com//";fopen($site,"r")or exit("Unable to connect to $site");?>Conclusion

PHP proves to be a great tool for writing dynamic web pages. It is not restricted to use by professional web developers. Non-technical users can also easily learn a few handy tricks to make their web pages easier to manage thereby MAKING them more useful.

Important Resources:

PHP Developer

PHP Developer Salary

Features of PHP

Difference Between PHP 5 and 7

PHP Frameworks

PHP Projects

PHP IDE

 

2.

Explain type hinting in PHP

Answer»

In PHP, TYPE hinting is used to specify the expected DATA type (arrays, objects, interface, etc.) for an argument in a function declaration. It was introduced in PHP 5.

Whenever the function is called, PHP checks if the arguments are of a user-preferred type or not. If the argument is not of the specified type, the run time will DISPLAY an error and the program will not execute.

It is helpful in better code organization and improved error messages.

Example usage:

//sendEmail() function argument $email is type hinted of Email CLASS. It means to call this function you must have to PASS an email object otherwise an error is generated.<?php function sendEmail (Email $email) { $email->send(); }?>
3.

Differentiate between GET and POST

Answer»

The difference between GET and POST are given below:

GETPOST
GET method is used for requesting data from a specified resource.POST is used for sending the data to the server as a PACKAGE in a separate communication with the processing script.
Data is sent in the form of URL parameters which are strings of name-value pairs separated by ampersands(&)Data sent through the POST method will not be seen in the URL
GET method cannot be used for sending binary data like images or word documentsThe POST method can be used to send ASCII as well as binary data like images and word documents
This method must not be used if you have any sensitive information like a PASSWORD to be sent to the server.Sensitive information can be sent using this method.
It can be used for submitting the form where the user can bookmark the result.Submissions by form with POST cannot be bookmarked.
You can use this method only for data that is not secure.Data sent through this method is secure.
GET method is not safer SINCE parameters MAY be stored in WEB server logs or browser history.POST method is safer than GET because the parameters are not stored in web server logs or browser history.
4.

What is PDO in PHP?

Answer»

PDO STANDS for PHP Data Object. PDO is a set of PHP extensions that provide a core PDO class and database, specific drivers. The PDO extension can access any database which is written for the PDO driver. There are several PDO drivers available which are used for FreeTDS, Microsoft SQL Server, IBM DB2, SYBASE, Oracle Call Interface, Firebird/Interbase 6 and PostgreSQL databases, etc.

It gives a lightweight, vendor-neutral, data-access abstraction layer. Hence, no MATTER what database we use, the function to issue queries and fetch data will be the same. And, it FOCUSES on data access abstraction instead of database abstraction.

5.

How to create API in PHP?

Answer»

API stands for Application Programming Interface. It defines the functions and variables. Communication between the database via PHP extensions is handled by API.

Now, REST API is the web architecture that uses HTTP PROTOCOL for exchanging data between two functions that means your application or SYSTEM. Now, let us have a look at how to create REST API in PHP by considering the EXAMPLE of accessing data from a database using PHP script.

Step 1 - Create a database: To create a database run the query given below:

CREATE DATABASE phptest;

Step 2 - Create a table: After creating a database, you have to create a table with dummy data. To create a table run the query given below:

CREATE TABLE IF NOT EXISTS `transactions` ( `id` int(20) NOT NULL AUTO_INCREMENT, `order_id` int(50) NOT NULL, `amount` decimal(9,2) NOT NULL, `response_code` int(10) NOT NULL, `response_desc` varchar(50) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `order_id` (`order_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;

Step 3 - Create a Database Connection: Create a db.php FILE and paste the below-given database connection in it. Make sure to update these credentials with your database credentials.

<?php// Enter your Host, username, password, database below.$con = mysqli_connect("localhost","root","","phptest");if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); die();}?>

Step 4 - Create a REST API File: Create an api.php file and copy the following script in it.

<?phpheader("Content-Type:application/json");if (isset($_GET['order_id']) && $_GET['order_id']!="") {include('db.php');$order_id = $_GET['order_id'];$result = mysqli_query($con, "SELECT * FROM `transactions` WHERE order_id=$order_id");if(mysqli_num_rows($result)>0) { $row = mysqli_fetch_array($result); $amount = $row['amount']; $response_code = $row['response_code']; $response_desc = $row['response_desc']; response($order_id, $amount, $response_code, $response_desc); mysqli_close($con);} else { response(NULL, NULL, 200,"No Record Found");}}else{response(NULL, NULL, 400,"Request is invalid");}function response($order_id,$amount,$response_code, $response_desc){$response['order_id'] = $order_id;$response['amount'] = $amount;$response['response_code'] = $response_code; $response['response_desc'] = $response_desc;$json_response = json_encode($response);echo $json_response;}?>

The above code will accept the GET request and return output in the JSON format.

Now you can get an output as given below:

Output File

This is how you can create REST API in PHP.

6.

How to connect to a URL in PHP?

Answer»

Any URL can be connected to PHP easily by making use of the library called cURL. This comes as a default library with the standard installation of PHP.

The term cURL stands for client-side URL. cURL make use of libcurl(client-side URL Transfer Library) which supports MANY protocols like FTP, FTPS, HTTP/1, HTTP POST, HTTP PUT, HTTP proxy, HTTPS, IMAP, Kerberos etc. It allows you to connect to a URL and retrieve and display information from that page – like the HTML content of the page, HTTP headers, and their associated data, etc.

Steps for connecting with URL using PHP cURL POST are given below:

  • Initialize cURL session.
  • DEFINE your URL where you want to post the request. We can directly enter this URL into the URL section inset option parameter or we can assign it to an OBJECT.
  • Now, define the cURL options that you want to execute with the post option.
  • After setting all the FUNCTIONS then it’s time to execute our cURL.
  • After this, close the cURL and echo your object to check their response.
//Step 1 To initialize curl $ch = curl_init();//Step 2 To set url where you want to post $url = ‘http://www.localhost.com’;//Step 3 Set curl functions which are needs to you curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POSTFIELD,’postv1 = value1&postv2 = value2’);//Step 4 To execute the curl $result = curl_exec($ch);//Step 5 Close curl curl_close($ch);
7.

What are the different ways of handling the result set of MySQL in PHP?

Answer»

There are 4 WAYS of HANDLING the RESULT set of MySQL in PHP. They are:

  • mysqli_fetch_array(): Returns the current ROW of the result set as an associative ARRAY, a numeric array, or both.
  • mysqli_fetch_assoc(): Returns the current row of the result set as an associative array.
  • mysqli_fetch_object(): Returns the current row of a result set, as an object.
  • mysqli_fetch_row(): Returns result row as an enumerated array.
8.

What is Memcache and Memcached in PHP? Is it possible to share a single instance of a Memcache between several projects of PHP?

Answer»

MEMCACHED is an efficient caching daemon designed specifically for decreasing database load in dynamic web applications. Memcache offers a handy procedural and object-oriented interface to Memcached.

Memcache is a MEMORY storage space. We can run Memcache on a single or several servers. Therefore, it is possible to share a single instance of Memcache between MULTIPLE projects.

It is possible to CONFIGURE a client to speak to a separate set of INSTANCES. Therefore, it is allowed to run two different Memcache processes on the same host. Despite running on the same host, both of such Memcache processes stay independent, unless there is a partition of data.

9.

What is the use of session_start() and session_destroy() functions in PHP?

Answer»

The session_start() function is USED to start a NEW SESSION. Also, it can resume an existing session if it is stopped. In this particular case, the return will be the current session if resumed.

Syntax:

session_start();

The session_destroy() function is used to destroy all of the session variables as given below:

<?phpsession_start();session_destroy();?&GT;
10.

What are the steps to create a new database using MySQL and PHP?

Answer»

The 4 main steps used to create a new MySQL database in PHP are given below:

  • A connection ESTABLISHMENT is done to the MySQL server using the PHP script.
  • The connection is VALIDATED. If the connection is successful, then you can write a sample query to verify.
  • QUERIES that create the database are input and later stored into a STRING variable.
  • Then, the created queries will be executed one after the other.