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://www.insicdesigns.info insic

    nice for the php beginners. useful tutorial!

    • Maet

      Nice for the php beginners!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • http://inspirationup.com Joefrey Mahusay

    This is very helpful article. I love to see more tutorials about WordPress Hacks. :D

  • Goredeep

    I have tried a few times to learn php and haven’t done that well…

    I look forward to these next few tutorials! If you can teach me, you are a genius lol

    Good work!

  • Chandrashekar

    The PHP 101 is perhaps the best tutorial I have ever read. Anyway, nice to have a separate PHP tutorial started on this site! All the best!

  • http://www.freshclickmedia.com Shane

    Hi Jeffrey – you summed it up perfectly in your first paragraph. I too, am a C# ASP.NET developer by day, but it’s the success of wordpress, codeigniter and expression engine that have made me look more closely at PHP.

    I feel much more comfortable with an MVC framework such as codeigniter than raw PHP, but I’m enjoying using the language.

    Thanks for posting.

  • http://www.ben-griffiths.com Ben Griffiths

    Another good PHP/MySQL installer for windows/mac/linux is xampp: http://www.apachefriends.org/en/xampp.html. Once thing that wasn’t mentioned is the indentation of code. As your code gets longer, it’s a good idea to indent, like this:

    This allows you to see functions etc on the page much more easily.

  • http://www.ben-griffiths.com Ben Griffiths

    Oh. It never showed the PHP – is there a way to do this in the comments here?

  • PHP Guy

    Combining HTML With Our PHP

    <?php echo “This text is bold.“; ?>

    DONT MERGE HTML, KEEP THE CODE OUT OF THE VIEW!

    and the ‘ is fastest than “

  • http://www.Oskard.be Jbcarey

    This is EXACTLY what i need!

  • http://www.apostropheart.com/ Mike

    After 8 months of teaching myself PHP I consider my skill level now to be intermediate. Not sure if I am allowed to plug somebody elses stuff here but from all the tutorials I found and books I worked through, the one I found most useful (and still the first I go back to if I am stuck) is “PHP Solutions” by David Powers and published by Friends of Ed.

    Can’t comment on Wamp or Xamp but Mamp is an excellent piece of kit. Aside from learning PHP it also means you can run local installations of WP, Drupal and EE which is great for developement.

    Personally, I don’t see how you can properly develop a WP site without some knowledge of PHP.

    Looking forward to following the series.

  • http://www.graphicrating.com Andy Gongea

    Thank you guys for this good tutorial. Many designers want to learn PHP and this is a great resource.

  • http://enhance.qd-creative.co.uk James

    Another great set of PHP tuts can be found here -> http://tizag.com/phpT/

    … nice article Jeffrey! :)

  • http://www.simplr.nl Ronald

    I found the tizag tutorials extremely helpful when starting to learn the basics of php (and other stuff, like XML)a while back. Maybe an addition for your resources? http://www.tizag.com/phpT/

    Other than that, very clear article, I know the struggles of a starter can be frustrating.

    Keep up the good work

  • http://www.simplr.nl Ronald

    ;-) James beat me by a few seconds, please disregard my comment re. tizag or otherwise, my apologies!

  • http://www.mikaelrieck.com Mikael Rieck

    What a wonderful idea. I’m one of the people that just haven’t taken the time to learn but with this offer I no longer have an excuse. I really look forward to learning with you.

    /Mikael

  • San

    Being a designer & good in xhtml & CSS, I have always wanted to learn programming.

    The best bet will be to learn php, & I’m the happiest that you have started to teach it :)

    My recommendation is to give video tutorials too, you can charge for them & they will be the best bet.

  • http://www.elliottcost.com Elliott Cost

    Very cool! I’m super excited about this and will be following these tutorials. Keep them coming!!

  • http://psdetails.com Exo

    Cheers thanks. Great for beginners. Just waiting for next-week. How about a bi-weekly? lol

  • http://niceone.filipruzicka.net/ Filip Ruzicka

    Oh, thanks for this tutorial. I’ve been working with PHP & MySQL a while, but I’m still a beginner. I would appreciate more tuts like this.

  • http://www.omeucasamento.pt João Araújo

    Great tutorial, looking forward for the next one!!

  • http://www.broof.de BroOf

    I woul love to see more tuts for PHP!

  • http://japangamingguide@yahoo.com James

    Great stuff, I’ve tried playing around with my wordpress theme before, now I’ll know what I’m doing. Roll on part 2!

  • http://www.tomschlick.com trs21219

    http://www.gravatar.com/avatar/8ce8ff45797a11ac9d5a33a6c336404d?s=80&r=any

    I think these tutorials will be very useful for php beginners. I expect the level of the tutorials to be far superior to anything i have seen on the web before just as everything on the eden sites is. Ill try to contribute to the tutorials also.

  • yamaniac

    great compilation!

  • http://www.uysys.com Uysys

    Great article.

    ———————————–
    http://www.uysys.com

  • http://brendanfalkowski.com Brendan Falkowski

    Hoping this goes down the object-oriented and class-driven PHP 5 route. That’s the precipice standing before me.

  • http://help-developer.com Simon North

    I learnt PHP via w3schools, it is by far the most helpful site and I now use it whenever I need to remember something or learn something new.

  • http://www.reliablesource.org Phill Pafford

    Adding Killer PHP to the list

    http://www.killerphp.com/

  • http://www.axlrod.dk Alex Rodebnberg

    Tizag helped me with simple guides for PHP among other things.. great site ..

    And one thing you have to learn fairly quickly is php.net .. you can search all functions there .. and basicly if you can code in another language.. thats all you need.

    Usually there is good examples of proper php code with all functions too, so you learn proper syntax .. and general good behaviour in php coding..

    I found that classic ASP can be VERY different from tutorial to tutorial .. because you could do stuff so many ways.. Thats not the case in PHP .. people generally behave good when coding.

  • http://desert-lion.blogspot.com Rijalul Fikri

    Wow, you write a very wonderful tutorial. It is easily understood. Btw, I’ve never used XAMP before. For those pack, I’m using appserv –> http://www.appservnetwork.com/ and it works well for me. Looking forward to see your next tutorial.

    If only I found this tutorial when I first learn PHP, it will be very easy for me :), ttu for the tutorial

  • http://devjargon.com Alex

    Good intro to PHP.

    I’ll personally vouch for Lynda.com. It is a fantastic resource. We use it at work for the majority of our training programs, and since they cover so many topics, nearly every department benefits from it. One of the big things they don’t cover though, is C# and other big programming languages. They do have most of what any designer/web developer would want, though.

  • John

    Where is the love for LAMP? .. Its the daddy of them all!

  • Dan

    Thanks! This is great- I’m looking forward to Wednesdays now!

  • http://www.mrtunes.ca Mr. Tunes

    hey this is a great concept. i need a training regimen. i have a lynda subscription, but i found that the book i’m reading is confusing me really quickly.

    it’s ullman’s visual quickpro guide which i think is highly rated. but all the discussion of slashes / early on is throwing me off.

  • http://www.peacockcarter.co.uk Richard, Peacock Carter

    Not a developer myself, but I think I can feel whether a website is PHP, ASP or Coldfusion. PHP certainly seems to power many nicer-functioning websites!

    The thing I hate about WordPress is that the PHP *is* mixed in with the HTML; it’s just messy.

  • http://joshmmiller.com Josh

    Thanks for this post. I can’t wait for the next few articles. This is exactly what I’ve been looking for!

  • http://jwaltonmedia.com Jdub

    Alleluiah.

  • Ty

    Looks like a great series!
    You forgot one other acronym xampp is a great localhost developers choice.
    http://www.apachefriends.org/en/xampp.html
    I’ve just recently setup the free IDE editor http://aptana.com/ Aptana Studio to work with a Xampplite setup, so I’m ready to rock and roll on these tuts! Bring it on…

  • http://thedailyapp.com Tommy M

    Great tutorial for those who are familiar with XHTML/CSS and want to broaden their horizons to work more on the backend than the front end. Most employers who are looking for front-end developers prefer applicants that are familiar enough with PHP experience to not get scared when they’re handed a page that contains “”.

  • http://thedailyapp.com Tommy M

    To the moderator: My email is tommy@thedailyapp.com, not @thedaulyapp.com

    Thanks,

    Tommy M.

  • vic

    this is 100% pure awesomeness

  • http://www.aminorstudios.com Eric Thayne

    I’ve been procrastinating learning this language for too long…

  • http://ha.xors.org Braden Keith

    THANK YOU THANK YOU THANK YOU. kiss kiss on each cheek.

    You do listen to what your audience wants! :D

  • Adam Griffiths

    Just to clear one thing up, PHP actually stands for PHP: Hypertext Preprocessor, not just Hypertext Preprocessor, but I’m just being picky.

    This is a good start for anybody wanting to learn PHP.

  • Miles Johnson

    Use XAMPP as a local server.

    Also its bad practice to echo in a function, always do a return.

  • Pingback: nerdd.net | news and opinion

  • Jonny McGurn

    Excellent resources here, great for beginners.

    Would of done it in a different order myself, ie: covered if statements before I went onto functions but that just me..

  • http://www.digitalskraps.com David Sparks

    Awesome!
    In April I got hired based on the web and design skills I already posses but the position was for a PHP coder which I dont know. The plan was i would receive training from the office since they have 1 coder already, they just need another however we’ve been so overly swamped my training hasn’t started yet. I have the lynda movies, I have a book, but I was needing a site I trusted and frequented already that is addressing PHP in a way i can understand being a designer/html/css coder.

    very excited to see this at its origin and to be a part of it since I’m now learning myself. It will help a lot to go along with other people learning as well.

    Cant wait!

  • scott

    * and the ‘ is fastest than “

    This argument has raged for a long time. All the latest is that there is next to 0 difference between them with latest PHP versions.

    With that said I personally like ‘ over ” because of the requirements to not have variables inside them. It forces me to concat variables so color coding picks them out nicely.

    for example:

    echo “hello $user_name! Welcome back”;

    vs

    echo ‘hello ‘.$user_name.’! Welcome back’;

    the second will color code properly in just about any PHP aware editor where as the first won’t.

    great article.

  • http://mine kevin

    .net sucks balls. what are you thinking?