Home »
PHP »
PHP find output programs
PHP find output programs (JSON) | set 1
Find the output of PHP programs | JSON | Set 1: Enhance the knowledge of PHP JSON by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 27, 2021
Question 1:
<?php
$student = array(
"Rahul" => 101,
"Rohit" => 102,
"Virat" => 103
);
$JSON_STR = encode_json($student);
echo $JSON_STR;
?>
Output:
PHP Fatal error: Uncaught Error: Call to undefined function encode_json()
in /home/main.php:7
Stack trace:
#0 {main}
thrown in /home/main.php on line 7
Explanation:
The above program will generate compilation error, because encode_json() is not a built function in PHP. The json_encode() function is used to encode an array into JSON format.
Question 2:
<?php
$student = array(
"Rahul" => 101,
"Rohit" => 102,
"Virat" => 103
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
var_dump($STR);
?>
Output:
object(stdClass)#1 (3) {
["Rahul"]=>
int(101)
["Rohit"]=>
int(102)
["Virat"]=>
int(103)
}
Explanation:
In the above program, we created an array $student that contain student information. Then we encode array into JSON format and assigned to $JSON_STR and then we decode $JSON_STR using json_decode() function and assigned to $STR. Then we call var_dump() function and pass $STR to it then the above output will print on the webpage.
Question 3:
<?php
$student = array(
"Rahul" => "ABC",
"Rohit" => "PQR",
"Virat" => "XYZ"
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
echo $STR->ABC . '<br>';
echo $STR->PQR . '<br>';
echo $STR->XYZ . '<br>';
?>
Output:
PHP Notice: Undefined property: stdClass::$ABC in /home/main.php on line 11
PHP Notice: Undefined property: stdClass::$PQR in /home/main.php on line 12
PHP Notice: Undefined property: stdClass::$XYZ in /home/main.php on line 13
Explanation:
In the above program, we created an associative array that contains keys ("Rahul", "Rohit", "Virat") and values ("ABC","PQR","XYZ"). Then we encode string $student and then we decode json_decode() into $STR.
Here, we access values directly using operator '->', we can access array values using specified keys.
echo $STR->Rahul.'<br>';
echo $STR->Rohit.'<br>';
echo $STR->Virat.'<br>';
In the above, echo statements we accessed values of associative array.
Question 4:
<?php
$student = array(
"Rahul" => "ABC",
"Rohit" => "PQR",
"Virat" => "XYZ"
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
echo $STR['Rahul'] . '<br>';
echo $STR['Rohit'] . '<br>';
echo $STR['Virat'] . '<br>';
?>
Output:
PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array
in /home/main.php:11
Stack trace:
#0 {main}
thrown in /home/main.php on line 11
Explanation:
In the above program, we created an associative array that contains keys ("Rahul", "Rohit", "Virat") and values ("ABC","PQR","XYZ"). Then we encode string $student and then we decode json_decode() into $STR.
The above program will generate syntax error, because $STR is not an array. it is an object returned by json_decode() function.
The correct way is given below,
echo $STR->Rahul.'<br>';
echo $STR->Rohit.'<br>';
echo $STR->Virat.'<br>';
Question 5:
<?php
$student = array(
"Rahul" => "ABC",
"Rohit" => "PQR",
"Virat" => "XYZ"
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
foreach ($STR as $key => $value)
echo $key . " => " . $value . "<br>";
?>
Output:
Rahul => ABC
Rohit => PQR
Virat => XYZ
Explanation:
In the above program, we created an associative array that contains keys ("Rahul", "Rohit", "Virat") and values ("ABC","PQR","XYZ"). Then we encode string $student and then we decode json_decode() into $STR.
Then we accessed keys and values using foreach loop and print them on the webpage.