• home
  • forum
  • my
  • kt
  • download
  • PHP Basics

    Author: 2009-04-17 11:26:01 From:

    Heres a quick intro to PHP. A lot will be left out, I’m assuming you know basic programming (variables and loops). W3 Schools is a great place to learn PHP more in depth.

    To try this code make sure you have XAMPP installed, check out this tutorial if you haven’t set it up yet.

    Every script should begin and end with the php open/close tags.

     
    <?php 
    //Comments
    //rest of code here
    ?>

    Here are some examples

     
    <?php
    // two backslashes are used for comments
    // A variable is created using the dollar sign before the name of the var
    $v1 = "bananas" ; //semicolon after each statement
     
    //echo is used to print to the screen
    echo $v1;
     
    //You can print multiple items using a comma
    $v2 = "apples" ;
    echo $v1, $v2;
     
    //If statements are similar to those in C++ and Java.
    $uno = 1 ;
    $dos = 2 ;
     
    if($uno < $dos)
    {
      echo "WOO!" ;
    }
    else
    {
      echo "WTF!?";
    }
     
    ?>


    The open/close tags make it easier to mix html in a php page.
    If you were to echo your html out you would have to escape quotes like this (quite a pain)

     
    <?php 
    echo "<html>n" ;
    echo "<a href="http://www.site.com">my site</a>" ;
    echo "<input type="text" value="hello" maxlength="6">" ;
    ?>

    The easier way to do it is to just close the php tag, type your html and open the php tag again. An example would be a php if statement.

     
    <?php
    if(!$_POST['submit']) //if the form hasn't been submitted, output the html
    {
    ?> //close tag here and work in html
    <html>
    <form name="myform" method="post">
    <input type="text" name="box">
    <input type="submit" name="submit">
    </form>
    </html>
    <?php //open the tag back up since we're back in php
    }
    else
    {
    //do whatever in php here
    }
    ?>

    Now lets go over loops

    //the usual for loop
    for ($i=1; $i<=10; $i++)
    {
      echo $i, "ha ha ha ";
    }
     
    //heres a foreach loop, used to loop through an array
    $myarray=array("red","green","blue");
    foreach ($myarray as $colors)
    {
      //you can use a "." to splice vars and strings together
      echo "Color: " . $colors . "<br />"; 
    

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (46)
      Cookies and Sessions (12)
      Counters (16)
      Database Related (36)
      Date and Time (15)
      Development (26)
      Discussion Boards (8)
      E Commerce (9)
      Email Systems (15)
      Error Handling (8)
      File Manipulation (38)
      Flash and PHP (6)
      Form Processing (25)
      Guestbooks (13)
      Image Manipulation (26)
      Installing PHP (7)
      Introduction to PHP (31)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (60)
      Networking (9)
      News Publishing (9)
      OOP (28)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (7)
      Postcards (1)
      Randomizing (15)
      Redirection (12)
      Searching (10)
      Security (32)
      Site Navigation (16)
      User Authentication (16)
      WAP and WML (7)
      Web Fetching (10)
      Web Traffic Analysis (15)
      XML and PHP (18)

    New

    Hot