Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP block syntax conventions

Tags:

html

php

tags

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.

like image 744
Slayer0248 Avatar asked Feb 20 '23 05:02

Slayer0248


1 Answers

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.

like image 126
Michael Berkowski Avatar answered Feb 22 '23 22:02

Michael Berkowski