A Beginner's Guide to Php [Tutorial 2]

Discussion in 'Web Programming' started by beast217, Jul 22, 2009.

A Beginner's Guide to Php [Tutorial 2]
  1. Unread #1 - Jul 22, 2009 at 10:51 AM
  2. beast217
    Joined:
    Jul 22, 2009
    Posts:
    9
    Referrals:
    0
    Sythe Gold:
    0

    beast217 Newcomer

    A Beginner's Guide to Php [Tutorial 2]

    Hello again.



    Today we are going to be covering simple variables. Now, for those of you who dont know what a variable is, here is the "buy-the-book" definition for it:

    Variables in php are different to variables in most other programming languages. In php a variable is denoted by a "$" at the beginning of the variable name.

    Quickly before we go more in-depth about variables, you should know that in php, and some other programming languages, there are different meanings behind a different amount of equal's signs. Have a look:

    Code:
    =
    1 equal sign means your assigning a value to something.

    Code:
    ==
    2 equal signs means your comparing a value to something.

    Code:
    ===
    3 equal signs are more complex, and you do not need to know the meaning behind it until much later in these tutorials.

    Also, throughout these tutorials, you will find me adding "//"s and "/*" to the php script. This signifies a comment, and will not appear, or have any effect in the script.

    Ok, back to variables. Have a look at a quick example:

    Code:
    
    [COLOR="#00ff00"]<?php[/COLOR]
    /*remember: we have to type the opening php tag so that the web browser   will know what language you are typing in.*/
    
    
    /* Take a note at a few things below: the $, the inverted comma's and the semi-colin at the end. The $ denotes the variable, the inverted comma's are necessary for any string ( If you dont know what a string is, look it up on google), and the semi-colin is necessary at the end of any php statement. */
    [COLOR="#00ff00"]$nameofvariable1 = "Hello World";[/COLOR]
    
    /*Note: Inverted comma's only should be used on strings, not boolean's ( true or false) and not numbers.*/
    [COLOR="Lime"]$nameofvariable2 = 76;[/COLOR]
    
    
    /*We close with a php closing tag, so that the browser knows if a transfer of different languages is taking place. If you only have php script in your whole page, the closing tag is not necessary, but most of the time you will using html in the same page as the php, so it might be a good idea to get used to adding closing tags. */
    [COLOR="#00ff00"]?>[/COLOR]
    
    
    In all of these tutorials, php code will be written in lime (green) and the comments will be left as normal.

    So why dont you go ahead and copy and paste the code above into your favourite text editor, and save it as index.php in c:/wamp/www/tutorial2 ( you will need to create a folder called tutorial 2).

    In your internet browser, navigate to localhost ( just type localhost) and click on tutorial 2.

    Have a look at what you get.

    Dont worry, the code is correct, you dont see anything because you have not told the computer to display your variable. Have a look at the next code(its the same code , but with 2 lines extra added to it.):


    Code:
    
    [COLOR="#00ff00"]<?php[/COLOR]
    /*remember: we have to type the opening php tag so that the web browser   will know what language you are typing in.*/
    
    
    /* Take a note at a few things below: the $, the inverted comma's and the semi-colin at the end. The $ denotes the variable, the inverted comma's are necessary for any string ( If you dont know what a string is, look it up on google), and the semi-colin is necessary at the end of any php statement. */
    [COLOR="#00ff00"]$nameofvariable1 = "Hello World";[/COLOR]
    
    /*Note: Inverted comma's only should be used on strings, not boolean's ( true or false) and not numbers.*/
    [COLOR="Lime"]$nameofvariable2 = 76;[/COLOR]
    
    
    /*These two following lines of code basically are telling the browser to display the 2 variable's we just created. */
    [COLOR="Lime"]echo $nameofvariable1;
    echo $nameofvariable2;[/COLOR]
    
    
    
    /*We close with a php closing tag, so that the browser knows if a transfer of different languages is taking place. If you only have php script in your whole page, the closing tag is not necessary, but most of the time you will using html in the same page as the php, so it might be a good idea to get used to adding closing tags. */
    [COLOR="#00ff00"]?>[/COLOR]
    
    
    Why dont you try that code out?

    What did you get?

    If you got this, well done, you just created your first php script.

    I know its not much, but you've gotta start out slow, make sure you get the basics right.

    Anyways i think thats enough for tutorial 2, cya at tutorial 3!
     
< A Beginner's Guide to Php [Tutorial 3] | A Beginner's Guide to Php [Tutorial 1] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site