When writing PHP, you need to inform the PHP engine that you want it to execute your commands. You can designate your code as PHP with special tags that mark the beginning and end of PHP code blocks.
<?php . . PHP statements go here . ?> |
The PHP source code inside each set of <?php ?> tags is processed by the PHP interpreter.
The filetype of a PHP file must be .php.
A PHP file normally contains HTML tags, just like an HTML file, and some PHP statements, which end with a semicolon. The semicolon is important because it tells PHP when the current line is ended, and PHP will complain if you don't use it.
The first script is to use the phpinfo() function to display information about the version of PHP you are using.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php phpinfo(); ?> </p> </body> </html> |
There are two basic statements to output text with PHP: echo and print. Echo can ttake more than one argument, but doesn't return any value. Print takes one argument.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php echo "Hello World<br />"; print "Hello World"; ?> </p> </body> </html> |
Using echo with two arguments.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php // Taking 2 arguments echo "Welcome to", "PHP class"; ?> </p> </body> </html> |
Using print with two arguments.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php // Taking 2 arguments print "Welcome to", "PHP class"; ?> </p> </body> </html> |
Ending the statement without the semicolon.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php // Ending without the semicolon echo "Hello World<br />" ?> </p> </body> </html> |
Ending the statement without the semicolon, but this time there are 2 statements.
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php // Ending without the semicolon echo "Hello World.<br />" echo "Welcome to PHP class.<br />" ?> </p> </body> </html> |