[PHP] Basic Starting Tutorial [PHP]

Discussion in 'Web Programming' started by Annex, May 16, 2012.

[PHP] Basic Starting Tutorial [PHP]
  1. Unread #1 - May 16, 2012 at 5:19 AM
  2. Annex
    Joined:
    Aug 28, 2005
    Posts:
    2,324
    Referrals:
    3
    Sythe Gold:
    0
    UWotM8?

    Annex Ballin'
    Veteran (Ex-Admin)
    PHP Programmers Retired Administrator

    [PHP] Basic Starting Tutorial [PHP]

    So I have decided it would be good to impart my knowledge of php on the forums in several different threads. Why do so? Well because teaching something forces you to relearn and make sure you know for certain.

    So lets start at the beginning.

    How do you make a php file? Its simple, open any ide or even note pad and put the follow code in

    Code:
    <?php
    ?>
    These essentially function as the opening and closing brackets that tell the server to process whatever is between using PHP, you alternatively can use
    Code:
    <? ?>
    for the same result, however i prefer to only use that when space is an issue which it isn't when you are starting to code php.

    Now you will probably have a lot of questions, so for now lets answer a very basic one, how do I make the screen say stuff. Well its simple the echo function. This serves as the print function if you are familiar with java or C. I'll also use this to demonstrate proper syntax. So to display the text "hello sythe" you would use the echo function like this:

    Code:
    <?php
    echo "Hello Sythe";
    ?>
    The webpage would display the text Hello Sythe exactly like that. The semicolon that proceeds the echo function is telling PHP that that is the end of the line and separates the commands if you are jamming them all onto one line, but that will not be the case for our goals, however after every command you will want one of these or you will get an error.

    Also if you put quotation marks it will literally echo what you type, if you want to use a variable statement you don't put quotation marks.

    That brings us the the next part of the tutorial, variables. Variables in PHP don't need to be predeclared like in C or Java, you can call them when you wish to use their values if you are using a single file for a webpage. If you wanted to echo the same phrase using a variable named message it would look like this:

    Code:
    <?php
    $message = "Hello Sythe";
    echo $message;
    ?>
    If your variables are numbers (integers) you can also perform mathematical operations to them like so:

    Code:
    <?php
    $x = "5";
    $y = "7";
    $z = $x + $y;
    echo $z;
    ?>
    The result would be the number 12 appearing on the screen. If you want to multiply you would use * (shift 8) to divide you use /.

    If you want echo multiple variables you would do it like this

    Code:
    <?php
    $x = "5";
    $y = "7";
    $z = $x + $y;
    echo $x . " + " . $y . " = " . $z;
    ?>
    The result should be 5 + 7 = 12

    The . actually tells php to echo both of the variables together (called the concatenation function), but its important to remember that it DOES NOT ADD ANY SPACES BETWEEN THE VARIABLES so if you put echo "hi" . $z; it would appear as hi12.

    Lastly we are going to look at the if statement which is very important to dynamic web pages. I will explain some more syntax aswell that will help you be more efficient with PHP. Using if there are 3 main commands: IF which means that the code is executed if the following statement applies; ELSEIF which goes AFTER the initial if which is the code used if something specific other than the original if is true; ELSE is the last of the 3 and executes when the IF and ELSEIF's aren't true. Lets use an example with the date then talk about the syntax.
    (don't worry about the $d = date("D"); that is the date function and is a global one)
    Code:
    <?php
    $d = date("D");
    if ($d == "Mon")
    {
    echo "Hungover Day";
    }
    elseif ($d == "Wed")
    {
    echo "Hump Day";
    }
    else
    {
    echo "Other Day";
    }
    ?>
    
    On Monday you would see Hungover Day, Wednesday you would see Hump day and every other day you would see Other Day.

    A note on syntax here is that the brackets { and } are NOT necessary, but it makes the coding easier to read, and easier to find mistakes if you have a lot of code to go through. If you use a { you MUST have a } after it or you will run into a syntax error.

    Going back to IF,ELSEIF and ELSE. For each if statement you only need the if statement, the others are not needed by syntax in your code. It is very common to use both IF and ELSE. ELSEIF is sort of an uncommon statement that applies to specific situations really.

    Now looking at the actual statement if ($d == "Mon"), this is like when using echo, if you are using a variable you don't need to use quotation marks but if you aren't you do. The statement also MUST be closed in brackets. Lastly you NEED two equal signs to allow PHP to know you are comparing the values and not give you an error.

    You can also say if something is not equal by using: if ($d != "Mon"), and if you have numerical variables you can use greater than or equal to and less than or equal to by using the following respectively:

    if ($x >= $y) (greater than)
    if ($x <= $y) (less than)

    IF YOU TRY TO COMPARE TEXT USING THE ABOVE TWO YOU WILL GET AN ERROR.

    Okay I lied one more thing, comments its simple and easy type // before anything and it becomes a comment and ignored by php. If you have a multi line comment then you can use /* to open and */ to close. so it would look like this:

    Code:
    <?php
    /*THIS IS A
    MULTI LINE
    COMMENT*/
    echo "hello sythe"; // THIS IS A ONE LINE COMMENT
    ?>
    
    That's all for now i'll get into more specifics later.
     
  3. Unread #2 - May 16, 2012 at 5:28 AM
  4. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    [PHP] Basic Starting Tutorial [PHP]

    What's the difference between doing :

    Code:
    $x = '8';
    $x = "8";
    ?
     
  5. Unread #3 - May 16, 2012 at 9:33 PM
  6. Jazz00006
    Joined:
    Aug 26, 2005
    Posts:
    807
    Referrals:
    0
    Sythe Gold:
    0

    Jazz00006 Apprentice
    Visual Basic Programmers

    [PHP] Basic Starting Tutorial [PHP]

    Put simply;
    "" allows you to use variables inside the quotes.
    '' doesn't allow you to use variables inside them

    Code:
    $a = 10;
    
    $x = "$a + 8 = ?" // This prints 10 + 8 = ?
    $x = '$a + 8 = ?' // This prints $a + 8 = ?
    
    http://php.net/manual/en/language.types.string.php


    You could use printf to sprintf to achieve a similar effect
    Code:
    printf("Variable x is %s",$x);
     
  7. Unread #4 - May 17, 2012 at 12:04 AM
  8. Annex
    Joined:
    Aug 28, 2005
    Posts:
    2,324
    Referrals:
    3
    Sythe Gold:
    0
    UWotM8?

    Annex Ballin'
    Veteran (Ex-Admin)
    PHP Programmers Retired Administrator

    [PHP] Basic Starting Tutorial [PHP]

    It also allows parses quotes as /" I believe which is useful much later on.
     
  9. Unread #5 - May 20, 2012 at 12:45 AM
  10. RS2Legit
    Joined:
    Jul 8, 2011
    Posts:
    92
    Referrals:
    1
    Sythe Gold:
    0

    RS2Legit Member

    [PHP] Basic Starting Tutorial [PHP]

    Just as a comment on the tutorial;

    It is a great guide for introduction to syntax and basic structures; enough to excite interest and then seek any more advanced information which they need. One thing that I would like to say, generally when declaring variables it is understood that you will define them to be the type of variable which they are; for instance, all integers will be defined as integers rather than strings.

    So you can define an integer without using quotations, and then the variable will be an actual integer value rather than a string representation. It doesn't make a huge deal in functionality, but it is good practice and will help with teaching safety later on.

    As a side comment about the "" vs. ''. If you are just using simple strings, no variables etc. it is good practice to use the ''. They are processed by the script more quickly!

    Great tutorial for beginners, thanks for the post.
     
  11. Unread #6 - May 25, 2012 at 9:05 AM
  12. williamxaviers
    Joined:
    May 23, 2012
    Posts:
    23
    Referrals:
    0
    Sythe Gold:
    0

    williamxaviers Newcomer

    [PHP] Basic Starting Tutorial [PHP]

    Great ! I find many people asking about the topics like I want to learn about Java, php etc. From the next time I am going to point them to this thread. Even I am also going to put relevant queries from the next time.
     
  13. Unread #7 - Jun 1, 2012 at 2:19 PM
  14. Teknology
    Joined:
    Jun 1, 2012
    Posts:
    16
    Referrals:
    0
    Sythe Gold:
    0

    Teknology Newcomer

    [PHP] Basic Starting Tutorial [PHP]

    Thanks for making this.
    this is handy for beginners
     
  15. Unread #8 - Jun 12, 2012 at 4:52 PM
  16. Goldey
    Joined:
    Nov 26, 2011
    Posts:
    102
    Referrals:
    1
    Sythe Gold:
    0

    Goldey Active Member
    Banned

    [PHP] Basic Starting Tutorial [PHP]

    Easy-to-understand tutorial, and very useful. Thank you!
     
  17. Unread #9 - Jun 19, 2012 at 2:58 AM
  18. williamxaviers
    Joined:
    May 23, 2012
    Posts:
    23
    Referrals:
    0
    Sythe Gold:
    0

    williamxaviers Newcomer

    [PHP] Basic Starting Tutorial [PHP]

    I agree that its easy to understanding and its very useful to all. I think it is very useful for the beginner and for them who want learn PHP. Its really gives good guidelines about all the syntax and basic structure which is very useful to all.
     
< [Assilio] NEED SEO DONE ON MY WEBSITE! | XML Forum Template/Style? Will pay. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site