Loading...
  1. Dismiss Notice
  2. Dismiss Notice
  3. Dismiss Notice
  4. Dismiss Notice
  5. If this is your first visit, you may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

[Tutorial] Basics for PHP

Discussion in 'Programming Lounge' started by Kymiloi, Apr 28, 2012.

Thread Status:
Not open for further replies.
  1. Kymiloi

    Kymiloi Retired

    Joined:
    Jan 29, 2011
    Messages:
    385
    Likes Received:
    229
    Trophy Points:
    298
    So i guess you came here because you wanted to learn the basics of PHP?

    If so, great!

    However, if you already know the basics of PHP, then this guide will be of no use for you!

    Table of Contents
    • Introduction
    • Requirements
    • Your first script
    • Tags and comments
    • Echo ""
    • Variables
    • If/else
    • Ending... D:

    Introduction
    Welcome to my PHP tutorial!
    In this tutorial you will learn the basics of PHP (output, variables, if/else and on)

    If you think this tutorial will learn you to for example create your own website, then i must disappoint you.

    This tutorial will get you started in PHP, so you can continue learning!

    Requirements
    You need:
    • A webserver with PHP
    • A browser
    • Notepad

    Optional:
    • An IDE (Notepad doesn't count as an IDE for me)

    As for the webserver with PHP, my recommendation for beginners, is to get Xampp (Google is your friend).

    Xampp has all that you need, including MySQL, mail server, PHP and so on.

    As for the optional IDE, the reason this is very smart, is because many IDE's have syntax highlighting and such things.

    My favorite IDE at the moment is Notepad++ (Note the ++ at the end) however you can use whichever you like, as long as it supports PHP.

    Your first script
    In many tutorials, your first script is the "Hello World" script.

    However, not in this tutorial!

    In this tutorial, your first script will be the "You're awesome!" script!

    Kinda a big difference, huh?

    You may have seen a PHP script like this before, but here comes your first script:
    Code:
    <?php
    echo "You're awesome!";
    ?>
    
    Tags and comments
    The most important thing to do when you make a PHP script, is to remember the tags!

    Without tags, no script.
    No script, no action.
    No action, no cake D:

    The tags is the following.

    Start tag:
    Code:
    <?php
    
    End tag:
    Code:
    ?>
    
    What you do with them, is to surround your script with them.

    So if you have a script like this:
    Code:
    echo "hellow im awesum!";
    
    echo "i amz nut D:";
    
    then you gotta make it look like this:
    Code:
    <?php
    echo "hellow im awesum!";
    
    echo "i amz nut D:";
    ?>
    
    Understand?

    So basicly it's just:
    Code:
    <?php
    //SCRIPT GOES HERE!!!
    ?>
    

    As for comments, they're not so important, however they are USEFUL.

    Use them when you can. Makes it much easier to find what you need in your scripts!

    There are two types of comments.

    Single-line and multi-line comments.

    It's pretty self-explanatory what they do.

    Single-line comments can only be on one line, if not the script freaks out.

    Multi-line comments can be on multiple lines, without the script freaking out.

    Single line comments looks like so:
    Code:
    echo "i am awesum?"; //Single line comments goes here
    
    while multi-line comments looks like so:
    Code:
    echo "No you are not awesum D:";
    /*
    multi
    line
    comments
    go
    here
    */
    
    So, single-line comments needs to be started off with
    Code:
    //
    
    and end with nothing.

    However, multi-line comments needs to be started off with
    Code:
    */
    
    and ended with
    Code:
    */
    
    .

    So now you've got startin/ending tags and comments in place! Time to move on!

    Echo ""
    This is one of the most basic things in PHP.

    Echo is a "function" (Well, i wouldn't really call it a function) that outputs the specified text/variable/function to the browser.

    Usage:
    Code:
    echo "TEXT GOES HERE!";
    
    You can also use it with variables, like so:
    Code:
    $i = 25;
    echo $i;
    
    You can even use it with functions, like so:
    Code:
    echo round(2.4235);
    
    Variables
    Variables is a very common thing in almost ALL programming languages.
    They are used for storing information.

    Example of a C variable:
    Code:
    int number = 25;
    
    Example of a PHP variable:
    Code:
    $number = 25;
    
    You can store many things inside variables.

    Basic rules for variables:
    • They must always start with $
    • The first character in the variable name MUST be a letter

    So to show the rules.

    Wrong variable:
    Code:
    lol = "hai";
    
    Right variable:
    Code:
    $lol = "hai";
    
    Variables can also store integers like so:
    Code:
    $a = 1;
    $b = 2;
    $c = $a + $b;
    
    As you can see i do "$a + $b"

    Which basicly means "1 + 2" as the value asigned to $a is 1 and the value asigned to $b is 2.

    If/else
    Without this, PHP would be nearly nothing.

    If/else is the thing that is used to check if a certain thing, is in a certain condition.

    Example:
    Code:
    $a = 255;
    $b = 64;
    
    if($a == $b) { // if $a equals $b
        echo $a . " equals " . $b;
    }
    elseif($a > $b) { // elseif is ran if the first if returns false. elseif $a is bigger than $b
        echo $a . " is bigger than " . $b;
    }
    else { // else is ran if every if/elseif above returns false
        echo $b . " is bigger than " . $a;
    }
    
    Ending... D:
    It's time to say goodbye D:

    Have a good time guys D:
     
    5 people like this.
  2. Smartik1

    Smartik1 Walking F.A.Q. Forum Legend

    Joined:
    Aug 1, 2010
    Messages:
    10,951
    Likes Received:
    5,503
    Trophy Points:
    588
    nice guide with basics :p however there already is a php guide here for setting up XAMPP and a link to w3schools that has everything u just said

    there r a few things i could use ur help with though if u have any idea :p

    1. i could use some explanation for the sleep function in php as it doesnt seem to work like in every other language. whenever i try to use the sleep command (including literally copy pasting an example from w3schools) all it does is make the page take forever to load (as long as the sleep duration is) and then shows all the results at once rather than showing everything before the sleep, waiting and then showing whats supposed to come after the sleep

    googling didnt really help. someone did mention that sleep shouldnt really be used to cause delay and mentioned it should be used together with a flush command but yea, no idea

    2. setting up a php mail client. i have serious issues with that (after half a day of googling seems like so does everybody else). i wanna set it up to use my hotmail smtp without using mercury/outlook/other bs

    saw lots of things mentioning the php.ini and sendmail.ini in xampp but the vast majority of those were for gmail and none of their advice actually worked. i get no errors anymore (used to get a port one but i changed it to the hotmail smtp which is 587) and error logs show nothing, php file says sent successfully but i get no email. i really dont know what to do anymore lol, tried everything
     
  3. Fantaros

    Fantaros Banned Forum Legend

    Joined:
    Oct 9, 2010
    Messages:
    4,455
    Likes Received:
    1,864
    Trophy Points:
    0
    Location:
    -
    nice one john ;)
     
  4. OmgHaters

    OmgHaters Proficient

    Joined:
    Dec 3, 2011
    Messages:
    281
    Likes Received:
    74
    Trophy Points:
    0
    No cake? I feel dissapointed.
     
  5. Kymiloi

    Kymiloi Retired

    Joined:
    Jan 29, 2011
    Messages:
    385
    Likes Received:
    229
    Trophy Points:
    298
    hewhew i will think and what you said
    btw
    the thing whit the mail its more simple if you use apache, anyway,even whit apache you can get issues,
     
  6. Smartik1

    Smartik1 Walking F.A.Q. Forum Legend

    Joined:
    Aug 1, 2010
    Messages:
    10,951
    Likes Received:
    5,503
    Trophy Points:
    588
    actually xampp runs apache and apache isnt a mail server, it doesnt have SMTP support (kinda why they put mercury there but u need a mail client like outlook for that)
     
  7. Kymiloi

    Kymiloi Retired

    Joined:
    Jan 29, 2011
    Messages:
    385
    Likes Received:
    229
    Trophy Points:
    298
    ik that xampp runs apache and dosent have a mail server, but its easyer cuz you can build your own...on apache
     
  8. gen_202

    gen_202 Proficient

    Joined:
    Oct 29, 2010
    Messages:
    139
    Likes Received:
    8
    Trophy Points:
    238
    Location:
    Tagbilaran city, bohol Philippines.
    word press win!!! ^^
     
  9. brucejje

    brucejje Field of chaos

    Joined:
    Dec 21, 2012
    Messages:
    137
    Likes Received:
    31
    Trophy Points:
    0
    Location:
    NOD HQ
    ahahah christmass 0day ftw!
     
  10. manestic

    manestic Banned

    Joined:
    Jan 10, 2013
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    223
    Location:
    The Netherlands
    Looks awesum! Goodjob! The beginners can learn a lot from this.
     
Thread Status:
Not open for further replies.

Share This Page