{"id":42027,"date":"2018-11-26T03:01:09","date_gmt":"2018-11-25T17:01:09","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=42027"},"modified":"2018-11-25T19:33:53","modified_gmt":"2018-11-25T09:33:53","slug":"php-json-decode-and-encode-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-json-decode-and-encode-primer-tutorial\/","title":{"rendered":"PHP JSON Decode and Encode Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"PHP JSON Decode and Encode Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.jpg\" title=\"PHP JSON Decode and Encode Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">PHP JSON Decode and Encode Primer Tutorial<\/p><\/div>\n<p>PHP and Javascript can form a potent mix of serverside and clientside interplay for web applications.  As a conduit between the two, datawise, and an industry standard, is the <a target=_blank title='JSON information from w3schools' href='\/\/www.w3schools.com\/js\/js_json_intro.asp'>JSON<\/a> protocol &#8230;<\/p>\n<blockquote cite='\/\/www.w3schools.com\/js\/js_json_intro.asp'><p>\nJSON: JavaScript Object Notation.<\/p>\n<p>JSON is a syntax for storing and exchanging data.<\/p>\n<p>JSON is text, written with JavaScript object notation.<\/p>\n<p>When exchanging data between a browser and a server, the data can only be text.<\/p>\n<p>JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.<\/p>\n<p>We can also convert any JSON received from the server into JavaScript objects.<\/p>\n<p>This way we can work with the data as JavaScript objects, with no complicated parsing and translations.\n<\/p><\/blockquote>\n<p>Looking at this JSON conduit from the PHP serverside, we have two really useful PHP functions, namely &#8230;<\/p>\n<ul>\n<li><a target=_blank title='PHP json_decode information, thanks' href='\/\/php.net\/manual\/en\/function.json-decode.php'>json_decode<\/a> \u2014 Decodes a JSON string<\/li>\n<li><a target=_blank title='PHP json_encode information, thanks' href='\/\/php.net\/manual\/en\/function.json-encode.php'>json_encode<\/a> \u2014 Returns the JSON representation of a value<\/li>\n<\/ul>\n<p> &#8230; that would be good to learn for any programmer interested in data, in all its forms, and possibilities.  And particularly for those programmers who love working with <a target=_blank title='Objects information from w3schools' href='https:\/\/www.w3schools.com\/js\/js_objects.asp'>objects<\/a> and <a target=_blank title='JSON syntax' href='https:\/\/www.w3schools.com\/js\/js_json_syntax.asp'>object notation<\/a>.<\/p>\n<p>What we&#8217;ve done today, in an introductory sense, is to write a PHP web application allowing the user to define up to 5 pieces of data, be that a scalar one, or an array one or a structure one, that collects JSON syntax into a JSON string.  That approach of doing this is new to us, and goes like this, in broad brush terms &#8230;<\/p>\n<ul>\n<li>we start with an HTML table element with columns enough for up to 5 delimited columns of data (in those forms talked about above) &#8230; and it is the case that &#8230;<\/li>\n<li>the web application is responsible for maintaining a valid JSON string, made up of several HTML td cell (innerHTML) parts (within a single HTML tr row &#8230; hint, hint) &#8230; so that being the case &#8230;<\/li>\n<li>what is posted back to the same PHP code via an HTML form method=POST action=[here&#8217;s looking at you, kid] becomes, simply, the one (relevant) HTML tr (row) element&#8217;s <a target=_blank title='HTML and Javascript innerText property information from w3schools' href='https:\/\/www.w3schools.com\/jsref\/prop_node_innertext.asp'>innerText<\/a> property value &#8230; cute, huh?!<\/li>\n<\/ul>\n<p> &#8230; so, in &#8220;response mode&#8221; the web application first calls &#8230;<\/p>\n<ul>\n<li><a target=_blank title='PHP json_decode information, thanks' href='\/\/php.net\/manual\/en\/function.json-decode.php'>json_decode<\/a> \u2014 to decode a JSON string (displayed via <a target=_blank title='PHP var_dump information' href='http:\/\/php.net\/manual\/en\/function.var-dump.php'>var_dump<\/a> PHP function), then &#8230; for the heck of it &#8230; calls &#8230;<\/li>\n<li><a target=_blank title='PHP json_encode information, thanks' href='\/\/php.net\/manual\/en\/function.json-encode.php'>json_encode<\/a> \u2014 to sanity check the JSON representation of that value (<font size=1>as much as anything because the dollar sign in front of a PHP variable name can cause some unusual behaviour, on occasions<\/font>)<\/li>\n<\/ul>\n<p>Please feel free to try out the PHP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php_GETME\" title=\"json_code.php\">json_code.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/json_code.php\" title=\"Click picture\">live run<\/a> way for you to try out some PHP and Javascript interplay via JSON data.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d42027' onclick='var dv=document.getElementById(\"d42027\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/json\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d42027' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PHP and Javascript can form a potent mix of serverside and clientside interplay for web applications. As a conduit between the two, datawise, and an industry standard, is the JSON protocol &#8230; JSON: JavaScript Object Notation. JSON is a syntax &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-json-decode-and-encode-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[1580,1607,2749,2469,652,664,666,2748,849,2750,932,997,1075,1238,1319],"class_list":["post-42027","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-cell","tag-decode","tag-encode","tag-innertext","tag-javascript","tag-json","tag-json_decode","tag-json_encode","tag-object","tag-object-notation","tag-php","tag-programming","tag-row","tag-table","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42027"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=42027"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42027\/revisions"}],"predecessor-version":[{"id":42034,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42027\/revisions\/42034"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=42027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=42027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=42027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}