PHP - Beginner Tutorial

Discussion in 'Web Design & Programming' started by Waffle, Feb 9, 2006.

  1. Waffle

    Waffle Alpha Geek

    Likes Received:
    Trophy Points:
    This tutorial is for anyone who has never touched PHP coding before...if you have, you'll find it kind of basic, but I will guide you through installing, and creating several basic scripts to get a feel for the language.

    It would help if you know HTML, but this is not necessary for this tutorial.

    Step One
    Firstly, you will need to obtain access to a PHP Capable server.

    Don't fear, for if you do not have access to a suitable web server, for there are plenty of easy options for creating scripts offline.

    If you don't have access, then head on over to Apache Friends and download yourself a copy of Xampp.

    This brilliant program will fully install and configure PHP, MySQL and Apache Web Server on to your computer.

    Download, install the program. Install MySQL and Apache as services.

    Once installation is complete, point your browser over to:


    and if the installation was successful, you will see the Xampp welcome screen.

    Choose your language, and you are ready to go. Navigate on the left - you can access phpMyAdmin, the internet standard for creating and managing SQL Databases, but for now, we aren't worried about that.

    Step Two
    You are ready to begin coding your first PHP script.

    Open up notepad or dreamweaver, or essentially any text application.

    Start a new document, and type in the following:


    echo "This is my first script!";

    Save the file as firstscript.php in this location:

    C:\Program Files\xampp\xampp\htdocs

    (I would suggest making a new folder for your scripts, so maybe:

    C:\Program Files\xampp\xampp\htdocs\myphp\)

    now, point your browser over to:


    or if you saved it in a folder..


    Hopefully, if all has gone well, you will see the following:

    This is my first script!
    if you don't or get an error..something went wrong in the installation of php.

    If you do, however, then, move on.

    Step Three
    PHP Code is a server side scripting language...all the content is dynamic and so everything is carried out on the server before being sent to the browser, as HTML. This means that your code is safe from people trying to steal it, and it enables you to do some awesome things with websites and applications.

    The basic syntax is:

    <?php  //START php
    //PHP Code here
    ?> //END php
    note php can be opened and closed many times within a document, it integrates very well with HTML and forms. You just must make sure the file is saved with a .php extension for the browser to parse php.
    #this, is also a comment. it won't be displayed to the user. good for annotating code.

    /* And this, is also

    a comment, stretching

    over multiple lines

    //You can also start and end PHP by using

    <? ?> //but this is informal and not XHTML compliant
    note, you terminate every instruction in php with " ; ". There are some exceptions, but as a whole, every line of code will end in ;, else there will be a parse error.

    Variables are a crucial part in any language, and PHP is no exception.

    Variables are declared like this:


    $variablename "variable value";

    //note that variable names CANNOT start with an integer..they can start with letters or an underscore.

    #also note that variable values must be contained in ""s unless it is an integer, in which case this is not necessary

    $myname "Waffles";
    $_myage 17;

    Some of the basic syntax in php are listed below. You've already looked at one of the most common, ECHO.

    <?php //start PHP

    //function name

    echo ("Something here"); //this function simply prints whatever text/variable/string was entered between the "'s and ().

    //example usage:

    echo "Hey, this is an echoed statement.";
    //prints Hey, this is an echoed statement. to the browser

    echo "$string";
    //prints whatever value the variable string had

    Last thing for the tutorial for now, IF/Else statements.

    These are vital for any application to have more than one possiblity, based on whether or not an arguement is true or false.

    the strucutre for an IF/Else is:

    if (
    something is true) {

    // do something

    } else {

    // do something else


    in english this translates to:

    If something is true/false (it can be either, or something more advanced such as if a value is present), then do something. Else, if this is not true, then execute something else.

    It's easier in practice.

    Load a new script:


    // IF/Else statement

    //lets set up some variables..

    $name "Waffles";

    $name == "Waffles") { //it is, so:

    echo "My name is Waffles!";

    } else {

    "My name is not waffles!";

    //end the statement.

    load that, and see what the result is.

    You should be displayed with the first message.

    Now go back to the script and change the name in $name, to some random crap.

    Now run the script again. As the $name is no longer equal to (==) "Waffles", the other code is executed.

    And there we have..very, very, basic PHP.
  2. Sniper

    Sniper Administrator Staff Member

    Likes Received:
    Trophy Points:
    basic is where it all starts, good tutorial dude! I was wanting to write some php tutorials, since its something I could write about without...maybe after my exams.
  3. Addis

    Addis The King

    Likes Received:
    Trophy Points:
    Good tutorial. :) Carrying on from if statements...

    Every statement should be terminated with a ;. Because the PHP interpreter program ignores whitespace characters during coding then it is important to remember to terminate every statement. e.g.:
    echo "Test..."$a $b//would be the same as
    echo "Test...";
    $a $b;
    Notice that the if (condition) does not terminate in a ;, as it compares an expression to see if its true or false. However because it often carries more than one argument you need to use the { and } around your true condition code to group them together.
  4. Anti-Trend

    Anti-Trend Nonconformist Geek

    Likes Received:
    Trophy Points:
    * Stickied *
  5. Waffle

    Waffle Alpha Geek

    Likes Received:
    Trophy Points:
    Time for an update..not so much beginners, but just random PHP Stuff.

    Functions, are either:

    Prebuilt into the code of a given language, and (usually), return values when parameters are passed into them.

    They can also be defined by a programmer, to create useful shorthand pieces of code, that can be reused quickly and easily.

    Some existing PHP Functions
    At last count, some 3000+ functions are built into PHP (the latest version anyway). They are constantly being expanded, as new versions/releases are brought out.

    A function is structured like this:

    functionname(parameters) {

    code to be executed with given parameters

    Here are some of the more common functions.

    echo() - is used to display output to the web browser.

    It is not strictly a function however, it is a language construct. It cannot return a value, but is considered a faster operation than the alternative, Print.

    Print works largely the same as Echo, except Print can return a value ( True/False).

    It is also said to be slower in execution, however the difference is 1000's of miniseconds, and so this is barely worth mentioning.

    Both will return the data entered between the parentheses to the browser (although echo does not require the ()'s, it will operate with just quotation marks).


    "Hello!"// shows "Hello!" on the screen
    print ("Hello!"); //shows "Hello!" on the screen

    Also to note, the parentheses in this example are not vital, but is considered valid coding.

    strtolower($string); - self explanatory - String to Lower (case), presumably.

    example usage:
    "Waffles AND SyRuP go Well TogetHer";

    $str strtolower($str);

    $str// Prints waffles and syrup go well togther
    note that the string ($str), was passed into the function strtolower($str), and the new value of $str, after being stripped of capital letters, is then echoed.

    I believe this is recursive coding...using the same variable again, but in like an updated form. (vague explanation admittedly)


    Now, to create your own function..

    First you need to tell PHP you are starting a function, then give it a name:


    MyFunction() { }

    is the syntax.

    You must then decide what you want your function to do.

    I have coded a handy function, that allows the coder to enter the name of a database, and a table within, and have the number of rows returned to the screen (this is useful when counting say, the number of members, or tutorials a database has.)

    Rather than type out lengthy SQL queries, several times, the function can be used multiple times, with different table/database names to return the row count.

    I will call the function "CountRows", and I need only 2 variables (it's basic).

    that would be, the database name, and the table name.

    CountRows($database,$table) {}
    we need to do something with those

    CountRows($database,$table) {

    mysql_connect("localhost","user","pass"); //connect to mysql

    mysql_select_db($database); //select the database entered into the first parameter..

    //run a query on the chosen table..

    $sql "SELECT * FROM $table";
    $result mysql_query($sql);
    //now count the rows..
    $num mysql_num_rows($result);


    mysql_close() //close the connection
    }  //and end function
    we are done.

    in order to use this, we would first include the function code on the page we are working, and then:


    "functions.php"//includes our functions page, 
    //so we can use them easily.


    echo "Number of downloads:"//we want to count our downloads.

    echo CountRows(data,downloads);

    //data is the database name, dowloads is the table name. 
    //this will return the number of rows in the table, and output it as an integer.

    and there you have, a basic, very useful, working function.

    I would probably extend this by throwing in some error handling, like what happens if the user can't connect to MYSQL or chooses an invalid table/db name. But providing you enter the correct info, you will be able to count any row, in any table, in any database.

    Hope this helps.
  6. Sn1P3R

    Sn1P3R Geek Trainee

    Likes Received:
    Trophy Points:
    ???? OK, now im confused:confused: ????

Share This Page