Contact Our Development Team
Free Code Tutorials & Open Source Code
Basic Syntax
Tutorials > PHP > Basic Syntax
PHP Files
Most PHP pages will contain a mixture of both HTML and PHP code. A block of PHP code must start with <?php and end with ?>. These blocks can be contained anywhere within a document. PHP files are usually saved with the extension .php, although some web servers may support other extensions such as .php3. Using .htm or .html will not work, as the PHP code will not be executed by the webserver, but will instead be displayed to the user.
Comments
There are two types of comments in PHP, using the same syntax as C++ and Java. One-line comments start with //, and continue to the end of the line. Multi-line comments start with /* and end with */.
Variables
Variables in PHP are prefixed by a dollar sign ($). Variable names must start with a letter or an underscore (_), and may contain letters, numbers, and underscores. Names are case sensitive. Note that there is a special variable, $this, which cannot be assigned. The use of the $this variable is explained in the later tutorial on classes.
 
Some examples:
<?php
    $name = "Bob";                  //String.
    $name2 = 'Joe';                 //Strings may be enclosed in either single- or double-quotes.
    $age = 42;                      //Integer.
    $ageString = "42";              //String.
    $ageString = "$age";            //String - same as above.
    
    $statement = "$name is $age.";  //String - "Bob is 42."
    $singlequotes = '$name is $age' //String - "$name is $age" - variables within single quotes will not be interpreted.
?>
This code block introduces a few key points. In PHP, variables do not have to be declared. In fact, they do not even have to be initialised with a value, although to use a variable without initialising it is very bad practice and should be avoided. Furthermore, the programmer does not usually specify a type - this is determined by PHP at runtime depending on the context in which the variable is used.
 
The penultimate line above demonstrates the ease with which strings can be constructed from other values - Any variables specified inside a double-quoted string will be interpreted. In the above example, the $statement variable would now contain the value Bob is 42. Note that this does not apply to strings enclosed in single quotes.
Outputting Text
There are two basic statments for outputting text in PHP: echo and print. Both take a string as Continuing from the code above, if we type:
<?php
    echo($age);
    print("<br>");  //The HTML 'line break' tag.
    echo $age;
?>
We get the following output:
42
42
Note that the echo and print statements are language constructs, not functions, so the parentheses are not required - the first and third lines above are functionally identical. Also noteworthy is the fact that the $age variable was originally an integer. The echo statement (and, indeed, the print statement) takes a string as its argument. Here, PHP has automatically converted our integer variable to a string.
 
In the next tutorial, we look at strings in more detail, and introduce some basic functions for handling them.
Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
     
Copyright ©2009, Wired IDS Ltd. | Licensed under Creative Commons Attribution Share-Alike | Load time: 0.3042 seconds