+ Reply to Thread
Results 1 to 2 of 2

Thread: Introduction to PHP

  1. #1
    VDC Team Member
    Join Date
    Feb 2004
    Location
    Kentucky
    Posts
    50

    Introduction to PHP

    Introduction to PHP
    Written by jd-inflames
    Updated February 10, 2009


    Introduction
    If you are reading this tutorial, I expect that all of you at least know what PHP is. For those who don't, PHP is a server-side scripting language, that when the client (you) decides to load up a page, the script will tell to the server what to provide the client, and then what the clients browser needs to do to comply with the scripts purpose.

    In this tutorial, I will not be explaining how to install PHP, or how to set up your server, but will be focusing on getting you started in understanding some of the syntax, and being able to write simple statements in PHP. When I am finished, I will have explained several commands, such as opening statements, comments, requires and includes, variables, echo and print, if / else / elseif, and basic math functions.

    Getting Started
    In PHP, you can either create a file that is completely comprised of PHP, or you can embed it in your basic HTML files. Regardless, what you need to remember to do when you save your files, is to save them with the file extension .php

    With that being said, when you upload them to your server, be sure that you have PHP installed. If you don't have your own hosting, you can also test your work by either installing IIS/Apache and then PHP on your computer, or by using some all in one software packages that configure your server for you such as AppServ or Xampp.

    Opening Statements
    To begin anything in PHP, it starts like this:

    PHP Code:
    <?php

    ?>
    PHP Code:
    <?

    ?>
    If you noticed, I offered two examples. One is considered long-hand, which tells the server that anything between <?php and ?> are going include php syntax. The other is short-hand, which still tells the server what the script is going to be, but the server has to add in the language. It's not quite as optimized as the long-hand counter part, but with todays technology...who cares.

    Comments
    Comments are VERY useful in any language, because you can use them to remind yourself what you did, why you did it, and if you share it with another person, you can use it to explain your poor organization skills.

    There are several ways to do comments in PHP. Pending on the languages that you might have learned first would be the ones you will probably use, but /* */ HTML comments work with PHP, ## C comments work with PHP, and // works with PHP. Here's an example:

    PHP Code:
    <?php

    ################
    ################
    ##            ##
    ## C Comments ##
    ##            ##
    ################
    ################

    /* 
        HTML 
          Multiline
              Comments
    */

    <!-- HTML Single Line Comments dont work too good -->

    // I personally prefer these comments, guess Im lame.

    ?>
    Echo and Print
    To display information in PHP, there are two commands, echo and print. They both do the same thing, so it's your preference on which you will be using. I like echo, it sounds cooler. Ok, here is where we actually get to coding, so the way I'm going to do this, is I'm going to explain as I go. KK?

    PHP Code:
    <?php
    echo "I love VD-Core! <br />";
    print 
    "I love VD-Core! <br />";
    echo (
    "I love VD-Core! <br />");
    print (
    "I love VD-Core! <br/>");

    echo 
    "Now you see the several ways of displaying information.  But do you \"KNOW\" what they mean?";

    ?>
    Output:
    I love VD-Core!
    I love VD-Core!
    I love VD-Core!
    I love VD-Core!
    Now you see the several ways of displaying information. But do you "KNOW" what they mean?
    Ok, explaining time! As I said before, echo and print are based off preference, they both do the same thing. There are also two valid ways of coding them, as displayed above. Some people will tell you that the parenthesis are the required way to go, but it's balogna, promise. A few things to remember, and that is your proper syntax. Text that you put in your echo commands will be in parenthesis, variables however will not. You will see more of that later. You also probably noticed the <br />. In echo commands, HTML is completely acceptable. It's not showing the client what is inside of those quotes, it is supplying the browser the HTML output to show to the client. Keep that in mind, it might be useful on future projects. At the end of our echo commands, and pretty much every other PHP command you will use, is the semicolon. This tells the script that particular command is done.

    To explain the quotes in \"KNOW\", you always use a \ in front of quotation marks when you are doing any kind of input in PHP, whether it's a query, an echo command, anything. This tells the script that is a quotation, and not the end of the information it is exporting to your browser. Rather similiar to &quot; in HTML.

    Variables
    Variables are all over the place in PHP. You will be using them in almost every script, so use them now. Anything you see $x = y, that is a variable identification. $x being the variable, y being the identification. You use variables for calling sql arrays, you use variables to display dates that way they are in real time, as I said, you will use them for everything. Allow me to show you an example.

    PHP Code:
    <?PHP
    // Here are your variables.
    $name="Joshua Drake";
    $location="Kentucky";
    // Here is what you are printing to your page.
    echo "Hello, my name is " $name "<br />";
    echo 
    "I live in " $location;
    ?>
    This is what the output will look like:
    Hello, my name is Joshua Drake
    I live in Kentucky
    Require and Include
    To make things easier, you can always use require or include to call files, that way you can slim down your documents, meaning if you need to make changes you won't have to go through hundreds of lines of codes OR you can include something that will be used on all of your pages. Keep in mind, that when you call a document, it will place it exactly where in the code you inserted it. To show an example, we are going to be making a small document to store some variables. When you are finished, save it as var.php, just for reference. Here's what I want you to put in that file:

    PHP Code:
    <?PHP
    // Here are your variables
    $jd "Joshua Drake";
    $m6 "Pat Andrew";
    $d date("D");
    ?>
    Heres the file I want you to really work on, and it's going to show your include.

    PHP Code:
    <?PHP
    // This will call your document.  Take note of the syntax.
    require_once('var.php');

    // Notice that I use require_once instead of require or include.
    // That's because it's only variables in that file, not something
    // that needs to be read over and over.

    echo "Hello" $jd "<br />";
    echo 
    "Hello" $m6;
    ?>
    Here is the output:

    Hello Joshua Drake
    Hello Pat Andrew
    If / Else / Elseif
    If you were curious about that date variable I threw at you earlier, it's time to put that into use. If you will notice, I used date("D"). You can replace the D with several things, and also use a combination of things. Check out PHP: date - Manual for reference of the different ways you can display time and date. On with the tutorial:

    PHP Code:
    <?PHP
    // This, again, is to call your variables.
    require_once('var.php')

    // I want you to notice the use of the if/else comments.

    if ($d="Fri")
    {
    echo 
    "Have a nice weekend";
    } else {
    echo 
    "Shucks, it's only " $d ;
    }
    ?>
    Ok, here's what all of this means. The use of if/else, is that if whatever the condition the if comment needs to be true, it will go by whatever is in your first set of brackets {}. If it is false, it will skip to what is in the else brackets {}. So, if the date equals friday, it will tell you to have a nice weekend. If the day is something else, it will tell you Shucks, it's only mon..or tue...or whatever kinda day it is.

    If you get a messege like this:

    Strict Standards: date() [function.date]: It is not safe to rely on the system's timezone settings
    That means your server isn't automatically setting a time zone based around your time. If that is so, you need to set one for it. Check that site I through out earlier (PHP: date - Manual) and it will tell you what the different time zones are that you can use.

    In Closing
    This is all for my basic PHP introduction. If you have any questions, feel free to ask. I will hopefully be creating more tutorials, such as using your SQL databases in PHP, more advanced SELECT queries, etc.
    Last edited by jd-inflames; 02-17-2009 at 12:43 PM.
    Zombie Survival Guides | Zombie Game Reviews | Zombie Movie Reviews
    Drake Technologies Forums - What do you know?
    Drake Technologies Partnership Program - Ever wanted your own website?


    Cold was my soul
    Untold was the pain
    I faced when you left me
    A rose in the rain....
    So I swore to the razor
    That never, enchained
    Would your dark nails of faith
    Be pushed through my veins again

  2. #2
    VDC Team Member
    Join Date
    Feb 2004
    Location
    Kentucky
    Posts
    50

    Re: Introduction to PHP

    It's done, sir. Little late, but as promised.
    Zombie Survival Guides | Zombie Game Reviews | Zombie Movie Reviews
    Drake Technologies Forums - What do you know?
    Drake Technologies Partnership Program - Ever wanted your own website?


    Cold was my soul
    Untold was the pain
    I faced when you left me
    A rose in the rain....
    So I swore to the razor
    That never, enchained
    Would your dark nails of faith
    Be pushed through my veins again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts