Saturday, November 5, 2016

PHP - require_once inside function requires use of global keyword

In PHP, require_once function locates a file and inserts its content into the the script. Its uses in PHP applications vary from simple loading of configuration files to supporting complex, hierarchical template systems. Typically, require_once is called at the beginning of the script execution. That way, the script is dynamically expanded to include new functionality, originating from the included source file. That scenario is common and straightforward to follow. However, there's a tiny peculiar detail in the behavior of PHP's require_once function:

If you are going to declare a variable V in file1.php and include file1.php into a function func() in file file2.php, you have to use global keyword to reference the V variable in function func() in file2.php.

Here's a simple scenario:

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...