How to generate response data in PHP?

In PHP, response data can be generated using the following methods:

  1. Output data to the client using the echo statement.
echo "Hello World!";
  1. Use the print_r function to output an array or object to the client.
$data = array("name" => "John", "age" => 30);
print_r($data);
  1. Use the json_encode function to convert an array or object into JSON format and output it to the client.
$data = array("name" => "John", "age" => 30);
echo json_encode($data);
  1. Set response header information using the header function, then output the data to the client.
header('Content-Type: application/json');
$data = array("name" => "John", "age" => 30);
echo json_encode($data);

These methods can generate response data in different formats as needed, such as text, arrays, JSON, and more.

bannerAds