...

View Full Version : variable problems in functions (SOLVED)



M.W.S.
01-19-2008, 09:39 PM
Hello
I'm building a news script and I got problem:
I have code similar to this:

index.php


blablabla
<?php
require('includes/settings.php');
news();
blablablabla
?>

in settings.php I put some variables and include 2 files...
and variables aren't set inside the function, for example, i have in news() such piece of code:


echo $base;


it won't output what it should do and when I check it with isset(), it says, that var isn't set, but when I put echo $base outside the function, it works with no problem.
help! :(

vinternet
01-19-2008, 09:47 PM
you have to pass variables to the function or make them global

M.W.S.
01-19-2008, 09:54 PM
could you explain how?

Inigoesdr
01-19-2008, 10:21 PM
Each function has it's own "scope" for variables, so if they aren't global or superglobal, they have to be passed in the function call:



function news($var)
{
echo $var;
}

news($base);

I used "var" as an example to avoid confusion, you could use "base". More info: Manual (http://php.net/variables.scope)

Fou-Lu
01-19-2008, 11:31 PM
Global can be completely avoided and IMO shouldn't be used; parameter passing is really the best control you can use.
Course, the old HTTP_*_VARS did require globalization, but unless you are supporting an obsolescent version of PHP, I wouldn't worry too much about the long arrays.

M.W.S.
01-20-2008, 12:09 PM
Thanks everyone, I did it :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum