What's the error in the following PHP code? - php

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;
?>

Related

HOW TO PRINT "THAT'S IT!" IN PHP? WITH DOUBLE AND SINGLE QUOTATIONS? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to print "that's it!" Exactly as same with double and single quotations in php? Can anyone have solution?
You just have to escape the single quote with a back slash
echo '"that\'s it!"';
I'll post something different because I like them and it think it's often missed or unknown by less experienced programmers.
HereDoc/NowDoc
//HereDoc works like " for variables
echo <<<TXT
"that's it!"
TXT;
The TXT tag can be anything that follows the same rules as variable names (\w+, and must start with Alpha), and the ending one must be on it's own line with "ABSOULTLY" nothing else besides ; (which can be omitted in arrays), nothing else not even whitespaces.
//NowDoc works like ' for variables
echo <<<'TXT'
"that's it!"
TXT;
It's often overlooked, as it's not documented in any really visible place, it's the third example on the string type page on PHP.net. Then after a bunch of scrolling you'll find the Nowdoc part, the only real difference is how they treat variables, as I mentioned.
http://php.net/language.types.string#language.types.string.syntax.heredoc
A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
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 macOS. The closing delimiter 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.
This is also common in other languages besides PHP (with minor variations), and it's the cleanest way (for larger amounts of text, think HTML, JavaScript and a mix of those and PHP).
$selector = 'a.foobar';
echo <<<HTML
<script type="text/javascript">
;( function( $, window, document, undefined ) {
"use strict";
$(document).ready(function(){
$('tr.foobar').css('display', 'none');//singe quotes are fine
$("div.foobar").css("display", "none");//double quotes are too
$({$selector}).css('display', "none");//we can even mix them up if we want.
//we can use PHP variable like {$selector}, even these
//comments become comments in the JS.
//if that wasn't enough, most IDE's treat them like HTML
//so they are not greyed out, but nicely colored!
});
} ) ( jQuery, window, document );
</script>
HTML;
And this would work just fine, even the {$selector} would be replaced by PHP, the {} are optional except for method calls (unless they changed that). I put them in by habit because it colors them better in my IDE. Which is excatly how PHP treats variables in "normal" double quoted strings. (variable interpolation) except here we can use both types of quotes any way we want to...
If you do ever put one in an array it will only work this way (without the ;):
$a = [
<<<TXT
sometext
TXT
, "something else",
1,
2,
'etc..'
];
Other languages that use them (linked to the PHP section)
https://en.wikipedia.org/wiki/Here_document#PHP
In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.
The important thing is it does not use the quotes to define the string, so you are free to use them however you want, with no escaping.
One last thing I happened to notice from the PHP documentation that i never really read before.
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 macOS
Maybe someone else knows, but I am not sure how important this bit really is. I program on a Windows Desktop \r\n, and then use the same exact files on a Linux server \n and Have Never 1 time had an issue with what that says. I do use editors though like Eclipse PDT, so it may default to the \n even on windows. But I have never had an issue on either one....
Enjoy!!
Probably one of the ways that works without escaping is as follows:
echo('"'."that'".'s it!"');

use raw html in 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

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