PHP
PHP introduction
PHP basics
PHP variables
PHP functions
PHP conditions
PHP loops
PHP arrays
PHP OOP
PHP strings
PHP forms
PHP entitites
PHP files
PHP include files
PHP date and time
PHP cookies
PHP databases
PHP sessions
PHP summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
VBScript
AJAX

Server scripting

ASP

Making money online

Make money online

PHP basics

PHP code can be declared in various places and can be used to print text as well as contain HTML tags.

This lesson 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.

Syntax:

<?php
script contents go here
?>

Example:

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

Output of this example:

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 in PHP 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.

Example:

<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.

Example:

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

Output:

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.

Example:

<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='http://www.landofcode.com'>
Landofcode.com main page
</a>";
?>
</body>
</html>

Output:

This text will be bold and italic
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

Single line comments in PHP are declared with two / symbols.

Example:

<html>
<head>
<title>Single line comments</title>
</head>
<body>
<?php
//this is a single line comment
//this is another single line comment
?>
</body>
</html>

NOTE: Single line comments can span only a single line.

Multi line comments

Multi line comments in PHP are declared with a starting /* and an ending */

Example:

<html>
<head>
<title>Multi line comments</title>
</head>
<body>
<?php
/*
here is a multi line comment
it will span multiple lines
this is the third line of the multi line comment
*/
?>
</body>
</html>

NOTE: Multi line comments can span as many lines as you want.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments