Search code examples
phphtmlcssembedding

Embedded PHP, cannot stop?


I'm a little bit confused right now. I have this code:

<?php $this->head( ?>
    <style type="text/css">
        .error {
            background-color: #ccc;
            border: 1px solid #999;
            padding: 10px;
            width: 500px;
        }
    </style>
<?php ); ?>

I was pretty sure that you could do this. It's pretty similar to:

<?php if (true) { ?>
    Hei
<?php } ?>

In fact an error occurs:

Parse error: syntax error, unexpected ';', expecting ')' in file.php on line 1 (line 1 of the code)

How can i better fix it?


Solution

  • If you are trying to pass the html as a variable then use heredoc.

    $var = <<<HTML
      <style type="text/css">
        .error {
          background-color: #ccc;
          border: 1px solid #999;
          padding: 10px;
          width: 500px;
        }
      </style>
    HTML;
    
    $this->head($var);
    

    You cannot do this:

    $variable = ?> <p>omgwtfbbq</p> <?;
    

    Thats just isn't a valid syntax.

    Insead, you can surround it with quotes (single or double)

    $variable = "<p>omgwtfbbq</p>";