basix

Learn PHP from Scratch: A Training Regimen

Tutorial Details
  • Difficulty: Beginner
  • Completion Time: 1 Hour

Holding the title of “number one”, PHP is the most popular language among developers. Even still, many prefer different languages. Yours truly, for example, is most comfortable when working in the ASP.NET environment. However, because of the enormous success of WordPress, more and more developers have decided to expand their horizons and learn yet another language.

I happen to be one of those very people. As my clients increasingly ask for WordPress implementation, learning PHP has become a requirement. I’m not alone in this endeavor. For those in the same boat, why not take the time and learn with me?

The Mission Statement

Over the course of the next few articles – to be posted each Wednesday – it is my intention to create a “work-out regimen” for all of us. If you’ve been meaning to learn, but haven’t gotten around to it yet, now is the time! On the flip side, for those of you who are PHP ninjas, I respectfully request that you get involved and offer advice to the rest of us. If you’ve benefited from the dozens of tutorials on this site, take a few moments to give back, via the comments section. This will be your one-stop resource for everything PHP. Each Wednesday, I’ll post a training article, as well as a list of resources that will help to explain the concepts in the lesson further. The key here is that I’m a beginner just like everyone else, relatively speaking. We can motivate one another to learn as quickly, and efficiently as possible.

So, why would you learn from a beginner? Try not to think of it as me teaching you. Think of these articles as a community effort where we all help each other. I’ll be learning from many of you in the same way that you learn from me.

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. I was pleasantly surprised as I began my training. 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

Yes, I’m sorry to say that there are a few more 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

Video Training

Lynda.com

Our first stop will be at Lynda.com. Maybe more than any other resource, Lynda.com has provided me with a wealth of knowledge that I’ll forever be grateful for. For the price of a couple of pizzas, you’ll gain access to a video database that details everything from ASP to SEO – and every acronym in between. When a client requests that I use a piece of software that I’m not completely comfortable with, my first stop is Lynda.com. If you’re still unconvinced, why not do a google search for “Lynda.com free trial”. I guarantee that you’ll come up with something. Just make sure that, if you are more than satisfied with their offerings, you sign up.

After you’ve signed up for an account, or the free trial, go to the site and under the “Subject” drop-down-list, scroll to PHP. For this lesson, we’ll be focusing on the “PHP with MySQL Essential Training” videos. Try to watch the first three chapters this week. That will get you fit and primed for next week’s lesson.

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. Refer back to your code and insert the following:

<?php echo "This is PHP in action"; ?>

Notice that in this second example, we kept everything on one line. 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;
?>

This example will produce the exact same result as the previous two. However, in this scenario, we’ve assigned the string to the variable and then “echoed” the variable instead. Now what if I wanted to concatenate a variable and a string?

<?php $myVariable =  "This is PHP in action.";
  echo $myVariable . " My name is Jeffrey Way";
?>

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 CSS */
?>

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.

