19 May 2012

Learn PHP

PHP is a general-purpose widely-used server-side scripting language originally designed for web development to produce dynamic web pages. It is among one of the first developed server-side scripting languages that is embedded into a HTML source document, rather than calling an external file to process data.PHP was originally created by Rasmus Lerdorf in 1995.

What Is PHP?

PHP stands for Hypertext Preprocessor. While other languages, like Javascript, function on the client-side, your PHP code will execute on the server level. It works seamlessly with our HTML. Furthermore, your PHP can be embedded within your HTML and vice versa. The important thing to remember is that, no matter how complicated your PHP is, it will ultimately be output as simple HTML.

Why Would I Use PHP?

HTML is 100% static. By implementing PHP into your code, we can create dynamic sites that will change dependent upon specified conditions. With a community base second to none, this open-source language has proven itself over the years to be one of the best options for dynamic web applications.

Is PHP Similar To Any Other Languages?

Absolutely.  If you have even a modest amount of knowledge when it comes to ASP.NET, Perl, Javascript, or C#, you’ll find that you pick up the syntax quickly.

What Do I Need To Get Started?

You must have the following installed on your computer in order to begin.
  • Apache
  • MySQL
  • Web Browser
  • Text Editor
  • PHP

WAMP, MAMP

There is few acronyms to learn. “WAMP” stands for “Windows-Apache-MySQL-PHP”. It is an open source project that will allow us to download everything that we need to get started right away. If you’re a Windows user, visit WampServer.com. On the other hand, if you’re using a Mac (MAMP), you’ll want to pay a visit to Mamp.info

The Basics

In order to alert the server that we are working with PHP, you’ll need to use the following syntax when adding PHP into your HTML documents:

<?php
     ...code goes here
?>
We start and end each PHP declaration with “<?php” and “?>”, respectively.

<?php
     echo "This is PHP in action";
?>
Remember, PHP is not white-space sensitive. Here, we are telling the server to “echo”, or write “This is PHP in action” onto the page. Each declaration in our code must have a semicolon appended to the end. Although HTML can be forgiving if you accidentally forget a bracket, PHP unfortunately is not. If you don’t use the correct syntax, you’ll receive an error. In this case, when we only have a single declaration, we could technically get away with leaving the semicolon off. But, it’s always important to follow best practices.

Defining Variables

We can assign values to variables quite easily. Rather than using “var” (C# and Javascript), or “dim” (VB), we can declare a variable in PHP by using the “$” symbol. For instance, let’s say that I want to assign the previous string to a variable called “myVariable”. I would write…

<?php 
      $myVariable "This is PHP in action";
      echo $myVariable;
?>
By using the period, we can combine variables and/or strings.

Inserting Comments Into Your Code

If you’re familiar with CSS and Javascript, you’ll find that inserting comments in PHP is virtually the same.

<?php 
     # This is a single line comment.
    // This is the most common way of commenting out your code.
    /* Here is a way to comment over multiple lines. This is the exact same way
       that you would comment in */
?>

Combining HTML With Our PHP

As said previously, remember that PHP and HTML can work in combination. Simply because we’re in the middle of a PHP statement does not mean that we can’t embed elements such as a break or strong tag.

<?php  
     echo "<strong>This text is bold.</strong>";
?>

Defining Your First Function()

Creating functions in PHP is nearly identical to Javascript’s implementation. The basic syntax is…

<?php
     function name ($arguments){
          your statement goes here;
     }
?>
If we wanted to create a function that “echos” 10 plus 5, we could write…

<?php
     function addNumbers (){
           echo 10 + 5;
     }
     addNumbers();
?>
We’re creating a simple function that will output “15″. We call the function with “addNumbers(). In this case, we aren’t using any arguments. Let’s see how we can implement them in order to make our function more versatile.
<?php
function addNumbers($firstNumber, $secondNumber){
           echo $firstNumber + $secondNumber;
}
addNumbers(10, 5);
?>
Now, our code is much more flexible. When we created our “addNumbers()” function, we added two arguments – $firstNumber and $secondNumber. The function will simply echo the sum of these two variables. When the function is called, we’ll need to pass in our two numbers – addNumbers(10, 5). In a real-world situation, the values for these variables might be taken from a couple of textboxes.
If these concepts are still vague, go back and read the article oncemore. Also, be sure to check out the following resources which will help you to further grasp the syntax of PHP. Please feel free to ask questions or offer advice in the comment section. I’ll be sure to work your thoughts into second step

Required Resources

  • Lynda.com

    Lynda.com : PHP With MySQL Essential Training

    Website and database assimilation is a necessity for many of today’s businesses, and learning to work with PHP is key to integration success. The objective of PHP with MySQL Essential Training is to teach both new and experienced web developers the comprehensive steps for building dynamic, data-driven, interactive websites.
    Visit Article
  • Developer's Zone

    PHP 101 : Down The Rabbit Hole

    To put together a cutting-edge Web site, chock full of all the latest bells and whistles, there’s only one acronym you really need to know: PHP.
    Visit Article
  • PHP Buddy

    PHPBuddy.com : Your First PHP Code

    This site covers the basic PHP syntax, including variable usage.
    Visit Article
  • PHP.NET

    PHP.NET : A Simple Tutorial

    Here we would like to show the very basics of PHP in a short, simple tutorial.
    Visit Article
  • W3 Schools

    W3Schools.com : PHP Tutorial

    This site will give you as detailed an introduction to PHP as you could ever hope for. Be sure to spend at least an hour or so here.

No comments:

Post a Comment