In this article, you will learn about JSON format and its available PHP functions. Also, we will look at the Callback functions in PHP. So, let’s start.

What is JSON?

JSON stands for Javascript Object Notation. It is a structure to represent the data in a more readable form. JSON is used all over the world by almost all languages, therefore it plays an important role in making cross-platform requests and getting responses.

Every language is capable of reading JSON so, most of the API calls are used JSON input data and response output. There are some built-in JSON functions in PHP.

  1. json_encode()
  2. json_decode()

json_encode is used to encode or convert a value into JSON formate. Look at the example below.

<?php
$age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);

echo json_encode($age);
?>

json_decode is used to convert or decode back the JSON object into an associative array (key-value pairs) or a PHP value. Look at the following example.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

var_dump(json_decode($jsonobj));
?>

The second parameter of json_decode is a boolean. If it is set to false, it returns an object. If it is set to true, it returns an associative array. The following example makes use of the second parameter of json_decode.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

var_dump(json_decode($jsonobj, true));
?>

How to access the json_decode return values?

As we see above that json_decode returns an object by default and an associative array when the second parameter becomes true. We will show you examples to access values from each of these outputs.

Example 1 – json_decode returns object

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$obj = json_decode($jsonobj);

echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
?>

Example 2 – json_decode returns an associative array

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$arr = json_decode($jsonobj, true);

echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
?>

How to loop through the json_decode array?

As we know, json_decode returns an associative array. So, foreach loop can access both the keys and their values in json_decode. Look at the following example in which we iterate over the json_decode result.

Example

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$arr = json_decode($jsonobj, true);

echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
?>

Callback functions in PHP

A function passed as an argument to another function is called a callback function. You can define your own callback function and pass it to another function. In this case, just pass the name of the function as a string. Look at the following example of a simple callback function in PHP.

<?php
function my_callback($item) {
  return strlen($item);
}

$strings = ["apple", "orange", "banana", "coconut"];
$lengths = array_map("my_callback", $strings);
print_r($lengths);
?>

PHP 7 and 8 can now pass an anonymous function as a callback function. In the following example, you will learn how to pass an anonymous function as a callback function in PHP.

<?php
$strings = ["apple", "orange", "banana", "coconut"];
$lengths = array_map( function($item) { return strlen($item); } , $strings);
print_r($lengths);
?>

How to use user-defined function as callback function?

Just like a normal function, use argument variables in your function. Now, pass this function as a callback function.

Example

<?php
function exclaim($str) {
  return $str . "! ";
}

function ask($str) {
  return $str . "? ";
}

function printFormatted($str, $format) {
  // Calling the $format callback function
  echo $format($str);
}

// Pass "exclaim" and "ask" as callback functions to printFormatted()
printFormatted("Hello world", "exclaim");
printFormatted("Hello world", "ask");
?>

As you are now familiar with the JSON format, json_encode, json_decode, and callback functions in PHP. In the next section, we will learn about two simple, but very easy, and important concepts of PHP.

What is include keyword in PHP?

Include the keyword in PHP allows using an external file having PHP script, in the current file. It saves us from writing the code of the file again and again everywhere we want to use its code.

Without using the include keyword, the code can be messy or redundant. To solve the problem of redundancy, PHP provides two keywords, include and require. Logically, they do the same thing but are different from each other. Let’s go through them one by one.

What is the difference between include and require in PHP?

includerequire
Include keyword allows to use the piece of code in some external file, into your existing code. Just add the file at the top of the PHP script using the include keyword and you can access the code of that file in your current code. Here, the important thing is, if the file you specify in the include keyword does not exist or contains any error in it. It will produce E_WARNING but does not stop your script.On the other hand, require keyword is also used to add external code files into the current PHP script. But on any kind of failure in the file, or in the case of a missing file, require the function to produce E_COMPILE_ERROR and does not allow the script to execute. It will stop the process.
PHP Include vs Require

Which is better to use out of include or require?

The choice between include and require depends on the use case or scenario.

If you are working on a platform in which high security is the main concern. In this case, you must use the required keyword. As the required keyword does not allow the script to execute for any error in the attached file or in case of file not found.

If you want your script not to stop due to the included file, you can use the include function in this case. It will acknowledge you about the problem using the E_WARNING error.

Syntax of include and require

include 'filename';

or

require 'filename';

Example of Include keyword in PHP

<h1>Welcome to my home page!</h1>
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>

Example of require keyword in PHP

<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>

You can learn official documentation about json_encode, json_decode, and PHP callback functions.