diff options
author | Kamil Kaminski <kylek389@gmail.com> | 2014-03-17 19:12:28 -0500 |
---|---|---|
committer | Kamil Kaminski <kylek389@gmail.com> | 2014-03-17 19:12:28 -0500 |
commit | 7bf745c1f1701a85126ce3ff9e60cb078bbcd57a (patch) | |
tree | a2da687fe69275bb2f2efbe19fbe4f264cb473a1 | |
parent | 1bad4fc00814e2c03ecadaa7faf93c6372f5bd30 (diff) | |
download | phpsandbox-7bf745c1f1701a85126ce3ff9e60cb078bbcd57a.tar.gz phpsandbox-7bf745c1f1701a85126ce3ff9e60cb078bbcd57a.tar.bz2 phpsandbox-7bf745c1f1701a85126ce3ff9e60cb078bbcd57a.zip |
php globals: try few exmaples
-rw-r--r-- | global_from-func-to-global.php | 10 | ||||
-rw-r--r-- | global_vars.php | 4 | ||||
-rw-r--r-- | globals.php | 16 |
3 files changed, 30 insertions, 0 deletions
diff --git a/global_from-func-to-global.php b/global_from-func-to-global.php new file mode 100644 index 0000000..94d3f81 --- /dev/null +++ b/global_from-func-to-global.php @@ -0,0 +1,10 @@ +<?php + +function foo() { + global $foo; + $foo = 'bar'; +} + +foo(); +// prints 'bar' +echo $foo; diff --git a/global_vars.php b/global_vars.php new file mode 100644 index 0000000..bf86e98 --- /dev/null +++ b/global_vars.php @@ -0,0 +1,4 @@ +<?php + +$fruit = 'apple'; +$color = 'green'; diff --git a/globals.php b/globals.php new file mode 100644 index 0000000..ade8d6d --- /dev/null +++ b/globals.php @@ -0,0 +1,16 @@ +<?php +/* + * + */ + +function foo() { + // when a file is included, the code it contains inherits the variable scope of the line on which the include occurs + include 'global_vars.php'; + echo $color; +} + +// prints green +foo(); + +// undefined +echo $color; |