1. PHP intro
  2. PHP basics
  3. PHP variables
  4. PHP functions
  5. PHP conditions
  6. PHP loops
  7. PHP arrays
  8. PHP classes & objects
  9. PHP strings
  10. PHP forms
  11. PHP entities
  12. PHP files
  13. PHP include files
  14. PHP date & time
  15. PHP cookies
  16. PHP databases
  17. PHP sessions
  18. PHP summary

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

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.

<?php script contents go here ?>
<html> <head> <title>PHP script</title> </head> <body> <?php print "PHP is cool!"; ?> </body> </html>
PHP is cool!

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.

<html> <head> <title>External scripts</title> </head> <body> <?php include("script1.php"); ?> </body> </html>

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.

<html> <head> <title>Print and echo commands</title> </head> <body> <?php print "Here is some text. "; echo "Here is some more text."; ?> </body> </html>
Here is some text. Here is some more text.

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.

<html> <head> <title>HTML tags in scripts</title> </head> <body> <?php print "<b><i>This text will be bold and italic</i></b>"; echo "<br /><a href=''> main page</a>"; ?> </body> </html>
This text will be bold and italic 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.

<html> <head> <title>PHP comments</title> </head> <body> <?php //this is a single line comment /* here is a multi line comment it will span multiple lines this is the third line of the multi line comment */ echo "Here is some text"; ?> </body> </html>
Here is some text
© Copyright 2013-2014
Terms of use | Privacy policy | Copyright information