+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 25

Thread: [PHP] PHP For Starters

  1. #1
    x10Hosting Member Complex is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    9

    [PHP] PHP For Starters

    PHP: For Starters
    This tutorial assumes that you have a basic knowledge of programming. No PHP Knowledge required.

    I. Basic Syntax

    All PHP Scripts start with one of 3 things: <?php or <? or <%
    and End with one of 2 things: ?> or %>
    NOTE: <% and %> only works if ASP tags are enabled in the server's php.ini! <?php is recommended over all.

    Example:

    PHP Code:
    <?php
    //PHP!
    ?>
    PHP Code:
    <?
    //PHP!
    ?>
    PHP Code:
    <%
    //PHP! Deprecated
    %> 
    You may have noticed the text prefixed with "//". This is a comment in php. Comments are ignored by the compiler and are used to help the developer understand the code. Comments in PHP can follow 3 things:

    //comment - A normal one line comment
    # Comment - Another one line comment
    /* COMMENT!
    YEP */ - A multiline comment

    Example:

    PHP Code:
    <?php
    //Comment 1
    #Comment 2
    /* This is a
    multiline
    comment. All this is
    ignored
    */
    ?>
    IMPORTANT! - All lines of code in PHP MUST END WITH a semicolon. You'll see more examples of this later. The only things that do NOT end with a semicolor are the opening and closing tags and comments

    II. Variables

    A variable in PHP is simple. It starts with a dollar sign ($) followed by a lowercase or uppercase letter or an underscore ($V or $v or $_v) and can contain an underscore and numbers. Here are some exampes:

    $var - GOOD
    $var2 - GOOD
    $v_ar - GOOD
    $2var - BAD
    $hÜh - GOOD

    Also, variable names are case sensitive so:

    PHP Code:
    $var "Hi!";
    $Var "Bye!"
    You would think that "Bye!" is now in $var, but its not. $var is still "Hi!" since $Var has a capital V, so its different. Be careful when writing a script about this. A good remedy is to not use similar names for variables and make it all lowercase.

    NOTE: One of the wonders of PHP is that you dont have to worry about types. A variable can be an int, string, bool, array and more without you having to define it as one.

    III. Math

    Math in PHP is just like in other languages:

    + Add
    - Subtract
    * Multiply
    / Divide
    % Modulus

    Very very simple. Examples:

    PHP Code:
    <?php
    $addexample 
    4//Add. Sum = 9
    $subtract $addexample 4//$subtract = 5
    $multiply $subtract //$multiply = 10
    $divide $multiply //$divide = 5
    $mod $divide //$mod = 1
    ?>
    Simple huh?

    Functions

    Functions are again, very easy, in PHP. Here is the syntax of one:

    PHP Code:
    function funcName($params $defaultVal) {
    //Code

    Lets start from the beginning on this one. Before defining any function, you must put "function " in front of it to show that it is one. Next comes the name of the function, you will use this name to call it in your code later. After that comes parenthesis and inside you can optionally put parameters. If after a param you put "= value" where value can be anything from a number to a string to a boolean value, this will be the default value if none is supplied. Otherwise if there is no default value and the function is called and no value is given for that parameter, PHP gives a fatal error. Here is a working example using a function:

    PHP Code:
    <?php
    //Pythagoreus theorem:
    function thmPythag($a$b) {
        return 
    sqrt($a^$b^2);
    }

    $num1 3;
    $num2 4;

    $hypot thmPythag($num1$num2); //$hypot = 5
    ?>
    In the above example, 2 numbers are given to the function thmPythag which uses the Pythagoreus theorem to give the length of the hypotenuse of a right triangle. You may notice a couple things you havent learned yet:

    return and sqrt.

    Sqrt is just a built in function in PHP to find the square root of a float.
    Return is used in a function to return a value! In VB its like this:

    Code:
    Function myFunc() as Integer
    myFunc = 5
    End Function
    In PHP:

    PHP Code:
    function myFunc() {
    return 
    5;

    IV. Scope

    Have you ever heard of the term Scope? Local Scope? Global Scope? Well if not, I am going to explain them now. Whenever you init a variable, it is put into LOCAL scope. This means that it can only be used in that scope. A scope is defined using brackets {}. Example:

    PHP Code:
    <?php
    $var 
    "hi!";

    function 
    scope() {
    $myvar $var;
    return 
    $myvar;
    }
    ?>
    If scope() was called, the return value would be blank. Why? Because $var was initialized outside of scope()'s scope. To use $var, you would have to put it into GLOBAL scope like so:

    PHP Code:
    <?php
    $var 
    "hi!";

    function 
    scope() {
    global 
    $var;
    $myvar $var;
    return 
    $myvar;
    }
    ?>
    Now if you called scope(), it would return "hi!" That is a very basic example of scope, I may make a more detailed and advanced scope tutorial somewhere down the road, but for your sake, the above will do, for now.

    V. Control Structures
    i. If Structure

    An if structure will compare an expression and if it results in TRUE, it'll execute the code following it, otherwise, it'll skip it. Syntax:

    PHP Code:
    if (expression)
        
    code 
    This is good for one line of code, if you need your code to span multiply lines after an if, use brackets:

    PHP Code:
    if (expression) {
        
    code
        morecode

    You can use many operators in the expression area of an if statement:

    > - Greater than
    < - Less than
    >= - Greater than or equal to
    <= - Less than or equal to
    == - equal to
    != - not equal to
    ! - not
    && - And
    || - OR
    ^ - XOR

    NOTE: In PHP, = is not the same as ==. What = tells PHP is "Set the value of the right side to the left side" and == tells PHP "Compare the value of the left side with the right side, if they are the same, return TRUE, else, return FALSE"

    Here are examples of each operator:

    PHP Code:
    <?php
    $num1 
    5;
    $num2 10;

    if (
    $num1 $num2)
        
    //Greater

    if ($num1 $num2)
        
    //Less than

    if ($num1 >= $num2)
       
    //Greater than or equal to

    if ($num1 <= $num2)
       
    //Less than or equal to

    if ($num1 == $num2)
      
    //Both the same

    if ($num1 != $num2)
      
    //Not the same

    if (!$num1) {
      
    //$num1 = True (anything not 0)

    if ($num1 && $num2) {
      
    //Both are true

    if (($num1 $num2) || ($num1 == $num2))
      
    //Greater than or equal to (not using >=)

    ?>

  2. #2
    x10 Elder Maurice is an unknown quantity at this point Maurice's Avatar
    Join Date
    Feb 2005
    Location
    The Netherlands
    Posts
    965

    Re: PHP: Starters

    Quote Originally Posted by Auvee
    Does this look familiar?

    Anyway, good tutorial...

    -Auvee B.
    Same thing goes for your FireFox thing :blink:

  3. #3
    x10Hosting Member Complex is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    9

    Re: PHP: Starters

    Yes, a friend of mine wrote this.

  4. #4
    x10 Elder Maurice is an unknown quantity at this point Maurice's Avatar
    Join Date
    Feb 2005
    Location
    The Netherlands
    Posts
    965

    Re: PHP: Starters

    Quote Originally Posted by Auvee
    True, but I won't say anything bad, as I'm sure this will help out many people who are new to PHP.

    -Auvee B.
    Ofcourse it will, I think every PHP coder (New) has too start with this. :homestar:

  5. #5
    x10Hosting Member Conquester777 is an unknown quantity at this point Conquester777's Avatar
    Join Date
    Mar 2005
    Location
    Vancouver, BC, Canada
    Posts
    90

    Re: PHP: Starters

    yeah really, if you're gonna copy from some other place; even if it's good. give credit, you've already copy+pasted all they wrote, can't you copy+paste the url too?

  6. #6
    x10Hosting Member wonkothesane is an unknown quantity at this point wonkothesane's Avatar
    Join Date
    Jul 2005
    Location
    Here and there, travel a lot.
    Posts
    42

    Re: [PHP] PHP For Starters

    Hey, I liked it. Very basics and good for people starting out with php (me, lol). Reminds me a lot of java (only other language i know... a little). Just one question, can you use php modularly like you can java? thnx

  7. #7
    x10Hosting Member FerretFreak is an unknown quantity at this point FerretFreak's Avatar
    Join Date
    Jul 2005
    Location
    MN
    Posts
    14

    Re: [PHP] PHP For Starters

    That was incredibly helpful ^^

    Nice job.

    &nbsp;Me's a pirate! Marharhar! :chris:

  8. #8
    x10 Spammer MicrotechXP is an unknown quantity at this point MicrotechXP's Avatar
    Join Date
    Feb 2005
    Location
    USA,California
    Posts
    3,818

    Re: [PHP] PHP For Starters

    Very Helpfull. I am going to start reading now becasue I wanted to learn php.

  9. #9
    x10 Sophmore Burak is an unknown quantity at this point
    Join Date
    Jul 2005
    Posts
    129

    Re: [PHP] PHP For Starters

    Too resourceful, thanx for sharing

  10. #10
    x10Hosting Member pennasb is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    13

    Re: [PHP] PHP For Starters

    Thanks for that. That is a good way to start.

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 40
    Last Post: 03-05-2010, 02:08 PM
  2. [PHP] PHP and interacting with HTML forms
    By Bryon in forum Tutorials
    Replies: 12
    Last Post: 11-06-2009, 06:09 AM
  3. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  4. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  5. [PHP] Placing the Ads in PHP Pages
    By Chris in forum Tutorials
    Replies: 22
    Last Post: 06-04-2005, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts