3

I have a question about " and ' in PHP. I have to put a complete <li> element into a PHP variable but this doesn't work, the output is completely false...

    $list = 
"
    <li class=\"<?php if($info[1] < 700) {echo \"half\";} else {echo \"full\";} ?>\">

        <div class=\"onet-display\">

        <?php if ($image = $post->get('image.src')): ?>
            <a class=\"onet-display-block\" href=\"<?= $view->url('@blog/id', ['id' => $post->id]) ?>\"><img class=\"onet-thumb\" src=\"<?= $image ?>\" alt=\"<?= $post->get('image.alt') ?>\"></a>
        <?php endif ?>

        <h1 class=\"onet-thumb-title\"><a href=\"<?= $view->url('@blog/id', ['id' => $post->id]) ?>\"><?= $post->title ?></a></h1>
        <div class=\"uk-margin\"><?= $post->excerpt ?: $post->content ?></div>

        </div>

    </li>
";

Is it because there is PHP Content in the HTML Code? How can I solve this?

Can someone help me and explain why this doesn't work?

3
  • Instead try using heredoc and nowdoc syntaxes. It would be better for such situations Commented Jan 3, 2017 at 8:50
  • 2
    you can't put <?php inside php code Commented Jan 3, 2017 at 8:52
  • You chose the wrong approach. Storing such content does not make much sense. You do not want such content inside a variable, since there you cannot do anything with it. So the question is: why did you put it there? What were you trying to do? If you told us, then we would be able to help and point out better approaches. Commented Jan 3, 2017 at 8:56

3 Answers 3

2

<?php ... <?php

Since your string contains PHP tags, I suppose you expect them to be evaluated. The opening PHP tag within another PHP tag is interpreted as a part of the PHP code. For example, the following outputs <?php echo time();:

<?php echo "<?php echo time();";

There are several ways to build a PHP string from PHP expressions.

Concatenation

You can create functions returning strings and concatenate the calls to them with other strings:

function some_function() {
  return time();
}

$html = "<li " . some_function() . ">";

or use sprintf:

$html = sprintf('<li %s>', some_function());

eval

Another way is to use eval, but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior.

Output Buffering

If you are running PHP as a template engine, you can use the output control functions, e.g.:

<?php ob_start(); ?>

<li data-time="<?= time() ?>"> ...</li>

<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;

Output

<li data-time="1483433793"> ...</li>

Here Document Syntax

If, however, the string is supposed to be assigned as is, use the here document syntax:

$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;

or

$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;
Sign up to request clarification or add additional context in comments.

Comments

0

You want to store some html into a variable.

Your source should (if not yet) start with

<?php

Then you start building the contents of $list.

Starting from your code the nearest fix is to build $list by appending strings:

<?php
$list = "<li class=";

if($info[1] < 700)
{
    $list .= "\"half\"";  // RESULT: <li class="half"
}
else
{
    $list .= "\"full\"";  // RESULT: <li class="full"
}

// ...and so on...

Now a couple things to note:

$list .= "... is a short form of $list = $list . "...

Where the . dot operator joins two strings.

Second thing you may make code easier to read by mixing single and double quotes:

Use double quotes in PHP and single quotes in the generated HTML:

<?php
$list = "<li class=";

if($info[1] < 700)
{
    $list .= "'half'";  // RESULT: <li class='half'
}
else
{
    $list .= "'full'";    // RESULT: <li class='full'
}

// ...and so on...

This way you don't need to escape every double quote

Comments

0

i think you have to work like this

$test = "PHP Text";

$list = "<strong>here ist Html</strong>" . $test . "<br />";  
echo $list;

Comments

Your Answer

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