I want to create an array which is in my function.php code definded so I do not have to transfer the array through my hole code. But it won't work...
This is an example of my function.php:
<?php
define("titleArray", array());
function foo(){
echo $this->titleArray;
}
function boo(){
array_push($this->titleArray, "Hello");
}
But it doesn't work... How can I fix that so that every function has access to the array?
Greetings
I suggest that you store your array globally using the $GLOBALS, and instead of using echo to print an array, use the print_r method instead.
<?php
$GLOBALS["titleArray"] = array('world');
function foo(){
print_r($GLOBALS["titleArray"]);
}
function boo(){
array_push($GLOBALS["titleArray"], "Hello");
print_r($GLOBALS["titleArray"]);
}
foo();
boo();
just remove $this
<?php
define("titleArray", array());
function foo(){
echo titleArray;
}
function boo(){
array_push(titleArray, "Hello");
}
note that array values are allowed after php 7.0 version
Generally we use define to make constant value which never updates, use class instead
<?php
class abc {
$titleArray = [];
function foo(){
echo $this->titleArray;
}
function boo(){
array_push($this->titleArray, "Hello");
}
}
The define() function defines a constant. Constants are much like variables, except for the following differences: A constant's value cannot be changed after it is set.
There are two ways to do it:
use global in every function
declare $GLOBALS[] once outside the function
$titleArray = [];
// $GLOBALS['titleArray'] = [];
function foo(){
global $titleArray;
print_r($titleArray);
// print_r($GLOBALS['titleArray']);
}
function boo(){
global $titleArray;
array_push($this->titleArray, "Hello");
// array_push($GLOBALS['titleArray'], "Hello");
}
Related
I try to find the solution since many hours, but I can't solve it (I'm not a programmer ;)).
On a function, I have set a dynamic array which I want to use in another function.
To do this, I thought to use $GLOBALS[] array
I have no problem to call the variable out of the function one, but when I try to use it in function 2, it doesn't works.
Here is my code :
function one($name,$a,$b,$c)
{
// $GLOBALS[${$name}] = array($a,$b,$c);
global $$name;
$$name = array($a,$b,$c);
}
function two($name)
{
$name="D1";
echo ${$name}[1];
}
one("D1",10,20,30);
one("D2",100,200,300);
two("D1"); // doesn't works
$name="D1";
echo ${$name}[1]; // works, gives 20
$name="D2";
echo ${$name}[1]; // works, gives 200
It doesn't works and I do not understand why.
Thanks for your help.
Etienne
how about doing something like this:
function one() {
$dynamicArray = generateDynamicArray();
return $dynamicArray;
}
function two() {
$one = one(); // if it's in a class use: $this->one();
foreach($one in $key=>$value) {
// your code for each item in the array we got form function one()
}
}
instead of defining it globally.
<?php
function one($name,$a,$b,$c)
{
global $$name;
$$name = array($a,$b,$c);
}
function two($name)
{
global $$name;
echo ${$name}[0];
}
one("D1",10,20,30);
two("D1");
The scope of your function is different to the global scope.
Why is $a not printing?
And what is the alternate of this, and I dont want to use return.
function abc () {
$a = 'abc';
global $a;
}
abc();
echo $a;
The reason why it's not echoing is because of two things:
1) You need to declare global "before" the variable you wish to define as being global.
and
2) You also need to call the function.
Rewrite:
<?php
function abc()
{
global $a;
$a = 'abc';
}
abc();
echo $a;
For more information on variable scopes, visit the PHP.net website:
http://www.php.net/manual/en/language.variables.scope.php
You can get your variable as:
echo $GLOBALS['a'];
see http://php.net/manual/en/language.variables.scope.php
You can use define():
function abc() {
define("A", "abc");
}
abc();
echo A;
Make sure you call the function. I added that just above echo.
First you must create and assign a variable. And then in you function describe that is a global var you want to use.
$a = 'zxc';
function abc() {
global $a;
$a = 'abc';
}
abc();
echo $a;
This is not really good idea to use golbal such way. I don't really understand why I so much want to use a global var...
But my opinion is better for you to use a pointer to variable.
function abc(&$var){
$var = 'abc';
}
$a = 'zxc';
abc(&$a);
echo $a;
Or even would be better to create an object and then access variable with-in this object
I have this in index.php
require('include\inc\config.inc');
When I created a function which require some variable from config.inc
When I run, it says the variable is missing
I need to redeclare the config.inc else the function will never work.
Is there a way to work around?
Now I have this
function test(){
require('include\inc\config.inc');
echo $tmp;
}
instead of
require('include\inc\config.inc');
function test(){
echo $tmp;
}
You should read about the variable scope. The $tmp variable is global and is not visible inside any function, unless declared with global
require('include\inc\config.inc');
function test(){
global $tmp;
echo $tmp;
}
Your variable is out of scope in the test() function as previously mentioned and using globals is not considered good practice. You need to pass the variable $tmp into the function test() like this:-
function test($tmp)
{
echo $tmp;
}
Then call the function like this:-
require_once 'include/inc/configure.inc'
test($tmp);
I'm not fluent in PHP, but can't you write:
require('include\inc\config.inc');
function test() use ($tmp) {
echo $tmp;
}
I know I can use $GLOBALS['var'] inside a function to refer to variables in the global scope.
Is there anyway I can use some kind of a declaration inside the function so I will be able to use $var without having to use $GLOBAL['var'] every time?
Joel
Although it's not recommended, but if you do want to, here's what you can do:
If you only want to GET the values from the vars (and not SET the values), just use extract:
extract($GLOBALS);
This will extract and create all the variables in the current scope.
How about using static class?
such as
class example
{
public static $global;
public static function set($arr)
{
foreach ($arr as $key=>$val)
{
self::$global[$key] = $val;
}
}
}
function example_function()
{
var_dump( example::$global );
}
example::set( array('a'=>1, 'b'=>2) );
example_function();
You can use the global keyword, So you can use type $var instead of $GLOBALS['var'] inside a function.
function myFunc() {
global $var;
$var = 3;
}
$var = 1;
myFunc();
echo $var;
Output: 3
I have a function that includes another file like so
// some function
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
}
// some_file.php
<?php echo $someData; ?>
How would I get this to work where the include file can use the variables from the calling function? I will be using some output buffering.
As long as $someData is defined in SomeFunction(), some_file.php will have access to $someData.
If you need access to variables outside of SomeFunction(), pass them as arguments to SomeFunction().
That's already available :)
See include()
The best would be to not do use globals at all, but pass the variable as parameter:
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
some_foo($someData);
}
Otherwise you risk transforming your code base in spaghetty code, at least on the long term.
Seems kinda unorganized to include files in functions...what about...
function SomeFunction()
{
$someData = 'SomeData';
return $someData;
}
$data = SomeFunction();
<?php include('file.php') ?> // file.php can now use $data
You don't have to do anything. Usage of include() (and it's siblings) is analogous to copy-pasting the code from the included file into the including file at the spot where include() is called.
Simple example
test.php
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
include 'test2.php';
}
test();
test2.php
<?php
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
Again, this is analogous to the combined
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
}
test();