TheGuyWhoCodes TheGuyWhoCodes - 6 months ago 7
JSON Question

Store a PHP variable once throughout a webpage.

So I have a a variable called json that stores all of the json. I want to load this once in the site, and be able to use it through any php function that I have. So let's say I have this line of code placed at the beginning of index.php:

$json = file_get_contents('http:/xxxxxxxx/bp.json');


I want to be able to use this variable from a function somewhere else in the page, WITHOUT loading it again.

Thanks!

Answer

You could store your variables in the PHP session and call it anywhere in your application.

$_SESSION["favcolor"] = "green";

http://www.w3schools.com/php/php_sessions.asp

Also best practices is to destroy and/or clear all your session variables upon exiting your application.