4

Sorry if this a completely nube sounding questioning. I'm new to the PHP syntax conventions, so I'm not entirely sure what I should be looking for.

The book I've got gives the following example as a conventional php block in html code.

<?php
//... some code ...
?>

I get that, but the confusing bit is that the example code I'm looking at some examples from xampp (e.g. the CD collection source code) doesn't seem to follow the same convention.

Instead, the example code reads more like this.

<? include("langsettings.php"); ?>

<?
//... some code ...
?>

Are the two forms just equivalent for all intents and purposes or did I completely miss something crucial to an intro to php here?

Also why doesn't php use closing tags (or does it and have I just not seen them)? I guess I'm thinking of javascript with the closing tags, but I guess either way, they're codebases in and of themselves so it works. It just seems like html has symmetry at the core of it's syntax, but php syntax sort breaks from that symmetry, which is odd.

Thanks for your time.

Slayer0248
  • 1,231
  • 3
  • 15
  • 26

5 Answers5

4

The only difference between these is that the second requires the setting short_open_tag to be enabled (which is off by default in new PHP version).

<?php regular open tag.

<? Short open tag (disabled by default)

Beyond this, the placement of something like <? include("langsettings.php"); ?> on its own line enclosed in its own pair of <? ?> is really a matter of style specific to the source you found it in. Different projects use very widely different conventions, and PHP books each tend to adopt their own convention.

PHP doesn't unfortunately have any real specific coding conventions such as you might find in languages like Ruby, Java, or Python, which is, in my unsolicited opionion, one of PHP's chief failings as well as one of its greatest flexibilities.

Now, as to whether or not short open tags are good practice for use in a modern PHP application is a separate issue entirely, which has been discussed at great length here.

Community
  • 1
  • 1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • And, while PHP is a serverside language that is pretty soft on beginners, it also invites to write messy, procedural code without a framework. – Justus Romijn Jun 06 '12 at 17:47
3

The two forms are equivalent, but you will find that the shortcode can give you issues. I would stick with the regular tags:

<?php

and the block closed by

?>

Edit: The closing tag is optional, but only if you want everything after the opening tag to be interpreted as PHP until the end of the page.

Anything outside those blocks are interpreted as HTML, so you have to ensure that you watch where you are opening and closing.

For example:

<body>

<h1> The Heading </h1>

<p>
   <?php
      echo "This is the Content";
   ?>
</p>

</body>

Will work, and output the php generated string into your paragraph tag.

PHP is similar to javascript in that it doesn't have 'open' and 'close' tags, but rather utilize a semicolon to declare the end of a particular php statement.

include "file1.php";
include "file2.php";

If you forget the semi colon, like so

include "file1.php"
include "file2.php";

That will generate an error.

Jesse Kernaghan
  • 4,544
  • 2
  • 18
  • 25
  • Good answer; I'd just add that the closing tag is optional. If it's omitted, all text between the opening PHP tag and the end of the page is interpreted as PHP. – octern Jun 06 '12 at 19:52
  • I neglected to mention in my original comment: I made the same omission in my answer, and was corrected by cHao (: – octern Jun 06 '12 at 20:07
1

The closing tag for a PHP block is ?>. The closing tag is not required, but it can be used if you want to interpret part of your page as PHP and other parts as literal HTML. People sometimes do this if they want to do some PHP processing at the beginning of the page, then write an ordinary static HTML page with just a few PHP variables echoed into it.

In other words, text that comes after a <?php tag and before a ?> tag is interpreted as PHP. If the closing tag is omitted, then all text between the opening php tag and the end of the page is interpreted as PHP.

One exception to this is that if you open a conditional statement inside a php block, then close the php block, ALL the following text on the page will be subject to that conditional, until you start a new php block and close the conditional statement. For example, if you run the script:

<?php
if(1==0) {
?>

<B>conditional HTML</B>

<?php
}
?>

the HTML between the two PHP blocks will not appear on the page.

Note that different PHP blocks are all part of the same script. Variables, functions, and classes defined in one block can be used by other blocks on that page, and so forth.

octern
  • 4,825
  • 21
  • 38
  • This is very important to understand! Basicly you will never have a closing php-tag. This will always ensure that NO data is send to the output buffer that is not created by PHP itself. – Daan Timmer Jun 06 '12 at 17:46
  • Something's fishy about this answer. If "anything on the page that's not between a `` will be....not interpreted as PHP", then a page with no `?>` wouldn't have anything between a ``, and the whole page should be interpreted as HTML. But not only does omitting the `?>` work, but it's *recommended* for pages that don't have literal HTML in them. – cHao Jun 06 '12 at 18:25
  • @cHao Thank you for pointing that out! I've revised my answer. What I wrote applied to the example the OP showed, but it's not the general correct rule. – octern Jun 06 '12 at 19:35
0

PHP starting tag is <?php and closing tag is ?>.

If there are short tags allowed on server you can use <? ?> syntax also.

You can read more about that on Offcial PHP Documentation

Best regards, Tom.

Community
  • 1
  • 1
user1440445
  • 101
  • 1
  • 1
  • 8
0

Issues regarding short version long open tags have already been covered.

I'll just mention one common gotcha in the question that hasn't been mentioned yet in these answers.

Compare the following:

<?php
 /*
  * Some comments here, (c) notice, etc.
  */

  header("Content-type: text/html");
  ...

vs

<?php
 /*
  * Some comments here, (c) notice, etc.
  */
?>

<?php
  header("Content-type: text/html");
  ...

The second one doesn't work.

Why?

There's a blank line of non-PHP code between the first block of PHP and the second. In a server environment that is not using output buffering, the blank line signals to PHP that the headers are all done, and anything from this point on is part of the HTML (or whatever) being sent to the browser.

Then, we try to send a header. Which produces:

Warning: Cannot modify header information - headers already sent

So ... be careful of your blank lines. A blank line INSIDE your PHP is fine. OUTSIDE your PHP, it may have nasty side-effects.

ghoti
  • 45,319
  • 8
  • 65
  • 104