Table of Contents
Whatever we see on our website, it is either saved within HTML or being fetched from a table. The HTML has a script file attached with it which calls backend, where SQL codes are written. These SQL codes interact with database. In this article we will learn how to send a response in PHP. There are different ways to send response via PHP APIs, we will showcase the one that can be used in all types of CRUD operations here.
// to send an array of data in respone echo json_encode(<array variable>); // to send a string Echo ‘<Your string>’; // to send error of SQL echo json_encode(mysql_error()); or echo $conn->error
How to send a response in PHP
As discussed in above use cases, we can send as below:
|String||echo ‘Operation successful’;|
|Array||$dataArray = ‘Insertion successful’;|
Getting PHP response in frontend
As you have seen above that a response can be of different type, hence in UI you will have to handle the response accordingly.
In case it is a string, you can print it directly.
In case of array you need to print the value at given position, e.g. response.
In case of object you need to print the value after parsing it with the help of keys.