Can you variable from part of page be used on another?
I have a variable declared between a<?php ?>
tag. Lower down on the page, i have another <?php ?>
where id like to use that variable. But when i echo, the variable, it displays off
, when the value of the variable is something totally different.
(Ultimately, id like the have the variable in an external include file)
(I posted a similar question here: If statement from external config file not working )
****UPDATE**** Include file (accountsconfig.php):
<?php
$account = 'on';
?>
On my main script, I have a series of if else (if $account == true, do this, else this), but its always going to the else no matter what the value of $account is (As if it doesnt even get the value of $ac开发者_JAVA百科count). Heres an example of one of my ifelse:
<?php
include('accountsconfig.php');
if ($account == 'on') { ?>
<select name="account_number" >
<option value="one">Account One</option>
</select>
<?php } else { ?>
<h1> <?php echo "$account" ?></h1>
<?php } ?>
This file and the accountsconfig.php are located in the same directory.
A possible reason could be, that there is a =
sign missing.
Maybe you misspelled the if ($account == 'off')
accidentally with a if ($account = 'off')
somewhere. That would explain the off
value.
Variables remain consistent throughout pages, and it is also possible to declare them in one file and then use them in another. In your case it is likely something else is happening to the variable such as having it's value altered, before you echo it onto the page.
精彩评论