PHP Syntax and Tags

38 Views

PHP Syntax and Tags

PHP | Syntax and Tags

PHP introduce list of Tags to code php. PHP tags allow 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 script. 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  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 you 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

syntax

 <?
	// 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 short echo tag. you only use it for 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 short echo tag and The tag <?= is always available regardless of the short_open_tag ini setting.

syntex

<?= 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>

 

Tags:-
PHP