Welcome to WebDesignForums.net!
You're currently viewing WDF as a guest. By registering for a free account, you'll be able to participate with other members in our friendly community. Being a member allows you to ask questions and get answers for those troublesome web development tasks!

In addition, as a member you'll be able to post your websites up for review. Using our unique website review system you can gain some amazing feedback from some of the best web developers around. This is a completely free service to all registered members.

Ready to register yet? Registration is 100% free. Click Here To Join Now!

PHP basics

Discussion in 'Coding Articles & Tutorials' started by infiniti, Apr 24, 2006.

  1. Offline

    infiniti New Member

    Message Count:
    146
    Likes Received:
    1
    Trophy Points:
    0
    I made this basic tutorial for my soon to be tutorial and portfolio site. I am posting it here for two reasons:
    1) To teach the basics of PHP.
    2) To have other web developers add to it.


    Here it is:
    ----------------
    To start a php block of code use this
    -------------
    [php<?php
    insert code here
    ?>[/php]


    To write a phrase:
    ---------------------
    PHP:
    <?php
    echo "your phrase here";
    print(
    "this also works")
    ?>
    In php variables start with "$" minus the quotations. To assign a variable data use the "=" sign. An example would be:
    ------------------------
    PHP:
    <?php
    $name
    ="John";
    ?>
    Addtion (+), Subtraction (-), Multiplication (*), Division(/) opperators look just like that. Other opperators include "&&" for and, || for or, == for equal to. Here is how you use them:
    ------------------------
    PHP:
    <?php
    $num1
    ="1";
    $num2="2";
    echo 
    "$num1 + $num2";
    echo 
    "$num1 / $num2";
    ?>
    Now for those other operators, you use them in if/else statements:
    ----------------------------
    PHP:
    <?php
    if (statement) {
    what happens if it is true
    }
    else{
    what happens if it isnt
    }
    ?>
    Here it is in use
    -------------------------
    PHP:
    <?php
    if (5==5){
    echo 
    "yay";
    }
    else{
    echo 
    "what?"
    }
    Loops in php can be used for various things, here is the basic setup for a for loop.
    --------------------------
    PHP:
    for(initial value;comparison;increment){
    what happens
    }
    An example is this:
    ------------------------------
    PHP:
    for($views_on_page=0;$views_on_page>10;$views_on_page+1){
    echo 
    "we need more members";
    }
    What that did is set the variable $views_on_page equal to zero then it setup a loop that for every time the variable was not greater than 10 it printed we need more members. After it did this it added 1 to the variable.


    Functions are units that combine code. Here is the basic setup:
    -------------------------
    PHP:
    function function_name (statementstatement2){
    coding;
    }
    function_name()
    Here it is in use:
    --------------------------
    PHP:

    function add_numbers($num1,$num2){
    $num1 $num2 $answer;
    echo 
    "$answer";
    }
    add_numbers(1,5)
    What the first line did was create a function called "add_numbers" with the statements "num1" and "num2". On the second line it says that num1 plus num2 equal answer. Then it prints the answer. To call or use the function you use the last line of code. In the parenthesis it give the value of num1 number 1 and num2 number 5. Then it will print the number 6.


    Getting Data from a user is easy but you need to know everything above and some html. So start off a form in html as follows:
    ---------------------------------
    HTML:
    <form name="f1" action="math.php" method="POST">
    <input type="text" name="value_one" value="number1"><br />
    <input type="text" name="value_two" value="number1"><br />
    <input type="radio" name="operation" value="add"><br />
    <input type="radio" name="operation" value="subtract"><br />
    </form>
    What that did is make a form with two text boxes and two radio buttons. One says add and the other subtract.

    Now for the php file called math.php:
    ------------------------------------
    PHP:
    <?php
    if ($_POST[operation] == "add"){
    $answer $_POST[value_one] + $_POST[value_two];
    }
    else{
    $answer $_POST[value_one] - $_POST[value_two];
    }
    echo 
    "$answer";
    ?>
    $_POST is a way of getting the data from the other file. In the second line it is seeing if the radio button set called "operation" was equal to add. In the third line it is setting the variable answer to value_one plus value_two from the other forum. Otherwise it is subtracting the values. In the second last line it is printing the answer.



    Good Job....now you know basic PHP


    Alanna Baxter likes this.
  2. Offline

    bfsog Coder

    Message Count:
    2,354
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Good, however any type of number in PHP (whole number, double) do not need quotes, such as your second code block.

    PHP:

    <?php
    $num1
    ="1";
    $num2="2";
    echo 
    "$num1 + $num2";
    echo 
    "$num1 / $num2";
    ?>
    And also you only need quotes when your displaying more than just a variable, so
    PHP:

    <?php
    echo "$answer";
    ?>
    does not need the quotes.

    Good intro.


  3. Offline

    infiniti New Member

    Message Count:
    146
    Likes Received:
    1
    Trophy Points:
    0
    Sorry but there is a typo, in the second last code block the second + should be a - . sorry for that


  4. Offline

    Wired WDF Moderator and Alien Overlord

    Message Count:
    7,600
    Likes Received:
    133
    Trophy Points:
    63
    Edit Your Post.


  5. Offline

    infiniti New Member

    Message Count:
    146
    Likes Received:
    1
    Trophy Points:
    0
    lol, i forgot i could do that


  6. Offline

    merlin New Member

    Message Count:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I am new to php sort off and value any tips and tricks


Share This Page