Hi y'all, I'm having a problem passing a variable to an include. I realize it's a scope issue (as it's technically a different page), just don't know what to do about it. origin_page.php (contents) $var=whatever; if(condition){ include 'another_php_page.php'; } ***************** another_php_page.php (contents) echo $var; // nothing here
make $var a constant and it shoudl work bit heavy handed but it should work could also try messing around with global
this is not a scope issue. When you include a file as far as things are concerned any code within the included file will act as if it were in the original file. The most likely answer is as Altari has suggested which is that you have redeclared $var or possibly never declared it in the 1st place.
No, including file doesn't change the scope. Function/class declaration does. 1.You can access variable in included function if it is global. To declare global variable declare it not inside the function. Inside function you can use $GLOBALS['var'] = 'value'; PHP: To access global var you can use global $var; echo $var; PHP: or echo $GLOBALS['var'] PHP: 2. If you need bulk variables passing, you can pass them to a functions using compact() and extract().
Thanks for all the help, but I still am not seeing results. More help? if(isset($_POST['rating_val'])){ $rating_val=$_POST['rating_val']; $mess = A rating of: " . $rating_val . " " . $trim_url; // $trim_url declared in page that this page is included in echo "This is trim_url: " . $trim_url . "<br />"; // Not showing up so not showing in message either $to = "address@domain.com"; $subject = "Rating"; $body = $mess; if (mail($to, $subject, $body)){ echo "<p>Thank you.</p>"; } else { echo "<p>There was an error.</p>"; }
I'm pretty sure, as the mail gets sent with everything but the one variable. Just in case this makes a difference, the variable is derived from stripping the original url (when the page is first loaded). Then if (condition) include other page (which is a form for mailing info). The info that is mailed is from a form and the variable that is derived from the stripped url.
Ok, figured it out (after I posted the last one). I added the var as a hidden field in the form. Then it was there in the post info. Sorry to have wasted everyone's time.