Jump to content

Question about missing ?>


pwired
 Share

Recommended Posts

Hi

On many places, like inside home.php, I see a starting php tag <?php

but I don't see it's corresponding php end tag ?>

Here's an example of a missing php end tag ?>

inside the home.php when installing processwire

<?php
/**
 * Home template
 *
 */
include("./head.inc");
echo $page->body;
include("./foot.inc");
 

Can anyone explain a php newbie why it seems to be ok to leave out

the end tag ?> and still everything seems to work ?

Link to comment
Share on other sites

Note:

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.

From: http://php.net/manual/en/language.basic-syntax.instruction-separation.php

  • Like 5
Link to comment
Share on other sites

The link that Luis came up with (thanks for that) to that explanation part of php explains to me also

the following I found inside head.inc

3 lines with a terminating semicolon at the end of each statement:

<title><?php echo $page->get("headline|title"); ?></title>
<meta name="description" content="<?php echo $page->summary; ?>" />
<meta name="generator" content="ProcssWire <?php echo $config->version; ?>" />

3 lines without a terminating semicolon at the end of each statement:

<link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>styles/ie.css" />
<script type="text/javascript" src="<?php echo $config->urls->templates?>scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<?php echo $config->urls->templates?>scripts/main.js"></script>

The link above to php.net explains:

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.

Question for me remains why use a terminating semicolon at the end of each statement in the first 3 lines of code

and not using it in the second 3 lines of code. Is it for personal or technical reasons ? Has it anything to do with that the first 3 lines of code is about title and meta name, and the second 3 lines of code about js and css ????


 

Link to comment
Share on other sites

Question for me remains why use a terminating semicolon at the end of each statement in the first 3 lines of code and not using it in the second 3 lines of code. Is it for personal or technical reasons ? Has it anything to do with that the first 3 lines of code is about title and meta name, and the second 3 lines of code about js and css ????

There's absolutely no technical reason behind these -- if I had to guess, I'd say that latter case just looked slightly better without the semicolon.. or it could have been simply an oversight, who knows.

Personally I try to use semicolon at the end of each statement, one-liner or not, just to keep things consistent :)

  • Like 2
Link to comment
Share on other sites

I'm with teppo - I always use the semicolon, and he's right on it being optional (though I would suggest it should be standardised on the default template for those not as familiar with PHP as well as for consistency).

But then again I always stick my curly brackets at the end of a line as well rather than on their own like all the cool kids, so I'm a heathen :P

  • Like 3
Link to comment
Share on other sites

The lesson to take here is that you shouldn't be looking at php code without learning at least the basics. IMO the best way to start is by reading a good introdutory book on php, and only then start looking at code from others.

  • Like 1
Link to comment
Share on other sites

Although I think I was writing PHP without knowing this myself for about 5 years diogo, but yes - I would also have learned this if I'd started reading PHP books at an earlier stage :)

Link to comment
Share on other sites

For what it's worth, I always put semicolons after even single lines, not so much in the case @pwired cites above, but in more general cases. Say I have this 

<?php $foo = "bar"; ?>

and then I realise I need to add another line

<?php 
$foo = "bar"; 
$baz = "qux";
?>

I don't need to remember to add a semicolon to the first line when editing to add the second line, because it's already there.

  • Like 2
Link to comment
Share on other sites

Although I think I was writing PHP without knowing this myself for about 5 years diogo

My point exactly. You wouldn't want php newbies to be looking at your first years code for sure ;)

edit: just to clarify. I'm not saying that you shouldn't look at code from others, and in this particular place, you can rest assured that Ryan's code will have very few inconsistencies or errors. But when not knowing the basics, you can be confused even by small tricks used by more experienced developers, like purposely assigning a value to a variable inside a condition if($summary = $page->summary) //do something if $summary exists

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...