Basics of PHP

Assuming that your php installation went smooth and you are now ready to begin, let's start with some basic understanding of PHP files.

You must understand and remember few things about creating PHP files. And these are:-

  1. Every PHP file will have an extension of .php or optionally .php3 You have an option to change these default file extensions using the web server's configuration file(s).
  2. A .php file should have all the PHP code inside <? (opening tag) and ?> (closing tag). The opening tag can be <?php as well. It will then be treated as php language code and parsed accordingly. This is useful when you want to mix PHP code with HTML.
  3. PHP can be embedded inside HTML, but in a slightly different fashion. In between the HTML tags where ever you want to make use of PHP code, you will have to use <? php code ?> and this will be parsed by PHP.
  4. All statements must end with a ;(semicolon). This is very important and this semicolon tells the PHP parser to treat this line as a single statement. Without this you will see error messages when parsing the code.

Writing your first PHP script

Why not display some output onto the screen? It is in fact much easier to accomplish with PHP. All you have to do is use PHP language construct called echo and we are done. Here's how..

Open a blank new file in your editor or notepad and write the below mentioned lines in the file. Then save the file with a .php extension. If you are using notepad as editor then be careful about the file extension. In Windows OS the file extensions are often hidden, so you must ensure that file saved has a .php extension. Ok, take a look at the code below.

<?

echo "This is my first line!"; 

?>

// Or, you can write it using other declaration tag as well, which is

<?php 

echo "This is my first line!"; 

php?>

Now save the file and upload it to the correct location in the web server and call it. The url could be http://localhost/filename.php (just an example, replace 'localhost' and filename.php with actual.)

Great! What do you see? We expect to see an output which will look like..

This is my first line!

If that is the exact output on your browser, then it is a success!!! We are on a good start. If you did not see this output, then perhaps PHP was not installed/configured properly. Note that if the PHP file is prompted to be downloaded in your browser, then PHP is not installed/configured properly as the file with .php extension is not being parsed. Please refer to your installation procedure.

Case Sensitivity in PHP

All the keywords and built-in constructs are case in-sensitive. Which means, there is no difference between echo and ECHO

Handling Statements in PHP

All statements are separated using a semicolon. It's only a compound statement such as if loop or a conditional loop which does not need a semicolon before and after the closing bracket. For example:

<?php

if (a=b) {
echo "a and b are equal"  
// Semicolon is not needed after the above statement unless there is another statement
}                     // Semicolon is not needed here

?>

Commenting the code

Code require commenting for a many reasons. The commented code is not parsed by PHP and is a good way of adding comments about statements, code and functions. All lines starting with // will be treated as a comment by PHP. One can also using # to comment the code. Example:

<?php

// Function to echo string passed as an argument
function echo_string($a) {
echo $a   // The value of variable a will be printed on screen
}

?>

Now you know that the text in first line and third line after // are treated as comments.

Further reading on PHP Basics: