$data = array( 'status' => 'success', 'message' => 'Operation completed successfully.' ); echo json_encode($data);
function echoJsonSuccess($message, $data = null) { $response = array( 'status' => 'success', 'message' => $message ); if ($data) { $response['data'] = $data; } echo json_encode($response); } echoJsonSuccess('Operation completed successfully', array('username' => 'johndoe', 'email' => 'johndoe@example.com'));This code defines the echoJsonSuccess function, which takes in a message and optional data parameter. It creates a response array with a status and message key, and if data is provided, it adds a data key with the provided data to the response array. Finally, it uses the json_encode function to convert the response array to a JSON string and echoes it out. Package/Library: This code does not rely on any specific package or library. It is a simple function that can be used in any PHP codebase.