use raw html in php - php

I want to keep my html code in a variable and then echo it once in PHP. here is the code I use for this:
my code
Note: I use the code's image here as I got formatting error upon posting this question.
But I constantly get this error:
Parse error: syntax error, unexpected end of file in E:\wamp\www\test.php on line 18
PHP version: 5.4.3
I use WAMP 2.2
Any idea on how I can fix this to work?
Thanks in advance

EOD;
Should be at the beginning of the line, as stated on php.net. Although this syntax isn't actually required in your situation - just use:
<?php
//some php code
?>
<div>
<!-- my html -->
</div>
<?php
//more php code
?>
You can open and close PHP tags as much as you want

The end tag, EOD;, must be the first thing on a new line. Otherwise, it won't work.

Your the heredoc syntax for strings the terminating string has to occur at the beginning of a line.
Excerpt From The Docs
It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.
See the Warning highlighted here

Related

What's the error in the following PHP code?

I'm testing the following code. I'm using PHP heredoc but I'm getting an error from dreamweaver. If I write it manually, it works. If I copy paste it doesn't work. Why is that?
<?php
$e=<<<EOP
whoever
EOP;
$el=<<<EOG
whatever
EOG;
?>
There is a space after your last closing identifier.
<?php
$e=<<<EOP
whoever
EOP;
$el=<<<EOG
whatever
EOG;
^ right there
?>
It needs to be removed.
As per documentation http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Warning
It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.
try to remove a space after "EOG;"
<?php
$el=<<<EOG
whatever
EOG;
?>

PHP end tag "?>" [duplicate]

This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 9 years ago.
I've had an interesting phenomenon with a PHP end tag. I had a php file that was executed by an Ajax call. In the php file was included a php library file with assorted functions. When this library was included the php response included a bunch of blank lines. When I removed the end tag from the library this stopped happening.
Can anyone explain to me what going on here ?
This is well documented. From the PHP Manual:
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.
Omitting the closing tag helps you prevent accidental whitespace or newlines from being added to the end of the file.
That's a core PHP feature: unlike other languages, you need to tag PHP code with a special tag (normally <?php) because everything else is considered literal output:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
?>
This is not PHP either
D:\tmp>php test.php
This is not PHP
This is PHP
This is not PHP either
Although the manual mentions HTML, PHP doesn't really know/care what content-type is outside its tags.
If you forget to close a PHP block when further stuff follows you normally get a syntax error:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
This is not PHP either
D:\tmp>php test.php
PHP Parse error: syntax error, unexpected 'is' (T_STRING) in D:\tmp\borrame.php on line 6
Blank lines are a sort of special case because they are valid and almost invisible in almost all languages (PHP, HTML, CSS, JavaScript...) so they often unnoticed.
Once you've removed the ?> tag, your literal blank lines have disappeared from the script output because they've become part of the PHP code (and, as such, they've started to get ignored).
Of course, blank lines are ignored by PHP but not necessarily by whatever you are generating which, as I said, does not need to be HTML: it can be a picture, a PDF document, an Excel spreadsheet. Bogus white lines can be easily avoided by not closing the last PHP block when it's the last part of the file.

PHP eats linefeed in php/plaintext mixed mode

This one is bothering me for a while. Let's say we have a simple PHP-File:
Line 0
Line 1
<?="Line 2"?>
Line 3
Processing this file will result in:
Line 0
Line 1
Line 2Line 3
Where did the line feed after ?> go? The linefeed is not beeing devoured when placing some character after the closing tag (e.g. ?>.).
Is there a way to control this behaviour? I'm not willing to place whitespaces after the closing tag, because my IDE is configured to remove whitespaces before linefeeds (and I like it that way).
Yes, indeed:
The closing tag for the block will include the immediately trailing newline if one is present.
http://php.net/manual/en/language.basic-syntax.instruction-separation.php
Meaning, if the ?> is the last thing on the line, the newline will be removed as part of the closing PHP block. You need to explicitly echo a newline or add an additional newline.
This is actually a feature (believe it or not). PHP consumes a linefeed if it directly follows a PHP close tag:
The closing tag for the block will include the immediately trailing
newline if one is present.
This was clearly put in so that a PHP file ending with a blank line would not cause a newline to occur in the output when included from another script. So it's really a "protect the ignorant" feature from the old days that we have to live with for the foreseeable future.
If you really want the newline there are other options: from simply putting in two newlines after the closing tag (the second will work!) to echoing a newline from code.
Outside of the <?php and ?> tags, the PHP interpreter operates in HTML mode and spacing inside HTML mode is less of an issue than it is for text contents.
To generate text with PHP you should use plain strings and build your output in this fashion:
$var = "Line 2";
$s = "Line 0\nLine 1\n$var\nLine3";
At least this won't give you a nasty, though documented, surprise :)

PHP - Problem with script tags within the php code

I am editing some code that I've inherited from elsewhere and there seems to be a problem with the script tags in this section of the code. If I remove them the page runs but obviously the javascript does not run.
I'm not too good with php so I'm not sure how to fix this.
$GLOBALS['TEMPLATE']['extra_head'] = <<<ENDHTML
<script src='js/ajax.js' type='text/javascript'></script>
<script src='js/blog.js' type='text/javascript'></script>
ENDHTML;
Just a guess, but if there are any characters (including spaces or tabs) on the same line before or after the closing delimiter of the HEREDOC, you will get errors.
From the manual:
Warning
It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.
If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.
What you eluded to (blank screen and broken syntax highlighting in your text editor) would be the result or symptom of a parse error if error reporting is off. It's hard to see if this is the case in your post, being outside the context of the actual file.
Check that out and see if it is applicable.
Aside: It's a good habit to turn on error_reporting(E_ALL) while in development.
Those 'tags' just allow you to enter bare HTML within PHP code without having to use quotations etc.
It's assigning anything between those 2 tags to the variable: $GLOBALS['TEMPLATE']['extra_head']

PHP omits a return

when a carriage return follows a closing php tag, php doesn't print it.
How can I change this?
Thanks a lot
That's normal behavior, and cannot be changed : the newline after a closing ?> is always ignored.
Here's the reference, in the FAQ of the PHP manual : Hey, what happened to my newlines?
(quoting, emphasis mine)
<pre>
<?php echo "This should be the first line."; ?>
<?php echo "This should show up after the new line above."; ?>
</pre>
In PHP, the ending for a block of code
is either "?>" or "?>\n" (where \n
means a newline). So in the example
above, the echoed sentences will be on
one line, because PHP omits the
newlines after the block ending. This
means that you need to insert an extra
newline after each block of PHP code
to make it print out one newline.
Why does PHP do this? Because
when formatting normal HTML, this
usually makes your life easier because
you don't want that newline, but you'd
have to create extremely long lines or
otherwise make the raw page source
unreadable to achieve that effect.
And here are a couple of interesting reads about this :
Rules pertaining to HTML or whitespace preceding or following PHP tags
PHP Stripping Newlines
The history of PHP eating newlines after the closing tag -- goes back to PHP 3 ^^
It's a default behavior of the language.
If you need the line break, you put a echo "\n" or echo "<br>" as the last line of the script.
This is intended behavior (see Escaping from HTML):
[…] when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) […]

Categories