0

look below code:

$myvar = array();

first();

function first()
{
    global $myvar;

    $array(apple, banana, orange);
}

second();

function second()
{
    global $myvar;

    print_r($array);
}

then in output, second function doesnt show up array... =/ i dont get how function get variable array from other function...

1
  • What you want to do? $array in the first function is meaningless - the <code>$array(...);</code> actually calls the function by name which is in the $array variable - but the variable is not initialized. Commented Apr 28, 2011 at 17:40

7 Answers 7

2

Two things

$myvar is not used anywhere in your functions;

you should learn how to add element in declared array.

    function first()
    {
        global $myvar;        
        $myvar=array('apple','banana','orange');
    }
Sign up to request clarification or add additional context in comments.

3 Comments

then what if use with $myvar = explode(";", $line)?
@user: What is $line where it is come from?
i only give example... $line = "apple;banana;orange;"; because im in troublesome with php read and write file.. i see function cant read array from global variable.
1

You are not setting variable $array anywhere.

Comments

1

Dont use global variables just because you are lazy. Use return values and function parameters instead

function first () { return array('apple','banana', 'orange'); }
function second ($param) { print_r($param); }
$fruit = first();
second($fruit);

Comments

1

You're asking for the variable $myvar in the global $myvar; line, but then you are trying to use a variable called $array.

Try using the lines:

$myvar = array('apple','banana','orange');

and

print_r($myvar);

Comments

1

$array isn't in the scope of function 2.

Comments

-1

i thing the right code it look like this:

$myvar = array();

first();
second();

function first(){
    global $myvar;
    array_push($myvar, 'apple', 'banana', 'orange');
}
function second(){
    global $myvar;
    print_r($myvar);
}

array_push add new items to the array.

Comments

-4

Try this with using a class instead of two functions with one global variable:

class functions {

    $myvar = array();

    function first(){
        $this->myvar = array('apple', 'banana', 'orange');
    }

    function second(){
        print_r($this->myvar);
    }
}

$fn = new functions();

$fn->first();
$fn->second();

It is not a good idea to use global vars (as you did in your question).

10 Comments

... persisting bulletin board grammar and giving irrelevant advises instead of an explanation that's useful to OP.
Use proper capitalization. The silly globals are evil meme is not overly helpful in a shallow one-liner. And rewriting two simple functions into an object does not explain what OP stumbled upon.
There is no rule. But it's allowed to downvote answers for any reason whatsoever. And yours always look like you mistake SO for a bulletin board.
I'm willing to overlook your low quality writing style (the downvote is silly, since you are unwilling to change it anyway). But like I told you, the advise is still not useful. Also there is really no point in always inquiring about the downvotes when you're not listening anyway.
@markus I have to agree with Neal on this one. Totally changing other people posts and "putting words in their mouth" is bad practice IMO and should be avoided.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.