A beginners guide to PHP

A beginners guide to PHP


An introduction to PHP which covers what is php? Opening and closing tags, variables, echo and comments

Welcome to the first tutorial on a beginners guide to PHP, in this tutorial I will cover:

What is PHP?
Opening and Closing tags (Syntax)
Variables
Echo (displaying text)
Comments

Note: Before viewing this tutorial you will need a basic knowledge of HTML.


What is PHP?
PHP stands for PHP Hypertext Preprocessor.

In PHP.net's own words: "PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML."

PHP is a scripting language which allows you to provide dynamic content for your users. PHP uses the extension .php opposed to .html, it can be created in almost any notepad or text editing software, personally I use Notepad++. Usually in a .php file, you write HTML as you usually would but insert php where necessary, here's an example: (don't worry if you don't understand it just yet)

<p class="result">
<?php
$equation = 2 + 1;
echo $equation;
?>
</p>


Which will output

3

You use PHP do do calculations and perform functions without the user seeing anything. When the user requests a page, the server completes the PHP functions and PHP converts the output to HTML and sends the page to the user. When the user views the source of the page, they see it as they would a normal HTML page.


Opening and Closing tags (syntax)

The PHP syntax is similar to many other languages, all PHP scripting should be done within the specified php tags. There are 2 types of PHP opening tag which are as follow

<?php

and

<?

Note that the first example '<?php' is the recommended method as this should work with all PHP enabled servers, the shorthand '<?' will only work if it is enabled on your server and is not recommended or good practice.
You always close the php code with a '?>'.
After each line of php code, php requires you to end with a semicolon ';', your code may not work if you do not follow this rule. For example the following statement will produce multiple errors:


<?php
$name = 'Matt'
$website = 'TutorialHelpCentre.com'
echo $name
echo $website
?>


In order for this code to work you must include semcolons at the end of each line. Here is the same code again with the semicolons included:


<?php
$name = 'Matt';
$website = 'TutorialHelpCentre.com';
echo $name;
echo $website;
?>



Variables
If you have never used variables before, I will try and briefly explain what they are, but you may need to look deeper into what they are.

In basic terms a variable is a way of storing a value for future use. You can store anything from an Array, String or even a session value.

In PHP variables are defined with a dollar sign($) the variable name is not important to what it contains but it is good practise to use something that describes what the variable contains to define a variable. Note that the variable name can only contain letters, numbers and underscores. The variable name comes directly after the dollar sign and the content of the variable is assigned using an equals sign. Here is an example of a basic use for variables.


<?php
$myName = 'Matt';
echo 'Hi my name is ';
echo $myName;
?>


This will print:

Hi my name is Matt


Another way of concatenating strings to variables is using a full stop(.) the code above could also be written as follows:

<?php
$myName = 'Matt';
echo 'Hi my name is ' . $myName;
?>


This will return the same output as above.


Echo (showing text)

Simply put, the 'echo' statement is php's way of outputting text. The echo statement will simply output any text which follows it.

<?php
echo 'Hello World!';
?>


The above code will output:

Hello World!


The code above can also be printed using a variable. The same code could be written as

<?php
$myPhrase = 'Hello World!';
echo $myPhrase;
?>


with the same output as above.

If you need to use quotes in your string you should escape them using a backslash, for example if we would like to print the line <p class="mytext">Hello World!</p> We will need to escape the quotes as follows:

<?php
echo "<p class=\"mytext\">Hello World!</p>";
?>



Comments

Comments are very useful in PHP, they help to organise your code and understand what lines of code are doing. Comments are written by you and only seen by someone looking at the PHP source file (probably only you). They have no effect on the code and are purely there to help you understand and read your code. Comments can be done in 2 ways, the first way is a double forward-slash (//). This is usually used when commenting individual lines of code. An example is shown below.

$name = 'Matt'; //store the users name


The second way of doing comments is a forward-slash and an asterisk (/*) followed by an asterisk and a forward slash. This is usually used for commenting larger sections and used frequently on copyright information within PHP files.

/*
Copyright 2009-2010
Matt Davenport - tutorialhelpcentre.com
*/


That's all for now, I hope you enjoyed this tutorial. More to come very soon!

Sources:
Php.net



Submit Comment

Comments

  1. Ayan on August 22, 2009
    thank's for all
    but i searching for somthing hard kind of tutorial..
    i think u will update those kind of examples..

    thank's in advance..
    Ayan