Welcome our webmaster and SEO forum
Please enjoy the forum, contribute what you can, and wind up the Moderators!
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Tutorial: A Complete Beginner's Guide to PHP

  1. #1
    jackfranklin is offline Junior Member
    Join Date
    Feb 2008
    Posts
    6

    Default Tutorial: A Complete Beginner's Guide to PHP

    Ok. You’ve got a basic knowledge of HTML and preferably CSS. Where now? It has to be something dynamic next! You have a choice of ASP, Javascript and PHP.

    Why Choose PHP?
    PHP is a loosely typed language. Simply put, it’s not so strict with the way you code it. One tiny error is not likely to throw the entire script off.

    The Very Very Basic PHP
    All PHP goes inside tags.
    Start tag:
    PHP Code:
    <?php

    End Tag:
    PHP Code:
    ?> 

    Firstly, don’t worry. I’m not going to show you how to do that boring ‘Echo World’ statement over and over. Let’s say we want to be able to work out the Area and the Perimeter of a rectangle. Data in PHP (Number or Text) are best stored in variables. [NOTE: A List of Items is best stored in an array, but we will see that a bit later on].

    The variable sign in PHP is the dollar sign on your keyboard (Above the 4). ‘$’. You then place the name of your variable after it. Example:
    PHP Code:
    $width 

    You then need to give this a value. You need to use a single equals sign after it. This is were the ‘loosely typed’ bit comes into play. You can either use:
    $width= OR $width =
    Personally, I always place a space around the ‘=’ so it’s easier to read, but it’s your choice. [NOTE: If you leave a space before the ‘=’ it’s always best to leave one after it as well. Also, choose your favourite way and stick with it don’t switch from one to the other]
    Ok, back to our rectangle. The rectangle will have 2 values which will change, the width and the height. So, we just create our two variables.
    [NOTE: From now on in this tutorial I’m going to put spaces around my ‘=’ but if you don’t want to you don’t have to, just change it.]
    PHP Code:
    $width 
      
    $height 

    Let’s say, this rectangle has a width of 5 and a height of 2.
    PHP Code:
    $width 5//This is the width of my rectangle
      
    $height 2//This is the height of my rectangle. 

    Woah! There are a couple of things to look at there!
    Firstly, note the ‘;’ after each line. This is needed after you declare the variable to tell PHP that that variable is finished, you have stopped declaring a value for that variable. Secondly, note the ‘//’ and then a comment. This is the PHP way of declaring comments. You know, in HTML you use <!—Comment-->, well in PHP you just place the ‘//’ in front of your comment.
    So, now our rectangle has a width and a height, we can work out the area and the perimeter. The area is the width multiplied by the height, and the perimeter is the Width, add the height, add the width, add the height, or in other words, (Width + Height) multiplied by 2. So, how can we do this in PHP? Let’s take a quick look at PHP Operators!
    Operators, or at least what I take as operators, are PHP’s way of dividing, multiplying, etc. Below are a list of Operators and what they do. I advise you learn these, they are not all what you expect:
    Addition – (+)
    Subtraction – (-)
    Multiplication – (*)
    Division – (/)
    Modulus – (%)
    Addition by One – (++)
    Equal To – (==)
    Not Equal to – (!=)
    Less Than – (<)
    More Than – (>)
    Less Than or Equal to – (<=)
    More Than or Equal to – (>=)

    Note that Equal to is a double ‘=’ not just a single one. This is because a single equals sign is a ‘giver’ – it gives something a value. EG: $variable = 2.

    So, back to our rectangle, to work out the area we will need to use multiply (*) and to work out the Perimeter, we will need to use Add (+) and Multiply (*).
    I’m going to create 2 new variables, $area and $perim.
    PHP Code:
    $area 
      
    $perim 

    Now, to work out the area, we multiply $width and $height.
    PHP Code:
    $area $width $height

    The perimeter is ($width + $height) * 2. This can be done in PHP. You are still able to use brackets.
    PHP Code:
    $perim = ($width $height) * 2

    So we now have worked out the area and perimeter. But, we want to show these to our user! In PHP, to display something, you use echo.
    PHP Code:
    echo 'The area of our rectangle is: '.$area.' and the perimeter is: '.$perim.' We worked them out using Operators!'

    You surround the entire echo statement in ‘ and ‘. All the text, including any HTML tags, need to be surrounded by ‘’. To display a variable, this is how you would.
    • You have a simple echo statement:
      PHP Code:
      echo '<p>What is the width of my rectangle?</p>'
    • To add a variable in after the question mark, firstly you need to add a single speech mark (').
      PHP Code:
      echo '<p>What is the width of my rectangle?'</p>'; 
    • Then add a full stop:
      PHP Code:
      echo '<p>What is the width of my rectangle?'.</p>'; 
    • Now add your variable name, another full stop, and another single speech mark.
      PHP Code:
      echo '<p>What is the width of my  rectangle? '.$width.'</p>'
    • In simple terms, all variables in echo statements must be surrounded with a: ‘. and a .’
    But there is an exception. If you only want to display a variable with no text you do it like so:
    PHP Code:
    echo $width

    Easy as that!

    So, we can now show off to the user. See if you can, on your own, display the width, height, area and perimeter in echo tags, each in a <h4> tag with ‘Width is: ‘ before them. (Replace width with whatever). See below for the answer.
    PHP Code:
    echo '<h4>Width is: '.$width.'</h4>';
      echo 
    '<h4>Height is: '.$height.'</h4>';
      echo 
    '<h4>Area is: '.$area.'</h4>';
      echo 
    '<h4>Perimeter is: '.$perim.'</h4>'


    That’s it!
    Last edited by jackfranklin; 02-19-2008 at 02:47 PM.

  2. #2
    jackfranklin is offline Junior Member
    Join Date
    Feb 2008
    Posts
    6

    Default

    More - I went over the post limit.
    Arrays
    Nearly there! Remember earlier I mentioned a list going in an array? This is how.
    An array is simply a list of items. To create an array you do the following:
    PHP Code:
    $worker[0] = "Jack";
    $worker[1] = "Ed";
    $worker[2] = "Alex";
    $worker[3] = "Rob"

    This creates a list of items. The variable $worker is now an array. [NOTE: That the first item is not the ‘1st’ but the ‘0th’.
    To display them, you just echo them like we did above:
    PHP Code:
    echo $worker[0]; //Displays Jack 

    PHP Code:
    echo 'One of the workers is called: '.$worker[2].' He is a nice worker'//Displays: One of the workers is called Alex He is a nice worker. 

    Happy?

    That is indeed the basics of PHP covered as simply as I can put.

  3. #3
    charlie123 is offline Junior Member
    Join Date
    Oct 2008
    Posts
    13

    Default

    Thanks u it is really very good.

  4. #4
    Mckee is offline Member
    Join Date
    Mar 2009
    Posts
    50

    Default

    nice, what are the basic for guideline for beginner?...thanks for your sharing the code above....

  5. #5
    liveSEO is offline Junior Member
    Join Date
    Apr 2009
    Posts
    2

    Default

    i can not understand where i write it? and in what name it will be saved?
    can i write in notepad like HTML and save in *.php.
    thanks

  6. #6
    craftygeek is offline Junior Member
    Join Date
    Jun 2010
    Location
    Norfolk, UK.
    Posts
    21

    Default

    Thats probably the cleanest & easiest introduction to PHP I've seen - nicely done.

    For anyone wanting to take PHP a step further, W3 Schools have a good PHP reference section.

    In answer to the post above - you can write php in any text editor just as you can with html...the php extension tells the server to run the PHP code to assemble the HTML before sending it to your browser when you view it....also if testing on your own computer, you will need a text environment (WAMP, LAMP, XAMP or MAMP depending on your computer).

  7. #7
    I4Visual's Avatar
    I4Visual is offline Junior Member
    Join Date
    Jul 2010
    Location
    Laindon, Essex
    Posts
    21

    Default

    Hey

    Thanks alot for the really simple guide, I'm getting into learning php myself atm, it's really quite simple the way you put it, not in the massive books.
    I4 Visual Media Ltd. - Website Designers

  8. #8
    Alfred is offline Junior Member
    Join Date
    Aug 2010
    Posts
    8

    Default

    Hi, as we know that php is a server side scripting language, which is run on server like Apache.
    and Before going to that i suggest you go to client side scripting like HTML, CSS,Java script ad many more,
    Here i also mention URL that help you, w3schools.com.

  9. #9
    damienb is offline Junior Member
    Join Date
    Oct 2010
    Posts
    10

    Default

    Quote Originally Posted by liveSEO View Post
    i can not understand where i write it? and in what name it will be saved?
    can i write in notepad like HTML and save in *.php.
    thanks
    You can use notepad when creating php files. You just must save it as *.php. But there are much better php editors like Dreamweaver and notepad++
    Last edited by damienb; 11-18-2010 at 10:20 AM.

  10. #10
    nikita123 is offline Junior Member
    Join Date
    Feb 2012
    Location
    india
    Posts
    25

    Default

    PHP is a programming language

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Bookmarks

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124