diff options
-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; |