Home Tutorials IT Jobs Source Codes Certifications Discussion Forum
  PHP Tutorials
PHP Introduction
PHP Installation
Syntax
Variables
Echo
Strings
Operators
Comments
Require
Include
If...Else
Switch
Forms
Functions
Arrays
While Loop
For Loop
For Each
Do While
Post & Get
File Create
File Open
File Close
File Write / Read
File Delete
File Append
File Truncate
File upload
PHP str replace
PHP substr_replace
PHP Capitalization
PHP Explode
PHP Implode
   IT Jobs
Software Jobs
Networking Jobs
   Model Question Papers
BE Computer Science
MCA
BCA
Others
 
   

PHP Syntax


PHP statements end with a semicolon.

PHP supports comments the same as C and C++ using the methods shown below:

<?php
// This is a comment
echo "This is a test of PHP!";
?> 

 

<?php
/* Line 1 of this comment
Line 2 of this comment */
echo "This is a test of PHP!";
?>

PHP's basic syntax is familiar.


    <?php
    echo "Hello, World!";
     ?>

produces

    Hello, World!

Variables are marked with a preceding $. You could write "Hello, World!" like this:

    <?php
    $message = "Hello, World!";
    echo $message;
    ?> 


String concatenation is done with . (a period); other arithmetic operators are what you would expect:


   <?php
    $greeting = "Hello ";
    $num = 3 + 2;
    $num++;
    echo "$greeting $num people!";
    ?>


produces

    Hello 6 people!



PHP has the full complement of operators, and they work just as you'd expect them to--especially if you come from a background of C or C++. A good rule of thumb for PHP: "When in doubt, try it; it will probably work."

Just as in Perl, a string surrounded with double quotes causes variables inside it to be interpolated, but a string surrounded with single quotes does not. So,


    <?php
    $name = 'Susannah';
    $greeting_1 = "Hello, $name!";
    $greeting_2 = 'Hello, $name!';
    echo "$greeting_1\n";
    echo "$greeting_2\n";
    ?> 



produces


   Hello, Susannah!
   Hello, $name!



Note that the \n in the string turns into a new line, just as in Perl or in C. This only works in double-quoted strings, however.

 

<< Previous | Next >>
Home  |  About us  | Privacy  |  Disclaimer  |  Contact us |  Advertise with us | Our Link Partners
All Rights Reserved 2009, CodeTeller.com