how to put php in html

Asked

Viewed 154 times

1

is giving error in my code and I do not know why..

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <?php
        echo <<<_END
        isso e um teste
        _END;
    ?>
</body>
</html>

this error is appearing

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:\Program Files (x86)\Ampps\www\php\apenasteste.php on line 17

i am using an apache server AMPPS to write my pages.

2 answers

5

I am assuming that you are just learning to use HEREDOC (and have NOWDOC as well).

The fact is that the end of heredoc cannot be indented. the _END; has to be on the left bank.

Of itself PHP manual:

Warning: It is very important to note that the line containing the closing identifier should not contain any other character except the semicolon (;). This means that the identifier should not be indented and should not have any spaces or tabs before or after the semicolon. It is also important to realize that the first character before the lock identifier should be a new line as defined by the local operating system. This is, n on UNIX systems, including MAC OS X. The lock identifier should also be followed by a new line.


Adjusted code:

In addition to adjusting the end of the HEREDOC, I took the spaces from "open PHP" and "close PHP" so that they do not leave in the final HTML.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php
        echo <<<_END
        isso e um teste
_END;

?>
</body>
</html>


Learn more about HEREDOC and NOWDOC in these links:

What use <<< EOH in PHP?

It is possible to use Heredoc with special characters in an array?

2

Hello, if you just want to imprint something you don’t need to use heredoc, may conventionally use single quotes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <?php
        echo "isso e um teste";
    ?>
</body>
</html>

You can also use an abbreviated form using tags <?= and ?>.

<body>
    <?= "isso e um teste" ?>
</body>

  • 5

    Open things in the answer that would be interesting to explain: 1) No need to use heredoc, but why change? 2) What is this simple quote convention you quoted? 3) Why did you use double quotes if you quoted the single quote convention?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.