PHP Introduction

In this chapter you will learn:

  1. PHP source
  2. PHP Statement
  3. PHP Opening and Closing Code Islands
  4. Example - PHP Statements

PHP source

PHP scripts are generally saved with the file extension .php.

Statement

The basic unit of PHP code is called a statement, which ends with a semicolon.

Usually one line of code contains just one statement, but we can have as many statements on one line as you want.

PHP Opening and Closing Code Islands

<?php and ?> marks the PHP code island.

The short tags version is <? and ?>.

<?="Hello, world!" ?>

Here is the equivalent, written using the standard open and closing tags:


<?php 
   print "Hello, world!";
?>

Example

The following PHP code uses print statement to output message on to the screen.


<?php/*from   j  ava2  s .c  o m*/
        // option 1
        print "Hello, ";
        print "world!";

        // option 2
        print "Hello, "; print "world!";
?>

The code above generates the following result.

echo is another command we can use to output message. echo is more useful because you can pass it several parameters, like this:


<?php
        echo "This ", "is ", "a ", "test.";
?>

The code above generates the following result.

To do the same using print, you would need to use the concatenation operation (.) to join the strings together.

Next chapter...

What you will learn in the next chapter:

  1. What are variables
  2. Syntax to define variable in PHP
  3. Example - Define PHP variables
  4. PHP Variable substitution in String
Home » PHP Tutorial » PHP Introduction
PHP Introduction
PHP Variables