PHP Basic Syntax


Basic Syntax of PHP

There are four different ways of writing PHP code, namely:
  1. <? echo ("This is a PHP script \n"); ?>
  2. <?php echo("This is too\n"); ?>
  3. <script language="php"> echo ("use this if your html editor do not recognize the PHP"); </script>
  4. <% echo ("This is similar to the ASP"); %>

You can choose one of four ways. But the most frequently used is the first and second ways. Note that each end of the line must always be a semicolon (;).

Sample Program:

<? php
echo 'Hello World! <br /> ';
echo 'This <i>script </ i> is my first php program';
?>

The script will be:
Hello World!
This script is my first php program

PHP script can also be combined with HTML. PHP scripts can be placed anywhere on
HTML document. For example:
<html>
<head><title><?php echo 'PHP Learning';?></title></head>
<body>
<?php
echo 'Hello World! <br /> ';
echo 'This <i>script </ i> is my first php program';
?>
</body>
</html>

But the script is processed by the server only the PHP script only (that is flanked by a <?php and ?>). The rest will be directly sent to the client browser without being processed first.

As in other programming languages you can put comment lines in your program. In the PHP is to put these comments to the right of the sign / / if one line comment and between /* and */ if more than one line remarks.
<? echo ("PHP exercise";  / / this is an example of one line comment
/* This is a comment
more than one line */
echo ("really easy");
?>

0 comments:

Post a Comment