Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6

Thread: PHP basics

  1. #1
    Senior Member
    Join Date
    Apr 2006
    Posts
    146
    Member #
    13110
    Liked
    1 times
    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 Code:
    <?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 Code:
    <?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 Code:
    <?php
    $num1
    ="1";
    $num2="2";
    echo 
    "$num1 + $num2";
    echo 
    "$num1 / $num2";
    ?>
    Now for those other operators, you use them in if/else statements:
    ----------------------------
    PHP Code:
    <?php
    if (statement) {
    what happens if it is true
    }
    else{
    what happens if it isnt
    }
    ?>
    Here it is in use
    -------------------------
    PHP Code:
    <?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 Code:
    for(initial value;comparison;increment){
    what happens

    An example is this:
    ------------------------------
    PHP Code:
    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 Code:
    function function_name (statementstatement2){
    coding;
    }
    function_name() 
    Here it is in use:
    --------------------------
    PHP Code:
    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 Code:
    <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 Code:
    <?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.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Good, however any type of number in PHP (whole number, double) do not need quotes, such as your second code block.

    PHP Code:
    <?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 Code:
    <?php
    echo "$answer";
    ?>
    does not need the quotes.

    Good intro.

  4. #3
    Senior Member
    Join Date
    Apr 2006
    Posts
    146
    Member #
    13110
    Liked
    1 times
    Sorry but there is a typo, in the second last code block the second + should be a - . sorry for that

  5. #4
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    Edit Your Post.
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  6. #5
    Senior Member
    Join Date
    Apr 2006
    Posts
    146
    Member #
    13110
    Liked
    1 times
    lol, i forgot i could do that

  7. #6
    Junior Member
    Join Date
    Dec 2007
    Posts
    6
    Member #
    16184
    I am new to php sort off and value any tips and tricks


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:21 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com