Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disappearing variables

Status
Not open for further replies.

Ghodmode

Programmer
Feb 17, 2004
177
NZ
This appears to be a scope problem, but I don't understand how this could happen.

The following is the first few lines of one of my functions. I put the JavaScript block in when I noticed that some variables I was expecting weren't set. The alert box, which should show "Product Review" is empty when the page loads. Can anyone see why this is happening?

Code:
function showPage( $review ) { ?>
<?php
    $pageTitle = "Product Review";
    include( '../includes/setup.php' );
    include( 'header.php' );
?>

<script type="text/javascript">
    alert( "<?= $pageTitle ?>" );
</script>

I have a feeling that I'm just cross-eyed from staring at code for too long and I'll see an obvious mistake I'm making if I look at it again later.

Thank you.

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
I think I mostly found the problem. There were two variables which had mysteriously lost their values. It turns out that it was for two different reasons. Only one of them was visible in my code snippet.

The $pageTitle variable was referenced in my header.php as a global variable. Setting it at the top of the function like that, then including header.php basically ignored the local version and used a global one, which didn't exist. So, it lost it's value. Setting it's value at the top of the file, instead of the top of the function, fixed the problem.

I still don't understand the cause of the other variable problem, though. It has to do with that $review variable that I'm passing as an argument. Inside of the function it doesn't have a value even though I can check it first (with print_r()) and pass it properly to the showPage function. It's an array and that's probably where my misunderstanding lies. I worked around this problem by taking out the parameter and referring to $review as a global variable within the function.

Why can't I read the $review array when it's passed as a parameter to the function?

Thank you,

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top