PHP | Syntax, and Tags
PHP introduces a list of Tags to code PHP. PHP tags allow the PHP parsing engine to get in and get out from the PHP block. A PHP script can be placed anywhere in the document wrapped with PHP tags. A PHP file normally contains HTML tags and some PHP scripts. The default file extension for PHP files is ".php". A PHP script starts with <?php and ends with ?>:
Canonical PHP tags <?php ... ?>
Basic and most universally PHP syntax
<?php // PHP script goes here ?>
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> // Canonical PHP tag <?php echo "Hello World!"; ?> </body> </html>
Short-open (SGML-style) tags <? ... ?>
To use the Short-open tag you need to enable a PHP configuration option. You have to enable short_open_tag by setting its value to "On" in your php.ini file.
Look for these lines:
186 ; This directive determines whether or not PHP will recognize code between 187 ; <? and ?> tags as PHP source which should be processed as such. It is 188 ; generally recommended that <?php and ?> should be used and that this feature 189 ; should be disabled, as enabling it may result in issues when generating XML 190 ; documents, however this remains supported for backward compatibility reasons. 191 ; Note that this directive does not control the <?= shorthand tag, which can be 192 ; used regardless of this directive. 193 ; Default Value: On 194 ; Development Value: Off 195 ; Production Value: Off 196 ; http://php.net/short-open-tag 197 short_open_tag=Off
<? // PHP script goes here ?>
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> // short_open_tag <? echo "Hello World!"; ?> </body> </html>
Shorthand tag <?= ... ?>
You can also call it a short echo tag. you only use it for the final output of variables and avoid any function-calls or ternary-logic that aren't directly related to the presentation of the data. Although <?= short_open_tag causes conflicts with XML, <?= does not. So there is no issue to use the short echo tag and The tag <?= is always available regardless of the short_open_tag in the setting.
<?= variable name ?>
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> // shorthand tag or short echo tag // Direct output <?= "Hello World!" ?> // Using a variable without echo <?= $var ?> </body> </html>