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
  • http://bobpotter.tumblr.com Bob Potter

    Hey thanks for this tutorial. I am really looking forward to reading this series and following along. Thanks for the additional resources at the bottom too.

  • http://www.swanydesign.com swany

    PHP has changed my life for the better! Client email news updates are a thing of the past!

    I hope to learn more from your articles and resources thank you!

    Swany

  • http://juarezpaf.com Juarez P. A. Filho

    Wow… I’m really very excited about this idea. I already know PHP, but is so great discover new things and I’m sure that ideas like that always give me a chance to learn more and more. I learned a lot of things at Lynda.com and it’s high recommendable. Let’s learn or improve skills together guys. CYA

  • Ryan

    Great idea. I certainly could use a training regimen, rather than the sloppy, haphazard way I gather knowledge. I love your site, and since I’m going to be here quite a bit, I’ll also use the time to properly learn PHP. Please keep up the good work. Cheers!

  • http://www.aplaceformyhead.co.uk matt_d_rat

    Excellent tutorial, very easy to follow. I am a semi-beginner at PHP, I self taught myself some basics for a university assignment, but I have always wanted to learn more…so I shall be watching this space very closely for some more tutorials – especially MySQL database interactions – that would be very handy.

  • http://www.codespanish.com Pablo Matamoros

    Great timing! I am actually in the process of builidng a webapp to finally learn PHP.

    Although I understand PHP, I never developed an application from scratch. My knowledge is limited, I can only modify Wordpres, Jommla, …, templates.

    I was going to use the CakePHP framework. Has anybody used it?

    W3Schools.com rocks!

    I recommend the new Netbeans IDE for PHP. It is free!: http://www.netbeans.org/

  • Pingback: meneame.net

  • Darren

    As someone mentioned above, there are differences between using single and double quotation marks for string variables.

    If you use double quotation marks, PHP will do extra parsing looking for stuff like variable names (eg. $foo) and escape sequences (eg. “\n”). If you use single quotation marks, PHP will treat the string exactly as you typed it. Finally, you can use the heredoc curly brace syntax in combination with double quotation marks to explicitly include a variable in your string.

    What I choose to do is:

    1. If I’m not using any variable names or escape sequences, I always use single quotation marks for strings.
    2. If I am using either of the above, I always use double quotation marks with curly braces. It’s not always necessary to use the braces but it makes it very clear in your code which parts of the string are variables names and prevents errors when you’re inserting an aobject variable or running a variable right up against a string fragment which is not part of the variable name.

    For example (this bit might get mangled in the comments):

    $foo = ‘hell’;
    $obj = new stdClass();
    $obj->prop = ‘hell’;
    echo “$fooo”; // error – unknown variable
    echo “{$foo}o” // prints “hello”
    echo “$obj->propo” // error – unknown variable
    echo “{$obj->prop}o” // prints “hello”

    Other people use different approaches but I find this way to be consistent and fool-proof.

    Some more info here:

    http://www.unix.com.ua/orelly/webprog/php/ch04_01.htm

    Cheers,
    Darren.

  • http://www.ahhh-design.com Amanda H

    Just what I’ve been looking for, thanks so much – for this and all your tutorials!

  • dronix

    For those that still don’t know about it, http://phpvideotutorials.com has free php tutorials that everyone can benefit from.

  • http://www.insicdesigns.info insic

    a lot of people love nettuts. this site rocks! lol

  • Nubenegra

    Thanks Great tutorial

  • tripdragon

    ruby on rails

  • http://www.casjam.com CasJam

    EXCELLENT TUTORIAL!

    I’m a front-end designer / developer, but I’m constantly running into situations where I either need to hire out a PHP programmer, or spend hours searching the web for a pre-made solution.

    My current dilemma is I need to create a very, VERY, simple CMS. One form with authentication, that will write and publish data to an HTML table. I haven’t found a solution that is simple enough for both me to set up, and my non-computer savvy client to use. What I need is so simple that I’d like to be able to put it together myself…

  • http://www.detacheddesigns.com Jeffrey Way

    Thanks everybody. I think this series will be a bi-weekly one. I need time to learn enough for level two! Thanks for all of the advice.

  • http://www.mabucplus.com Mark Abucayon

    wow, I like to see this one… very useful for PHP beginners.

  • Rafyta

    So cool that you will be posting one of these a week…
    Just wanted to share with everyone that PHP originally stood for “Personal Home Pages” and then became recursive: “PHP Hypertext Preprocessor”.

    Keep up the coolness…

  • Zach LeBar

    THANK YOU! I’m beginning to learn PHP, and have been searching for this exact stuff. Can’t wait for Wednesdays now. :)

  • Wayne

    Very cool. I’ve hacked away at some PHP code maintenance, just enough to be dangerous. Looking forward to Wed’s!

  • Stephen Coley

    I agree with Mr. Griffiths, XAMPP is a really good solution to the hassle of dling apache, mysql, and php. It has never failed me.

  • http://www.icomworks.co.uk Owz

    I’m just starting out on PHP so this is great too be able to learn along with others..

    Nettuts/psdtuts/vectortuts is by far the best tutorial sites I’ve come across on the web.. keep it up guys…

  • http://callidemedia.com Ibrahim Al-Rajhi

    A great book for learning PHP and MySQL is PHP 6 and MySQL 5 visual quickstart…you can find it on Amazon, I find it easier to learn through a book since everything I need is in one place. (also you only need to pay once instead of a monthly fee for lynda.com)

  • http://solapurnews.blogspot.com paresh

    great for php begginers, thanks for sharing

  • ericb

    Thanks for sharing this noob friendly tutorial! Right timing too since my PHP class will star this Fall. Post some more as you progress on PHP!

  • http://laminbarrow.com Lamin Barrow

    I have considered learning PHP a year ago but i lost interest when i started to build web apps in ASP.net but it seems you have just reignited my interest in the language with this article. Thanks very much. :)

  • Charlotte Spencer

    Tizag.com Offer a really good tutorial also.

  • Charlotte Spencer

    My bad, it’s been mentioned :D
    I will be following these tutorials as I’m starting PHP right this second.

  • http://talkingtofu2.iblogger.org Taylor Satula

    Really helpful im not a php guru so this is great

  • http://www.miketorosian.com Mike

    eagerly anticipating lesson 2, is it coming soon?

  • Pingback: Learning WordPress » Post Topic » Sidebars and widgets and PHP! Oh my!

  • Nathan

    If you could include links to the various lessons as you go along, that would be awesome. I’ve learned a lot from this tut and this site in general, thanks a lot.

    -Nathan

  • http://www.miketorosian.com Mike T.

    When can we expect part 2?

  • unslb

    Thanx for this very interesting and helpfull

  • http://www.johndeszell.com John Deszell

    Great starter!

    I read through it pretty briefly and will have to go back through it and look into it more. I’ve been wanting to learn PHP as right now I’m a guru of XHTML/CSS but have no programming experience. The big push lately is that I recently moved and trying to find another job and most of them require you to know either PHP or ASP. My old job I just did front end work and we had programmers that just programmed and didn’t touch the HTML/CSS. I rather learn PHP as I’ve been tinkering heavily with WordPress and want to move on learning Drupal as well.

    I look forward to learning with you and the next installment.

  • Pingback: Trucos básicos en PHP para tu sitio web

  • http://joshmmiller.com Josh

    Whatever happened to the weekly lessons? :-(

  • http://www.miketorosian.com Mike T.

    I’m with Josh, I was really pumped up for this and there hasn’t been anything in like a month.

  • http://freshclass.deviantart.com Charlotte Spencer

    Im also waiting for the next lesson… :(

  • http://www.ewan.org.uk Ewan

    Me too. Such a great idea as well.

    Jeffrey?

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @Everyone – Check back on Tuesday for Part 2.

    P.S. I apologize for the delay. Basically what happened was, days after writing this article, I became the editor of NETTUTS. I suddenly was put in a position where I didn’t have time to write Part 2 the way I wanted. However, I’ve gotten someone to take over the series. Tomorrow, we’ll have Part 2 of the Ruby series. Then Tuesday, Part 2 of this series! Wahoo.

  • http://www.ewan.org.uk Ewan

    Fantastic! Can’t wait. I have a few books on PHP, but the problem with books are I can pick them up and down as I please. And they tend to be down more often than up. With these tutorials I can do X amount of learning per week, almost like following a college course. Fantastic.

    How many do you intend to do, out of interest?

  • Sam Halta

    THANKS FOR THE UPDATE!!! I can’t wait… been waiting for an update to why it wasn’t continued! Good Luck as an editor!

  • Pingback: Learn PHP from Scratch - Part 2 - NETTUTS

  • Pingback: pligg.com

  • poring

    Hi!

    I have a stupid question regarding php. Do I need an internet connection? Because I prefer to work at home (where I have no internet access). Also for WAMP if I use that tool would it also require me to have internet access? Sorry for posting noob question. But I’d really appreciate your answers

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @poring – No. You don’t.

  • http://www.estudigital.com Estudigital

    Excellent!! I want more… :p

  • waqas

    Thanks, I am new in php world and this help me most to understand php language grammar .. Thanks

  • Pingback: Two Mad Geeks Tech Articles » Blog Archive » A Training Regimen For Learning PHP From Scratch

  • waqas

    Thanks, php world and this help me mo
    I am new inst to understand php language grammar .. Thanks