Technology Programming

PHP Training Tutorial

    Getting Started with PHP

    • As a server-side language, PHP only actually works when running on an active server. To create an environment that runs PHP correctly in order to use it yourself, you'll need to install a private home server. The open-source package XAMPP provides all the components you need to do so (one of the P's in there stands for PHP).

      A PHP file is essentially an HTML file with snippets of PHP include. You can write HTML code as normal, but whenever you write any PHP code, begin with "<?php" and end with "?>". You can also plant HTML code inside PHP code using the "print" or "echo" commands. Here's a sample PHP document:

      <html>
      <head>
      <title>My First PHP Program</title>
      </head>
      <body>
      <?php
      print "<h1>Hello, World!</h1>";
      ?>
      </body>
      </html>

      Note that "print" doesn't mean "print to the computer screen" -- it means "print to the HTML document." The web browser, in turn, will interpret the HTML document (in this example, converting "<h1>Hello, World!</h1>" into a heading). Also note that each line in PHP ends with a semicolon; if you don't include one at the end of each line, the program won't work.

    Variables and Arrays

    • Variables are a way to store data. Variables start with a "$" sign and can be named whatever you wish. The following declares a variable and then prints it out again.

      $language = "PHP";
      print "My favorite server-side language is $language";

      Of course, you wouldn't need to create the $language variable in this case; you could just write "My favorite server-side language is PHP." But what if you had a form on your website that asked for a user's favorite server-side language? You could store their answer in the $language variable for use later. It's that kind of situation where variables can really come into play.

      Arrays are essentially variables that can store multiple values. Here's how you declare an array:

      $bookshelf = array("Macbeth", "Hamlet", "Julius Caesar");

      Each value in the array is then automatically stored in an array "key":

      $bookshelf[0] = "Macbeth";
      $bookshelf[1] = "Hamlet";
      $bookshelf[2] = "Julius Caesar";

      There are many different types of arrays, such as "associative arrays," which can have non-numerical keys:

      $info = array();
      $info["name"] = "Bill Gates";
      $info["company"] = "Microsoft";

    If/Else and Loops

    • If/else conditional statements allow your code to make decisions. For example, let's say you have an option in a member directory for whether a member's email address will be shown or not. You've stored the user's answer to the question in the $displayEmail variable:

      if ($displayEmail == "yes"){
      print "johndoe@example.com";
      } else {
      print "Email hidden by request.";
      }

      Note that the double equal signs check for equality, because a single equal sign would mean you're assigning a value to a variable.

      Loops, on the other hand, repeat an action until a certain condition is met. PHP contains a few different loop functions, such as the "while" loop:

      $number = 1;
      while($number < 11){
      print $number;
      $number++;
      }

      Once PHP gets to the closing bracket, it jumps back up to the opening bracket again until "$number < 11" isn't true--so that snippet of code would print "12345678910". The line "$number++;" is shorthand for saying "$number = $number + 1;" and is an easy way to auto-increment your variables.

    Further Training

    • Obviously, this only scratches the surface of PHP's possibilities. While all the rules and syntax can be daunting at first, the best way to learn the language is to look at a lot of code and write a lot of code. With that in mind, work through the PHP tutorials at instructional sites such as W3 Schools. Also check out MySQL, a database system that can work closely with PHP to create dynamic sites and comes packaged with XAMPP. For examples of PHP and MySQL at work, examine how website management programs such as Joomla create websites by using MySQL databases and PHP template files.

SHARE
RELATED POSTS on "Technology"
Blitzerwarner: Could You Escape the Speed Cameras?
Blitzerwarner: Could You Escape the Speed Cameras?
What Makes a Perfect Web Design Company?
What Makes a Perfect Web Design Company?
Inside No-Fuss Products In Gaming Computers
Inside No-Fuss Products In Gaming Computers
Gaming Institute, Game Design Schools at PAI-ILS, PAI International Learning Solutions Pune
Gaming Institute, Game Design Schools at PAI-ILS, PAI International Learning Solutions Pune
Free Music Players For Websites
Free Music Players For Websites
'See How NCP Tracks Down Fake Ration Card Holders'
'See How NCP Tracks Down Fake Ration Card Holders'
You Can Stop Panic Attacks in Just two Minutes
You Can Stop Panic Attacks in Just two Minutes
WordPress Blog to WP Site! A Journey to Online Success
WordPress Blog to WP Site! A Journey to Online Success
Videos will give your business portal an extra leverage
Videos will give your business portal an extra leverage
Excellent Ways To Make Weight Loss Work For You
Excellent Ways To Make Weight Loss Work For You
Five Notable Differences between National and International Logo Designs
Five Notable Differences between National and International Logo Designs
Who Should Your Phoenix Web Design Client Be?
Who Should Your Phoenix Web Design Client Be?
White Papers & Other Documents - including ALFLB
White Papers & Other Documents - including ALFLB
Google Analytics Tips
Google Analytics Tips
Want To Make Your Website Faster? Follow These Steps
Want To Make Your Website Faster? Follow These Steps
The Starting of Couture Apparel
The Starting of Couture Apparel
Choose Custom Website Design Services
Choose Custom Website Design Services
Choosing the Right Joomla CMS Developer
Choosing the Right Joomla CMS Developer
A Look at SQL Server Denali AlwaysOn
A Look at SQL Server Denali AlwaysOn
4 Tips to Choose the Perfect Flash Animation Course
4 Tips to Choose the Perfect Flash Animation Course

Leave Your Reply

*