Thread: [PHP] The basics of PHP

Results 1 to 10 of 10
  1. #1 [PHP] The basics of PHP 
    Registered Member
    whac's Avatar
    Join Date
    Nov 2011
    Posts
    176
    Thanks given
    35
    Thanks received
    84
    Rep Power
    245
    I figured some of you might want it. I thought that this post should go in this forum category instead of web development because this tutorial teaches the basics of general programming (using PHP as an example), not web design.

    Note: This post was copied and pasted directly from my website. Also because I'm new to this forum, if I have broken any rules, please remove this topic if you wish (and contact me about what I've done).

    Anyways..


    Hi,

    If you are interested in web development, or if you want to start learning a loosely typed programming language (less strict), you might want to consider PHP. PHP stands for PHP: Hypertext Processor, been around for years and years. Open source, and millions of web developers use it in combination with Apache and MySQL. Being popular means that there will always be lots of help on the internet, so use it to your advantage. The internet is your friend.

    A portable web package (Apache + PHP + MySQL) I use is EasyPHP, and click [Only registered and activated users can see links. ] to download it. I assume that you can figure out how to install it properly. Make sure you install all the components just to be on the safe side. This tutorial requires that you have a basic understanding of computers, HTML, and common sense.

    After you run it, test to see if it works by going to your browser and typing [Only registered and activated users can see links. ]. If that doesnít work, type [Only registered and activated users can see links. ]. If only the latter works, you will have to remember that that is your address, not 80. You can always change your port in the Apache settings.

    Start by visiting your EasyPHP installation folder, and view the "www" folder. That is the folder which all your files will be stored. Open your text editor (I recommend Notepad++ for good syntax highlighting) and write this code (this is the hello world program):
    Code:
    <?php
    echo "Hello World!";
    ?>
    Save it in your www folder as index.php. Test by going into your browser and typing your address (localhost or localhost:8887). If it works, well, congratulations!

    The <?php and ?> are the starting and closing brackets of PHP. Anything else in between is the PHP code which it will process. PHP is case sensitive, meaning that "echo" and "Echo" arenít the same. As you can see, echo is kind of like print. The printed text should be enclosed in single or double quotes. The semicolon at the end means to end the command.

    Now with variables. I hope you know what they are. If not, think of Algebra. For example:
    Code:
    <?php
    $x = 5;
    $y = 2;
    $z = $x + $y;
    echo "Z is: " . $z;
    ?>
    Yeah, in PHP variables start with the dollar sign. Donít forget them! There are different types of variables, and the most basic ones in PHP are string (words, text, etc) and integers (numbers). Variables x and y are declared with the values 5 and 2, respectively. The variable z will have a result of 7, right? The final command is just printing the value of z. The dot in PHP in this sense is kind of like adding two objects together. The other basic arithmetic functions are * (multiply), - (subtract), and / (divide).

    Example of text:
    Code:
    <?php
    $name = "Justin";
    echo "My name is " . $name;
    ?>
    The next crucial basic factors of programming are conditional statements. If this, do this. If not, do that. For example:
    Code:
    <?php
    $age = 17;
    if ($age == 15)
    {
    	echo "You are the same age as me.";
    }
    else if ($age > 15)
    {
    	echo "You are older than me.";
    }
    else
    {
    	echo "You are younger than me.";
    }
    ?>
    And there you have it. This example demonstrates conventions. Like, you don't HAVE to tab a few spaces for the echo statements. But it is helpful when others look at your code (and you yourself too). There are many conventions in programming, and I may make a tutorial solely on conventions later on. I hope you can understand the syntax of the brackets by using common sense. If statements do not contain a semicolon because they are not actions. Functions, conditional statements, and many other parts in PHP are enclosed in brackets. When testing for equality, == is used, not =. This is a very easy mistake that many programmers make in any language.

    Comments are used to make code easier to read. They don't do anything, but it is good programming practice to include comments in your code so other people (and yourself) will be able to write and read your own notes.

    Example of comments:
    Code:
    <?php
    ################
    # I Like Comments
    # by JZ
    #
    # March 26, 2012
    ################
    
    $x = 17;
    $y = 2;
    
    /*
    * below is a cool method
    * which i really enjoy due
    * to the fact that comments
    * are cool and good looking
    */
    
    function multiply($x, y)
    {
    	return $x * $y; //this returns the product of argument 1 and argument 2
    }
    
    echo "The product is " . multiply($x, $y); //calls the multiply() function and echos it to the user
    
    //end of program
    ?>
    And there you go. Use comments whenever you can.


    Next up, functions. Why use them? First off, they are are a good method to organize your code. Second, they are convenient to use. Instead of writing 100 lines for your so-and-so application and copying and pasting the same code, you can use functions. They are also very versatile.

    Here's an example:

    Code:
    <?php
    $firstNumber = 2;
    $SecondNumber = 3;
    
    function add($a, $b)
    {
    	return $a+$b;
    }
    
    echo "The sum of " . $firstNumber . " and " . $secondNumber . " is " . add($firstNumber, $secondNumber);
    ?>
    As you can see, functions can return a value. But they don't have to if you don't want them to return anything:

    Code:
    <?php
    function displayBoldText($str)
    {
    	echo "<b>" . $str . "</b>";
    }
    
    displayBoldText("This text should be bold.");
    ?>
    A common problem people face when using functions is the state of the variables. Variables declared in a function will not be able to be accessed outside of the function. What people commonly do is attempt to call a variable that was declared outside of a function without realizing that the variable's state is not global. To fix this, use the keyword global:

    Code:
    <?php
    $myName = "Whac";
    
    function displayMyName()
    {
    	global $myName;
    	return $myName;
    }
    
    displayMyName(); //will result in 'whac' because it calls the global variable $myName
    ?>
    In an earlier example, you should have observed the arguments inside the function. Arguments are very useful to provide data for your functions to use. For example, these are some built in functions that use arguments:

    Code:
    <?php
    $aString = "lmfao";
    $s = strlen($aString); //in this case, a single string is the argument for this function
    $con = mysql_connect("localhost", "root", $aString); //three arguments in this example, ip, user, and password
    ?>
    Hereís a very good website: [Only registered and activated users can see links. ] to learn from.

    More to come later.
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Registered Member

    Join Date
    Jan 2012
    Posts
    213
    Thanks given
    143
    Thanks received
    130
    Rep Power
    223
    PHP stands for PHP: Hypertext Processor
    It stands for hypertext preprocessor and it's ugly.

    Reply With Quote  
     

  4. #3  
    Maggie
    Maggie's Avatar
    Join Date
    Jan 2011
    Posts
    559
    Thanks given
    218
    Thanks received
    46
    Rep Power
    53
    thanks i guess.
    Reply With Quote  
     

  5. #4  
    Banned
    Join Date
    Apr 2011
    Age
    24
    Posts
    1,363
    Thanks given
    370
    Thanks received
    184
    Rep Power
    0
    <?php and ?> are not brackets. {} are brackets.
    Reply With Quote  
     

  6. #5  
    Professional Shithead
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,242
    Thanks given
    3,273
    Thanks received
    2,871
    Rep Power
    5000
    We've got a web dev board. PHP ought to go there.
    Reply With Quote  
     

  7. #6  
    Respected Member

    kLeptO's Avatar
    Join Date
    Dec 2006
    Age
    23
    Posts
    2,955
    Thanks given
    1,177
    Thanks received
    753
    Rep Power
    3084
    Moved to website development section.
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  8. #7  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    23
    Posts
    770
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by Turkleton View Post
    It stands for hypertext preprocessor and it's ugly.
    No, he's right. It's a recursive acronym.
    Reply With Quote  
     

  9. #8  
    Registered Member
    whac's Avatar
    Join Date
    Nov 2011
    Posts
    176
    Thanks given
    35
    Thanks received
    84
    Rep Power
    245
    Quote Originally Posted by sKyrO View Post
    Moved to website development section.
    I don't mind the move to the Website Development section, but this tutorial was about general purpose programming, not web development. But okay.
    Reply With Quote  
     

  10. #9  
    Registered Member
    Join Date
    Feb 2012
    Posts
    91
    Thanks given
    7
    Thanks received
    6
    Rep Power
    12
    Good job.


    Reply With Quote  
     

  11. #10  
    Valar Morghulis


    Laxika's Avatar
    Join Date
    Sep 2006
    Age
    27
    Posts
    2,814
    Thanks given
    1,806
    Thanks received
    276
    Rep Power
    2128
    Quote Originally Posted by Turkleton View Post
    It stands for hypertext preprocessor and it's ugly.
    It stood for "Personal Home Page" but actually it's a reverse acronym now and he's right... But I agree, the new name is ugly as hell...
    [Only registered and activated users can see links. ]
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

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

Similar Threads

  1. [PHP]Request your tutorials here[PHP]
    By r-servers in forum Tutorials
    Replies: 4
    Last Post: 10-11-2011, 12:56 AM
  2. Replies: 20
    Last Post: 08-14-2010, 04:56 PM
  3. [PHP] Server Status Signature [PHP]
    By Naab in forum Website Development
    Replies: 21
    Last Post: 10-22-2009, 06:54 PM
  4. Replies: 0
    Last Post: 04-21-2008, 07:13 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •