Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 2 - PHP Code


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:10 PM

    If you visited the link in the last lesson you probably already know how to tell PHP code and how to comment it. Never the less, I want to make sure and go over it just so that no one is confused.

    This is what PHP code looks like by itself:


    [php]
     ...(more code)....
     ...(more code)....
     echo "$$var dollars spent.";
     ...(more code)....
     ...(more code)....
    [/php]



    Everything between the opening ";" tags is sent to the PHP core to be processed, then the result is sent back. and the rest of the page continues to load. Here is what PHP code might look like in an HTML file:

    <HTML>
    <HEAD>
    <TITLE><?php echo $tile; ?><TITLE>
    </HEAD>
    <BODY>
    <?php echo "Welcome to $sitename"; ?>
    <p>We are committed to serving you!</p>
    </BODY>
    </HTML>

    Don't worry if you don't understand it, this is just so you know what to expect.

    Another important part of programming is to document your code. Imagine trying to find a function or variable in a 1000 line PHP document! Without any human comments in the file it could take you forever to find what you were looking for. Here are the two most common forms of placing "Human Readable" text in a program:



    [php]
    // two forward slashes tell the program to ignore what is behind them on the line.
    [/php]

    [php]
    /*
    This is how you "comment" multiple lines in something.
    Everything between the opening "/*" and closing "*/" is
    Ignored by the program. This form is more common than the
    "//" (Double-backslash) because unlike the other form this
    one is not limited to only ONE line of text.
    */
    [/php]

    The text in both of these "comments" is ignored by PHP. Here is an example of using them in a script:



    [php]
    /* CONFIGURATION of connect to DATABASE SECTION*/
    $servername='localhost'; // Replace 'localhost' with your server name
    $database_username='root'; // Replace 'root' with your username
    $database_password='password'; // Replace 'password' with your password
    $database_name='database_1'; // Replace 'database_1' with your database name
    
    /* take all of the values and connect to the database */
    mysql_connect($servername,$database_username,$database_password);
    mysql_select_db($database_name);
    [/php]


    See how the comments help to describe what to do here? While you probably don't know what these functions are; you can get a general idea of what this script does thanks to comments in the code.



    One thing you need to understand before we go any further is the concept of "variables". (If you are a programmer you probably already have a knowledge of variables. Therefore you are welcome to skip this intro.)

    Wikipedia.org describes a variable as: "...a place to store a value in computer memory." A variable is stored in a "byte" (or collection of bytes) in the computers memory. Variables store some value like a phone number or age in the bytes. Most computers now-a-day have about 256MB (Mega-bytes of memory) which is 256,000,000 bytes (Mega means "million").

    If I have lost you I apologize. Maybe you can understand variables better if I relate them to P.O. Boxes at the post office. There are many different P.O. boxes (Think "variables")in your post office. At any time they could be used to store many different pieces of information - your paycheck, a letter from grandma, a warrant of arrest for that guy down the street...ANYTHING. Therefore they are like variables, they store different pieces of information and what they hold can change from day to day.
    Here is what a variable in PHP looks like:
    Example:


    [php]
    $variablename
    [/php]

    Right now there is nothing in the variable "$variablename". So lets add something..
    Example:

    [php]
    $variablename = 'John';
    [/php]
    Now the variable "$variablename" holds the value "John". Think of it like this: If you opened up "$variablename" you would find the word "John" inside! (Just like PO Box "204" holds a postcard.) "Capitalization is very important in PHP. It is generally a good idea to have all variables be entirely lowercase, and only use alphanumeric characters along with underscores (_) if necessary. $greeting is not the same as $Greeting, so it is important to remain consistent and watch the typos." -seretogis Below is a list of links for you to follow so that you can learn the syntax of the PHP variables and how to use them. These are the best tutorials I have found so have fun and see you in lesson 3!




    Melonfire - Intro & Variables

    free2code - "Variables"


    PHPFreaks - Variables