That should be enough for this week. If these concepts are still vague, go back and read the article again. 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 Part 2 – which will come out next Wednesday. If you enjoyed this article, please submit it to your favorite social networking site!

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.

    Visit Article

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Pingback: WebTeam » Blog Archive » PHP Tutorials

  • http://www.honourchick.com honour chick

    thanks alot… this help alot because of the easy explanations.

  • Pingback: KWM-Inside » PHP Links

  • Pingback: Bookmarks for December 15th from 10:34 to 10:34

  • http://www.wplancer.com Bnago

    You have forgotten XAMPP :) – It is a great tool, I have used it under XP, Vista and Ubuntu. Now I have switched to Ubuntu completely so I only use it there. It is a great way to develop WordPress theme locally using XAMPP.

  • Pingback: The Best Tutorials and Freebies of 2008 - NETTUTS

  • http://www.phpvideotutorials.com Brian

    It is worth adding PHPVIDEOTUTORIALS.com to that list.

  • john
  • Pingback: PHP Linkliste « H79.de

  • http://www.christallization.com Chris

    This is a very good tutorial for beginners, good job mate.

    Cheers…

  • Mutombo

    Yea! I believe that its all good! Believe i want to share in your pride and joy, but please help me. i have a project in PHP, at least till i get to learn it, and the problem is that i cant get any program to run. not even the “Hello world!” thing.
    I already have the wamp server thing, just installed it, dont know what to do with it. But i still get no results with my pages. How do i configure my Dreamweaver or any other web develpoer ide with the php environment.
    Please Please Please help me. Ya! I’m Desperate deal with it. thank you!

  • jbug

    Jeff, great tut.. and killer resources!

    However, I think you give WAY too much credit to WP.. Do you honesty think it was the popularity of WP that spawned the mass interest in PHP ? Or could it have been the multitude of Open Source apps all built using this awesome language? I know everyone at Envato loves their WP, and for good reason.. but although it may be a focus of development for PHP, it’s success is not the ‘reason’ for more developers flocking to this language.

  • http://www.sql-injection-tols.blogspot.com sHAFIQ-uR-rEHMAN

    Wow, what Great Tutorial, its really good for those who just start learning PHP! this tutorial will almost lear all the Basic concept about Variables+ FUCNTIONS….

  • vamsi

    very basic :P
    but highly useful for beginers :)

  • Pingback: Learning PHP from scratch -part 1 - Tutorials

  • Pingback: Easy PHP I: Introduction » WordPress Freelancer

  • Pingback: Easy PHP II: Variables » WordPress Freelancer

  • http://www.image4tomorrow.blogspot.com albartoss

    this article is very short cut. if a beginners have some knowledge of php, then he will understand it, other wise not.

  • HeatoN

    Very basic , but still it will help beginners.

  • Pingback: Jeffrey Way

  • Young

    Good post!

  • http://robertwojo.com Robert Wojciechowski

    Great resource for beginner web developers!

  • Kniggles

    This looks good, I am working up to become a novice and would like to ask, WHERE is part 2 and the rest please?
    also, do I have to have .php installed on my laptop or on the server where my web site is hosted ?
    I am using the free 000webhost and I see .php and mysql in the cpannel on there. I would like to start simple by adding users,password and a text box for other text info that can then be displayed somewhere else on the site.
    thanks.

  • http://www.otreva.com Otreva

    I read this a few weeks ago and took your advice to visit Lynda.com for tutorials. Although this is pretty basic, I must say it was a great start and Lynda has really helped me understand PHP much better.

    Good beginner tutorial.

  • http://www.gamezeed.com game

    Thanks to the Good article.

  • Rodell

    Hay i have been working through this tutorial and it has been a great help so far and i would love to carry on and do some of the next ones. However i cant find a link to part 2?
    Can anyone direct me to it?

  • http://blog.twostepmedia.co.uk Ben Howdle

    Hi, really good tutorial, i think these beginner articles are very important for learning and i certainly learnt alot from Nettuts alone – check out http://blog.twostepmedia.co.uk/php-user-defined-functions-quick-tutorial/ for User Defined PHP Functions Tutorial – be gentle, i’ve only just started writing tutorials!

  • http://www.awalrabin.com.np/ Rabin Awal WEB Development

    will be Very understandable tutorial on php for beginners. the explanation you presented are just what a newbie on Php programming need. keep the Good work on continuous spinning wheel..

  • Lacey

    So what happened to part 2? …

  • Joseph
  • http://netwebsite.in/articles Rahul

    thanks good articles very nice very very help full :) :)

  • http://reygcalantaol.com Rey

    I am learning PHP and already started a projects with it. But I am I want to learn classes in PHP I am quite poor on it.

  • Pingback: Learn PHP from Scratch: A Training Regimen | cloudsourceme.com/blog

  • Pingback: 30+ PHP Tutorials For Beginners | Technical

  • Spandana

    Thanks it is Very Helpful for Beginers like me..:)

  • http://www.aphrodite1994.com mark

    Just the thing I’ve been looking for coupled with some links to other sites too.

  • Taha M. Asadullah

    Hi Jeffery,
    Thanks a lot for the basic tutorial.
    But, I dont expect referring to other sites/tutorials is a way to teach. Instead, when you label it as From Scratch, I expect to learn all the things from this tutorial itself.
    Greetings from India.

  • Pingback: 5 +1 enlaces útiles para el licenciado que quiere aprender a programar php | hkadejo

  • Blacked

    Nice tut ;)

  • http://www.psdbook.net lee

    Thank you very much for your tutorial. tutorials on tuts plus are the best ones i have read. i want to learn php and mysql.

  • http://somesa.org Ntambazi Michael

    Hey guys, I am Michael a computer engineering student. I really need a coding help from anyone about retrieving data from a database dynamically with a time interval. Inorder to understand what i am talking about, please visit http://www.ntvuganda.co.ug/ and look below the NTV logo at the Latest news .

    Thank you very much.

  • Pingback: codendesigns » Blog Archive » Learn PHP from Scratch – Part 2

  • http://www.foulfreekick.com Ouch

    Nice article – I’m just starting out and have found your links quite useful.

  • Franky Mapesa

    i like the article can help me

  • http://www.learning-html.org/ html

    I found this first tutorial incredibly useful. It really introduces you so well to the PHP world..

  • http://danrichards.net Dan Richards

    FYI, all the Required Resources are broken links now.

  • Anup

    Nice article specially for beginners. Basic explanation is fab. :)

  • raJ RANJAN

    what is this this is called basic of php …..very sad and unworthy article

  • amrut ratnani

    this nice n i understood easily..

  • Pingback: WordPress Freelancer - Easy PHP I: Introduction