ExpertRefresh

Basics / Basic syntax  

1) Is the following code valid? <?php echo $a = 1; // ?>




2) Short open tags have the major drawback of conflicting with:





3) To disable short tags ini php.ini witch directive are used?








Php tags Short open tag (<? OR <?=) are enabled by default in php.ini (short_open_tag = on) <?php if (ini_get('short_open_tag') == 1): ?> <? echo "Short open tag ENABLED"; ?> <?php endif; ?> Short tags <? ... ?> have the major drawback of conflicting with XML headers With <?php is working correctly (View Source: <?xml version="1.0" ?>) <?php // --- Look Here -- // echo <<<EOF <?xml version="1.0" ?> EOF; ?> With <? is displaying EOF; <? // --- Look Here -- // echo <<<EOF <?xml version="1.0" ?> // --- Look Here -- // EOF; ?>

Newline Characters

It is good practice to ommit the closing php tag because newlines are used as separators between the header of HTTP server response and the actual data. <?php // test.php $a = 1; ?> [newline] [newline] <?php // index.php error_reporting(E_ALL); include('test.php'); header("HTTP/1.0 404 Not Found"); // Warning: Cannot modify header information - headers already sent by // Without [newline] will be ok // So it's good practice to omit closing tag Alternativly, you can use output_buffering flag in php.ini ; php.ini ; Output buffering allows you to send header lines (including cookies) even ; after you send body content, at the price of slowing PHP's output layer a bit. output_buffering = 4096

Comments

Single line comments can be ended using newline characters (r,n or rn) or by using PHP closing tag ?> Later can cause unintended output. // don't show this ?> or this // will output "or this" Example of comments: // Single line comment # Single line comment /** Multi-line comment */ /** API documentation example * * @param string $bar */ function foo($bar) {}

Code blocks

<?php // block code A { echo 1; } // block code B { echo 2; }

Language constructs

Are elements that are built-into the language. <?php $a = echo 4; // Parse error: syntax error, unexpected T_ECHO // echo does not have a return value $a = print(4); // output 4


References