Forum Moderators: coopster

Message Too Old, No Replies

php isset in an include

         

madmatt69

3:22 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Heya,

I've got file that I use as a template which has various variables, with block of html in it.

example: $footer, $header, etc.

The actual webpage calls this include file and then references the blocks of code appropriate for it.

I have one block of code for some ads that only appear on certain pages.

So in my include file, I added a function to check if the variable $adverts is set, and if so, display the $adverts html.

I can't seem to get it working though.

Here's my function:


function check_ads() {
if (isset($adverts)) {
echo $adverts;
}
}

and then in my page.php file I've got

<? $adverts = 'this is my ad'; ?>

Any ideas where I'm going wrong? I'm sure it must be something simple.

PHP_Chimp

3:27 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




function check_ads() {
if (isset($adverts)) {
echo $adverts;
}
}

You have a problem with variable scope.
Within your function $adverts is not set...and can never be set, so you will never get the echo.
If you had:

function check_ads($adverts) {
if (isset($adverts)) {
echo $adverts;
}
}

Then the $adverts that you are setting within the general scope will be passed into the function and so you should get the echo.

Have a look at the second example on [uk.php.net...]

<edit>
The alternative would be to use global


function check_ads() {
global $adverts;
if (isset($adverts)) {
echo $adverts;
}
}

[edited by: PHP_Chimp at 3:29 pm (utc) on Sep. 5, 2008]