View Full Version : Passing Variable And Scope

01-08-2008, 01:34 PM
I have a variable at the start of my page that gets the value from the URL, looks up the database of that value and assigns a title to it in $title. So for each page I have a separate title assigned to it.

I have a function that counts the number of people browsing my web page (that I found here (http://www.goodphptutorials.com/out/Display_how_many_visitors_online)) that has a nice little script that does this. However, I would like to add to it that value from $title so that it also adds the page they are currently viewing into the database too.

Is this possible and am I going about it the right way? I've tried various ways of doing this, but I can't get the value from $title to $counter and into that database.

01-08-2008, 02:39 PM
You will need to do this:

$title = whatever; //declared in global scope.

function foo()
global $title; //brings the globally declared variable into local scope.

//Now use $title as you wish - note that any changes you make to $title here will be reflected globally.

01-08-2008, 04:17 PM
Just managed to work that out thank you :)
Also had another problem that i.p's weren't always being stored but that was just a conflict with two people having the same I.P. Thanks for your help!

01-08-2008, 04:32 PM
global $title;

Do not use global unless completely necessary. Which in this case it isn't. Global is a bad shortcut.

It's bad design for a function to depend on external situations. A function should be completely separated.

I'm not sure I understand the question though so I don't know how else I would do it.