简体   繁体   中英

Declaring the value of a variable and calling it in an include later on (PHP)

I'm surprised I couldn't find a similar question on this site. Maybe my search abilities are a bit rusty...

So I am naming a variable in the <head> of each page in my site, like this:

<?php $color="orange" ?>

Color could also equal blue or green or whatever. So if the color is orange, I want to display an orange inside of an include file on the site. Let's say... for the menu...

<?php include 'inc/menu.php'; ?>

Now, inside of that include I have something like this:

<?php if ($color="orange") { echo "<span id='showmyorange'></span>";} else {echo "";} ?>

I may also use something like this:

<div class="<?php echo $color>">

However, it seems to default to one option, as if defining the value of $color without looking back at the information in the header of my page, as if $color="" . How can I define the value of a variable outside of the include file in which it is called?

you need to change this line:

<?php if ($color="orange") { echo "<span id='showmyorange'></span>";} else {echo "";} ?>

to this:

<?php if ($color=="orange") { echo "<span id='showmyorange'></span>";} else {echo "";} ?>

notice the double == sign. One is for assigning the variable, two "==" for confronting the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM