1

Hi I am trying to add more than one class whilst echoing some html but it Just does not seem to work. the PHP looks like the following:

echo
"<div class="."alert alert-success"." role="."alert".">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>";

the output looks like this (captured using dev tools on chrome):

<div class="alert" alert-success="" role="alert">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>

It has autofilled the ="" on the alert-success but it looks like this officially

<div class="alert" alert-success role="alert">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>

For some reason the alert-success class appears to be put in after the brackets. I have tried moving around the " but it remains in the same place. is there something simple I am missing.

1
  • 1
    read this: Strings Commented Jan 6, 2015 at 10:17

4 Answers 4

2

The real output is :

<div class=alert alert-success role=alert>
        <strong>Well done!</strong> You successfully read this important alert message.
</div>

The reason to this is simple : strings in PHP are enclosed by double quotes, and in your code you never put double quotes in the string : you use them to close the string.

To use a double quote in a PHP string enclosed by double quotes, you need to escape the character, by adding a backslash before it :

echo
"<div class=\"alert alert-success\" role=\"alert\">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>";
Sign up to request clarification or add additional context in comments.

Comments

1
echo
  "<div class='alert alert-success' role='alert'>
    <strong>Well done!</strong> You successfully read this important alert message.
 </div>";

1 Comment

As per usual i missed an extremely basic thing, thanks. Will accept it when timers off :)
1

Try with -

echo
"<div class='alert alert-success' role='alert'>
        <strong>Well done!</strong> You successfully read this important alert message.
</div>";

Comments

0

The problem is double code and single code so that is wont work. If you want to use double code us backslash.

class=\"alert alert-success\" role=\"alert\"

This is run successfully thanks.

"Well done! You successfully read this important alert message";

Comments

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.