1

Here's what I have:

In file containging functions:

global $numTrax;

then I call a function within html page which is simply html for the player, but, at that point i want to put in how many tracks it plays so:

audioPlayer(5);

and the function is

function audioPlayer($numTrax)
{
    echo ' ... all html ...';

    // if i echo $numtrax here it shows 5
    // because function i used was audioPlayer(5)
    // so, i'm reassigning it using $numTrax = $numTrax
    // then next function: audioPlaylist($user_id,$username,$numTrax); has $numTrax
    // but the problem is it's not showing it there

    $numTrax = $numTrax;
    return $numTrax;
}

then I have another function further down the page which creates the track list and is called as follows:

audioPlaylist($user_id,$username,$numTrax);

The problem is that $numTrax is not being carried through

QUESTION Can I make the variable $numTrax travel through the functions?

5
  • 1
    Just declaring global once somewhere does not mean that variable is available in all functions! Are you passing the variable around somehow? Or are you just relying on your one global statement to make it pass magically? Commented May 20, 2013 at 12:07
  • 1
    1. Don't use global. Ever. It's a crutch, and a dangerous one at that. 2. Can you show how you attempt to invoke the function? Does $numTrax exist at that point? Commented May 20, 2013 at 12:08
  • Eh? Please read the question, I thought it was clear, if not please say how not so - oh, and I don't believe in magic. Commented May 20, 2013 at 12:08
  • So you do just have one global $numTrax outside a function and that's it? You are not passing the variable into the function when calling it? That's not very clear. Commented May 20, 2013 at 12:09
  • 1
    The problem is, you show us one function definition - audioPlayer - but then show us a separate function's invocation - audioPlayList. We can't tell what's going on without seeing that function's definition, or how you've used $numTrax to that point. Commented May 20, 2013 at 12:13

6 Answers 6

2

Like I said above, never use global. It is exactly the wrong way to handle variables.

I'm guessing you want something like:

$numTrax = audioPlayer(5);

audioPlayList($user_id, $username, $numTrax);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Yup, totally get all that - was trying to do it in one line is all. So somehow carrying the 5 from audioPlayer(5) to audioPlaylist()
0

Don't use same variable in your function

Try,

function audioPlayer($nT)
{
    echo ' ... all html ...';
    $numTrax = $nT;
    return $numTrax;
}

Comments

0

you need to define the variable in function like

global $numTrax;

to use the global variable. Otherwise, it will create a new variable with local scope.

Comments

0

If I don't guess wrong, I think you are using wrongly the keyword global.

The assign $numTrax = $numTrax; in

function audioPlayer($numTrax)
{
    $numTrax = $numTrax;
    return $numTrax;
}

is actually assigning the value of the local variable $numTrax to itself.

You have to define the global var just as:

$numTrax = null ;

and in your function you you must avoid shadowing the global one renaming the parameter, and declare the global variable (so no new local variable is created), like for example:

function audioPlayer($paramNumTrax)
{
    global $numTrax; // declare it exist a global var somewhere called $numTrax

    $numTrax = $paramNumTrax; // Asign the local value to the global variable
    return $paramNumTrax;
}

More info: http://php.net/manual/en/language.variables.scope.php

(in some cases could happen that: "Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.". Don't know if this is your case)

Hope this helps.

Comments

0

Change your function to this:

function audioPlayer($newNumTrax)
{
    global $numTrax;

    echo ' ... all html ...';

    $numTrax = $newNumTrax;
}

To access or reassign a global variable within a function you need to use the global keyword to bring it into scope. The function's argument should have a different name from the global variable so as not to get them confused. Then, just assign the value of the argument variable to the global variable.

Going by how you've called it, this function doesn't need to return anything. It just sets the value of the global variable.

For the audioPlaylist call, there's no reason to pass $numTrax as an argument. It's a global variable, so all functions can access it. Define the function like this:

function audioPlaylist($user_id, $username)
{
    global $numTrax;     #now you can use the global variable in this function

    #rest of function here
}

... and call it like this:

audioPlaylist($user_id, $username);

Please note that using global probably isn't a very good strategy for designing your program. I recommend passing variables to functions rather than relying on globals. If your functions get too unwieldy, break them into smaller functions.

Comments

0

Variables aren't global by default in PHP. You have to declare them as such in every function where you want to use them, or set $GLOBALS['numTrax'] instead.

With that said, though, it's generally considered a really bad idea to rely on global variables, for several reasons. Most involve "action at a distance", or relying on implementation details that are now ten times harder to change rather than coding to an interface. (The general prohibition is a bit overly broad, but globals aren't needed in 95%* of the cases where they're used.)

*(Totally made up number, cause i don't have stats on it with me. But know that it's too high. :P)

Comments

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.