Use of json_decode to cast different data type - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: Use of json_decode to cast different data type fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Use of json_decode to cast different data type.php

  1. <?php
  2.  
  3. $data = [ '{fdass]', '', 'hi2', 'Check slashes \fasdfa \n ok', 'ññññ', '3hello', '-4', '2', '-3.2', 'false', 'true', '[]' ];
  4.  
  5. foreach($data as $i => $val){
  6.     $decode = json_decode( $val );    
  7.     if( is_null($decode)){
  8.         echo "\n$i-result: " . $val . "\t\ttype: " . gettype($val);
  9.     } else {
  10.         echo "\n$i-result: " . print_r($decode, true) . "\t\ttype: " . gettype($decode);
  11.     }
  12. }
  13.  
File Description
  • Use of json_decode to cast different data type
  • PHP Code
  • 22 Jan-2021
  • 411 Bytes
You can Share it: