Do I need a trailing semicolon here? - php

Example:
<?php $formElement->display()?>
Is this fine, or should I provide a ; ?
Well I guess that the PHP interpreter is clever enough to see that the line is finished and the expression done because of the ?> at the end. Right?

It is not required, but you should put it, as a good practice.
That way, the day you need to add another instruction after this one, it'll work fine.
And here is the manual's page that answers your question : Instruction separation (quoting, emphasis mine) :
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement.
The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
The closing tag for the block will include the immediately trailing newline if one is present.

No, the closing ?> will automatically close the line.
From the PHP Docs:
The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

As you say, the PHP interpreter will cope as-is.
However, I'd say that adding the semicolon is probably slightly better practice, but that's just a personal coding preference.

Simple answer: yes. It's okay to only have one statement without a semicolon inside PHP tags.

Related

Which "trailing newline" the PHP manual is talking about? It's just confusing the manual readers and PHP learners. Please clear the confusion

I'm reading the PHP manual. I come across a sentence on the manual page(URL : Instruction separation). There is the first paragraph on this page saying :
As in C or Perl, PHP requires instructions to be terminated with a
semicolon at the end of each statement. The closing tag of a block of
PHP code automatically implies a semicolon; you do not need to have a
semicolon terminating the last line of a PHP block. The closing tag
for the block will include the immediately trailing newline if one is
present.
I understood everything written in the above paragraph except the last highlighted sentence i.e.
The closing tag for the block will include the immediately trailing
newline if one is present.
What does this sentence mean? What does this sentence imply?
This sentence has created confusion and doubts in my mind.
What exactly mean by "Trailing Newline" here?
Is the meaning of "Trailing Newline", "Line feed", HTML escape sequence character "\n", <br> same in this context? Or something else?
What exactly does the "trailing newline" mean here? The newline present just before the closing PHP tag (?>) or the newline present just after the closing PHP tag (?>)
?
Someone please answer all of my questions in simple, lucid and easy to understand language. It would be far better if you explain with some working code example demonstrating this concept.
Note : I've referred the already asked question on stackoverflow about this but its answer is not satisfactory, it's incomplete and vague. So, please don't mark my question as Duplicate of it. Thanks.
Basically, it says that if you have ?> in your code somewhere and then output on the next line, there won't be a newline/line feed/\n in the output there.
foo
<?php /* whatever */ ?>
bar
Will output
foo
bar
So even though there are three lines in the code, there are only two lines in the output.
Note that if you add a space after ?> on that second line, there will be a line with just a space between foo and bar. That is what is meant with the "immediately" part of the docs.
To answer your specific questions:
What exactly mean by "Trailing Newline" here?
It means the starting of a new line directly after the ?> closing tag.
Is the meaning of "Trailing Newline", "Line feed", HTML escape sequence character "\n", <br> same in this context? Or something else?
The "trailing newline" is the newline/line feed/\n that "trails" (comes directly after) the ?> closing tag. So newline, line feed and \n mean basically the same thing. It does not mean <br>.
What exactly does the "trailing newline" mean here? The newline present just before the closing PHP tag (?>) or the newline present just after the closing PHP tag (?>) ?
The newline present just after the closing tag ?>. This can be seen in my example, which has no newline before the ?>.
The closing tag for the block will include the immediately trailing newline if one is present.
I understood everything written in the above paragraph except the last highlighted sentence i.e.
What is meant by this is: if a closing tag is followed directly by a newline, the newline is considered to actually be a part of that closing tag.
So my example above is parsed as:
foo -> output "foo"
\n -> output a newline
<?php -> start processing PHP
/* whatever */
?>\n -> stop processing PHP
bar -> output "bar"

PHP is removing a newline typed after an echo statement

Consider test.php
OK
here
<?php echo "now"; ?>
what
if you were to run this file, you could expect it to output following:
OK
here
now
what
But it returns
OK
here
nowwhat
What is causing this? Can it be prevented?
P.S.
If you add any character after the line where php code is, even a space, then the newline is retiained.
OK
here
<?php echo "now"; ?>[space]
what
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.
From php.net
So the "eating" of a newline is expected behavior. The work around as you found is to put a space, or use the above answer and add a \n.
Please add a break tag inside echo
OK
here
<?php echo "now <br>"; ?>
what
And your output will be like this:
OK
here
now
what

Why is the semicolon optional in the last statement in php?

I was surprised when I ran the following code in my editor:
<?php
echo "hello";
echo "world"
?>
As it can be easily seen, a semicolon (;) is missing from the code, however it still works!
How this works and why ; is {0,1} here?
Because the close tag implies a semicolon. You can read more about this in the manual under Instruction separation.
And a quote from there:
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.
An example to prove this:
1. script with missing semicolon at the end, but with closing tag:
<?php
echo "1";
echo "2"
//^ semicolon missing
?>
output:
12
2. script with missing semicolon at the end, but without closing tag:
<?php
echo "1";
echo "2"
//^ semicolon missing (closing tag missing)
output:
Parse error: syntax error, unexpected end of file, expecting ',' or ';' in
Because the semicolon tells the parser that you've reached the end of that instruction. It lets it know that the next piece of text is a new instruction. However the closing tag tells it that we're at the end of all instructions, you don't need to parse anything else. Because we're not parsing anything else we don't need the end of instruction semicolon, it's implied.
That is because the semicolon is not a symbol to terminate a statement.
It looks like that because it occurs almost always at the end of a statement.
Note the almost always... could be a hint.
Trying to get rid of the asymmetry, we can say it is always between statements!
That leads directly to the real meaning of the semicolon: it does not terminate statements - it separates statements.
Obviously, after the last statement, there is nothing to separate.
(Most languages allow a semicolon at the end of a block anyway, to prevent the related trivial errors. It can be done by discarding the semicolon, or, more explicit, by inserting a command that does nothing after the semicolon. )

Split code by semicolon

I would like to parse a source code. I read it line by line, but one line can contain more than one command.
I wonder if it is possible to split line by semicolons, but only by those, which aren't in a '...' or "..." blocks.
Answering your question: to split a line by a semi-colon not inside double or single quoation marks, you can use the following regex:
(?:'[^']*'|"[^"]*")(*SKIP)(*FAIL)|;
It will find ; exactly outside "..." and '...'.
See demo.
However, please consider using appropriate tools for the task you are taking up.

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

Categories