PHP basics
PHP code can be declared in various places and can be used to print text as well as contain HTML tags.
This tutorial focuses on:
- Declaring a script within a webpage
- Declaring a script in an external file
- Printing text on a webpage
- Including HTML tags in a script
- Including comments in a script
Declaring a script within a webpage
A PHP script begins with <?php and ends with ?>. It can be placed anywhere within a webpage. You can have as many scripts within a webpage as you want.
NOTE: Each line of code in PHP must end with a semicolon. The semicolon distinguishes one set of instructions from another. If you do not end a line of code with a semicolon, you will get an error!
Declaring a script in an external file
An external file in which a PHP script is declared should have a .php extension. This file will be included within the PHP source code of a webpage using the include() function inside an internal PHP script as if it is actually on the same webpage with the source code. The advantage to using external scripts is that you can include the same script(s) on several pages without having to rewrite them.
In this example, an internal script calls the external script named script1.php
NOTE: External scripts cannot contain the <?php ?> block. Only internal scripts can!
Printing text on a webpage
You can print text on a webpage in PHP using the print or echo commands.
Including HTML tags in a script
HTML tags can be included in a script using the print or echo commands mentioned above. Any tags included in a script through these commands will be interpreted by the web browser as regular HTML.
Landofcode.com main page
Including comments in a script
Comments in PHP are declared so that code would be easier to understand and to navigate. Comments are not seen on a webpage, but only within the source code. Comments can be placed anywhere within PHP source code. In PHP you can have single line comments and multi-line comments.
Single line comments are declared with two / symbols and can span only a single line. Multi-line comments are declared with a starting /* and an ending */ and can span as many lines as you want.
