InterviewSolution
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");?>ConclusionPHP 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:
|
|||||||||||||||||
| 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 FileThis 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:
|
|
| 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:
|
|
| 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();?> |
|
| 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:
|
|