0

Ok, not sure how to Google this so am asking kindly probably a very dumb question, but why isn't this code working? lol The Variables display correctly outside of the $menu variable but when i try and output the contents of $menu it shows literally "$online" text.

$menu = '
<div id="service_info">
    <span id="service_availability">Availability</span>
    <li class="availability_$Online">Online</li>
    <li class="availability_$InPerson">In Person</li>
    <li class="availabilit_no">Over the Phone</li>
</div>
';

4 Answers 4

2

Well,i often use

$menu = <<<EOD
<div id="service_info">
    <span id="service_availability">Availability</span>
    <li class="availability_$Online">Online</li>
    <li class="availability_$InPerson">In Person</li>
    <li class="availabilit_no">Over the Phone</li>
</div>
EOD;

This is just look more clean.

Sign up to request clarification or add additional context in comments.

Comments

2

Use double quotes. Variables aren't recognized in single quotes.

$menu = "
<div id=\"service_info\">
    <span id=\"service_availability\">Availability</span>
    <li class=\"availability_$Online\">Online</li>
    <li class=\"availability_$InPerson\">In Person</li>
    <li class=\"availabilit_no\">Over the Phone</li>
</div>
";

You can use single quotes to display variables only if you break out of the string:

$menu = '
<div id="service_info">
    <span id="service_availability">Availability</span>
    <li class="availability_'.$Online.'">Online</li>
    <li class="availability_'.$InPerson.'">In Person</li>
    <li class="availabilit_no">Over the Phone</li>
</div>
';

2 Comments

Gives me the following error ( ! ) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\ttc\services\malware\virus-removal.html on line 10
Accepting is for wussies, real men just send the rays of their delight to the poster. )
2

There's a special string syntax just for these cases: HEREDOC

$menu = <<<MENU
<div id="service_info">
    <span id="service_availability">Availability</span>
    <li class="availability_$Online">Online</li>
    <li class="availability_$InPerson">In Person</li>
    <li class="availabilit_no">Over the Phone</li>
</div>
MENU;

By the way, I really recommend studying all the page linked. )

Comments

0

I just want to make one suggestion as you seem to already find the solution. If it is not quite necessary to store html tags in php variable, please include php variable in html.

**<div id="service_info"> <span id="service_availability">Availability</span> <li class="availability_<?php echo $Online; ?>">Online</li> <li class="availability_<?php echo $InPerson;?>">In Person</li> <li class="availabilit_no">Over the Phone</li> </div>**

6 Comments

Yes, that's yet another way to do it. It's even better with the short form: <?= $Online ?>, clean and distinctive. )
But I don't suggest to use the short tag.
Because of what, considering this form was given the official green light by PHP core developers?
I didn't understand what you mean?
<?= is always enabled in PHP 5.4
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.