PHP Introduction

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// w w  w.  ja v  a 2  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.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition