• Knowledge Database

    Knowledge Database

    Didn't find what you we looking for?
    Why not ask one of our online staff or drop us a message!

    Webhosting | Internet Access | Security | 

    Tutorials > Advanced Web Hosting, PHP > Introduction to PHP - the basics of PHP coding



    Introduction to PHP - the basics of PHP coding

    PHP - Pretext Hyper Processor development language started in 1995. It was originally offered as a free toolset for very basic functions that we now take for granted on the internet. Back in 1995 the internet was newborn and such tools did not yet exist. PHP is an HTML-embedded scripting language

    Advantages over HTML

    PHP is a dynamic language allowing changes to pages as input data changes. This allows the page to automatically update to provide the most relevant data. HTML is static and cannot change without manual intervention by the developer. Files with .php extensions are "executable" by webservers that can run PHP code. The Linux RSAWeb Webhosting servers run PHP with MySQL as a backend database to store information. The syntax of PHP is similar to other common languages C and Perl. If you have previous programming experience in either C/Perl/ASP you should be able to pick it up quite easily.

    PHP Basic Commands

    To display some information in PHP, you can do:

    <?php
    echo "Testing displaying this info using PHP";
    ?>
    Variables are using to hold information and are simple to assign. Variable names are prefixed by a $ sign. An array can be created by using square brackets after the variable name. Comments for single lines are // or for multiple lines /* COMMENT THIS */

    <?php
    $test = 'Testing PHP variables';
    echo $test; // this will display the variable '$test'

    /* to comment on multiple
    lines, use the forward slash and the star, but remember to close it again
    otherwise your code will not work!
    */

    // creating arrays can be just as easy
    $test1[name] = 'RSAWeb';
    $test1[website] = 'www.rsaweb.co.za';
    $test1[service] = 'web hosting';
    ?>

    Forms

    Forms are used throughout PHP and are one of the building blocks on your website to make it more dynamic and interactive. Example of a form, it is in HTML, and then POSTs to the contact.php page which processes it:

    <form action='contact.php' method='POST'>
    <input type='text' name='companyname' value=''>
    <input type='submit' name='submit' value='submit'>
    </form>
    Then the contact.php form looks like this:

    <?php
    echo "Company Name: " . $_POST[companyname];
    ?>
    Once a form is submitted, you can access the submitted variables using $_POST[variablename], it is not generally wise to use $_GET as it tends to be a bit insecure if not used correctly.

    Mathematical & Arithmetic Operations

    Adding, subtracting etc are all easily achievable using PHP. Look below for examples.

    <?php
    // example 1
    $num1 = 5;
    $num2 = 6;
    $num3 = $num1 + $num2;
    // $num3 will equal 11

    Loops, Whiles, Fors etc

    These controls allow you to loop through arrays, run repeat calculations and a mirad of other options.

    <?
    // FOR example will echo out the $x value on a new line each time
    for ($x=0;$x<10;$x++)
    {
    echo "<BR>Number: " . $x;
    }

    // WHILE example will each out the $x value until 10 is reached
    while ($x < 10)
    {
    echo "<BR>Number: " . $x;
    }

    // IF / ELSE / ELSEIF example
    if ($x == 5)
    {
    // if $x is 5 do this setup
    echo "IF";
    } elseif ($x == 10)
    {
    // if $x is 10 do this setup
    } else {
    // if $x is anything else, do this setup
    }
    ?>

    Database Integration

    Database integration is what makes PHP extremely powerful. PHP can interface with many databases, but our preferred web hosting database for small websites to large corporate hosted applications is MySQL. Have a look at our MySQL tutorial for more information. PHP is an extremely power programming language, some of the basics have been outlined above. For a full function reference, please visit www.php.net

    Similar Information from this category