Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

And by the way, I am New to PHP

Published:
Updated:

Foreword (May 2015)

This web page has appeared at Google.  It's definitely worth considering!

https://www.google.com/about/careers/students/guide-to-technical-development.html


How to Know You are Making a Difference at EE

In August, 2013, one of my colleagues posted this, and it made my heart glad.

Grading Comment:
Thanks so much Ray!  I actually haven't posted in a while because at your suggestion I got a few books and spent the last couple of months reading and learning and it's made a world of difference, so thanks a ton for that suggestion as well!

"And by the way,... I am New to PHP"

So many PHP questions at Experts-Exchange include (or should include) that statement that it got me thinking about how to advise new programmers on ways to begin learning PHP.  The popularity of PHP is undeniable; it powers an overwhelming number of web sites, from the very smallest to the giants like Facebook, hundreds of thousands of WordPress blogs, and everything in between.  So it makes sense that any web developer would need to have some foundation in the PHP language.  And for many novice programmers, PHP is the first language they try to learn.


PHP came from humble beginnings; it was originally captioned "Personal Home Page" and was intended to be a language so easy to use, that "even Grandma could learn to use PHP."  And that was fine in the 1990's before the advent of object-oriented programming, hackers, spam, and web services.  But the online environment has grown up and PHP has been forced to grow up, too.  Today, while PHP can still perform simple tasks, PHP can also power enormous database-driven applications that build online communities and analyze mountains of information.


My favorite description of PHP, disarming in its elegance and simplicity, is "What PHP can do is convert a static website that has content that has to be changed by hand into a dynamic one that can display content based on any criteria you can think of."


Getting started with PHP can be daunting, especially if you do not have a background in computer science.  This article will give you some learning resources to get the background you need and will introduce several popular and effective ways to learn PHP.  But there is something you might want to read before you finish this article.  Every professional programmer I know thinks that this is an article of wisdom.  You can take it in if you have seven minutes.  Then come back and finish this article.


Getting the Right Foundation

If you really want to be a professional programmer, you probably want to go to college to major in computer science or electrical engineering.  Almost every major college and university teaches these subject, and the principles are not very hard to learn, but it takes time and practice to turn the learned principles into praxis.  Even if you're not sure about making a career in programming, if you're read this far you probably want to consider taking an Introduction to Computer Science class.  Fortunately, these classes are now available online, for free, using the same syllabus, curriculum and learning materials that the colleges use.  


As but one example, here is what M.I.T. offers:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/Syllabus/


If you're serious about learning programming, you will need a little math (but not very much).  This series of video lectures will give you a good foundation.

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-042j-mathematics-for-computer-science-fall-2010/video-lectures/


Learning to Think Like a Programmer

For a variety of reasons, you're unlikely to find a basic computer science class that teaches PHP.  PHP is "easy" and these classes are for scientists who like things hard (I'm only joking a little bit).  Expect to find C, Python, Java, JavaScript and similar highly structured programming languages in the beginning classes.  Don't worry about that -- every programmer who achieves anything noteworthy has written programs in many different languages.  The ideas from one language or another bring about a level of intellectual cross-pollination that helps you think about problems from different perspectives and map problems onto different computational frameworks.  And at the most basic levels, all programming languages share many common aspects.  The greatest difference is the written syntax we use to express our ideas.


Do Not Waste Time on Non-Working Code

A programming language is a tool that builds a sequence of instructions.  The instructions tell the computer what we want it to do.  For almost everything you might do with PHP, the thing we want the computer to do is manipulate data.  Each programming problem can be defined and understood in terms of an input and an output, with the computer programming being the intermediary that transforms the input into the output.  If you think about that for a little while, you will come to the same conclusion that almost every experienced programmer comes to: it does nobody any good to post code that does not work.  Instead define and post the data and you will be amazed how quickly you can find clear, easy-to-understand answers.


Expect to Spend a Lot of Time Debugging

You will find that you're human and the computer is not.  Problems expressed in human languages are turned into solutions expressed in computer languages.  This process is full of ambiguity, double-meanings, and confusion.  Humans share culture, but computers require precision at a sub-microscopic level.  Even a single character out of place can render a computer program useless (or worse, destructive).  It's very rare that a programmer can write more than a few lines of code without introducing an unwanted element.  The process of correcting and removing these unwanted elements is called "debugging" and until you have several years of programming experience, it will consume most of the time you spend programming.  More about programming and human factors, as well as many interesting observations from the trenches is available in Jeff Atwood's writings.


Deconstruct Problems Until They Are Small Enough to Solve at a Glance

Complex programs are made up of simpler programs, and simpler programs are made up of even simpler programs.  A mental process of deconstructing problems underlies all programming activities.  As you learn PHP, you will find that you deconstruct complex problems into several smaller problems that are readily solved using PHP programming solutions.


You may have heard of the phrase, "Hello World."  It is the name given to the simplest example of the solution to any programming problem, and it demonstrates success in some small aspect of building a more complex program.  In PHP, the "Hello World" exercise looks something like this.


 

<?php echo "Hello World.";

This simple PHP script proves that PHP is installed correctly and that it can produce output that is visible on a web browser.  More importantly, it removes many elements of confusion and ambiguity.  What if this script did not work?  Where would you look for a solution?  What symptoms of failure could you find?  Who can help?  All of these uncertainties and doubts impinge on the process of debugging.  And thus we come to the first and most important part of thinking like a programmer: Make the larger problems smaller by reducing each of the larger problems to a collection of smaller problems.


Do this iteratively until the smaller problems are so small that the solution is obvious.  The small solutions then become the building blocks of the larger solutions.  As you construct larger solutions from smaller building blocks, you will hide the details so that the result works like a vending machine - providing useful services to many clients (either programs or people).  Programmers often refer to such self-contained units as a "black box."


Learning the Language of Programmers

A shared culture has a shared language and many terms of art.  You can learn something of PHP without knowing much about general purpose programming, but your learning will be faster and easier if you understand the language that programmers use.  Many familiar words assume new meanings.  For example, the word class takes on a unique meaning when it's applied in the context of computer programming.


I am not suggesting that you need to read or memorize every term of art (a professional will) but you need some good references that you can use to look up words that are unfamiliar or are used in unfamiliar ways.  You might want to take an afternoon to browse these glossaries.


LabAutopedia

Henson's Glossary

Cal State CS202

WhatIs.com TechTarget Programming Words


Learning the Techniques of Programmers

All good programmers have been forced at one time or another to research a subject that they know nothing about.  They have to find out numerous details (too many to remember) and develop code that handles these details.  It's a huge exercise in short-term memory, and it's a huge investment of time.  You wouldn't want to have to do it over again.


Unfortunately, some programmers are condemned to do it over and over again because they didn't write the code clearly, or they used thoughtless variable names, or they left out the comments.  And when they need to reuse that piece of code, they find themselves looking at it and wondering things like, "Did I test this enough?" or worse, "What was I thinking?"  You can avoid this problem by following a few simple rules.


Before you begin writing code, learn the "PSR" basics.  These are valuable standards.  They will make your programming easy to read and understand, and they will make your code look professional (highly valuable if you want to get a job as a programmer).  Here are the two links you should study.

http://www.php-fig.org/psr/psr-1/

http://www.php-fig.org/psr/psr-2/


1. Whenever you're required to solve a problem that you have never solved before, make a teaching example from the solution.  Collect a library of these.

2. Whenever possible use meaningful variable names, eg, if the value is "Today's date" name the variable $today instead of something useless like $x.

3. Use comments to say what your programming is intended to do.  Often you will find that simply writing the commentsfirst, before you write a single line of code, will help you consolidate your thinking.

4. Test iteratively as you build the code.  Don't write more than a few lines without testing.  Use the principles of Test-Driven Development.

5. Programming is all about the data.  Learn about var_dump() and make regular use of it!

6. Avoid copying code you find on the internet, especially code that has a lot of compound statements.  Instead of copying, deconstruct the code, taking a moment to ask, "why?" and rewriting the code in your own words, with your own explanatory comments.

7. Adopt a coding standard and adhere to it rigidly.  With discipline comes great power.


Learning How to Get Help: the SSCCE

Anyone who says he is a self-taught programmer is missing something.  You cannot make this stuff up - you have to learn it from a variety of sources, and one of the most important sources is the community of programmers who surround you.  Perhaps you're in a university class where you can learn from your colleagues and classmates.  Or perhaps you're a regular at Experts-Exchange and Stack Overflow.  In any learning community you need to know how to share ideas with others.  That's where the SSCCE comes in.  If the only thing you take away from this article is the SSCCE, it will have been worth my time to write it and worth your time to read it.  Follow the link, read the page (there is only one page) and embrace the principles!  The SSCCE is not alone in the universe, other authors have given voice to the same concept: a problem well stated is a problem half solved.


Think about the data - what do you have for input and what do you want for output.  Try to assemble that information before you ask the question.  Then you can say, "Here's what I've got and here's what I want."  That makes for a clear problem definition.  Clear problem definitions will save you an amazing amount of time.  As Charles Kettering said, "A problem well stated is a problem half solved."


Try to avoid complicating the situation with an unnecessary explanation, when an input/output example could be used instead.  This is an actual quote from a question posted here at EE: "take that number, split it into an array. so 25 would become [2,5], or 12 would become [1,2] then use those to determine which image to show in the basket count, then loop the array to determine which images to show"  What's the author trying to say?  Your guess is as good as mine.


Learning PHP from PHP.net

Now that we have introduced the deep background stuff, and shown you how to frame a problem and ask for help, it's time to get into the part of this article that is about learning -- and using -- PHP.


A good place to start is with php.net, where you will find the best online technical documentation in the world.


If you do not read the online documentation, you're robbing yourself, stealing valuable time from learning.  Instead of learning from the experience and writings of others, you'll be learning by trial and error, maybe from reading other people's programs.  That takes longer.  A lot longer.  And it's full of risk.  Trying to learn programming by trial and error is like trying to learn to bake by looking at an apple pie.  Sure you can appreciate the finished product, but looking at a pie will tell you nothing of the other ingredients, the processes and the tools used to make the pie.  Likewise, a finished program tells you nothing of the thought processes of the developers, nor of the assumptions they made or the test data they used.  So don't try to learn that way.  Instead, exploit the php.net web site for all its worth.  Here is how.


New in 2014: PHP has an About the Manual page, including How to Read a Function Definition and links to Support for New Users.


PHP has a "getting started" page.

PHP has an introductory tutorial.  No excuses -- Just Do It!

PHP has an omnibus FAQ page.

PHP has its entire manual online!


PHP has its own Security page and it is required reading, no excuses!


PHP has the language reference online.   No excuses here either.  You must read these sections and every one of the associated links:

Basic Syntax

Types

Variables

Constants

Expressions

Operators

Control Structures

Functions

Predefined Variables


If you want to move beyond basic PHP programming and begin collaboration with others, you want to read these sections, too.

Classes and Objects

Exceptions

References


If you want to interact with software or data on other servers you need to read these sections.

Context Options

Protocols and Wrappers


If you want to learn from the collective wisdom of others, these FAQ pages are very helpful:

Using PHP

PHP and HTML

PHP and Databases


Part of what makes php.net so valuable is the function reference.  PHP has, at this writing, over 1,500 built-in functions.  You can't memorize all of them, so you need a quick way to refer to them.  When I am programming I have a window open to php.net at all times.  If you know the name of the function you want to use, just type a URL like this: http://php.net/date and PHP will find the date() function.  Couldn't be easier!  And the magic doesn't end with the official PHP documentation.  Every function page has a section for user-contributed notes.  Often, seeing the way others have used a function (or learned from an unpleasant surprise) will give you great insights into the richness and power of the PHP language.  For example, see this:

http://php.net/manual/en/function.array-slice.php#112359


If you don't know the name of the function you want to use, you can find the function reference table of contents.   Don't plan on reading all of this.  You might want to bookmark it.


The parts of the function reference that you want to read first are probably these:

Date/Time

Database, especially MySQL

File System

Strings

Arrays

Variables

Sessions

SimpleXML

JSON


Learning PHP from Books

One of the really great things about tech people is that they like tech features, for example, the ability to write book reviews on Amazon.com. If you find a popular and well-reviewed book (and it's not too old) it will probably make a valuable addition to your technical/professional library.  You can't have too many PHP books!  Here are some of the books that I own and recommend.  If you decide to get any of these, be sure to look for the latest editions, because PHP is a living language.


Beginner: Yank Ignore the hokey title -- it's a good book.

Beginner to intermediate: Welling/Thompson

Intermediate to Advanced: Powers

Intermediate to Advanced: Ullman

Advanced: Zandstra


Learning PHP from Online Resources

Most of these resources are 100% free:  Almost anything about PHP from SitePoint is worthwhile.  There are good introductory references at Tizag and W3Schools, however the Tizag PHP web site appears to have gone out of date.  Many of my students swear by CodeAcademy.  And of course there is a Wiki: http://en.wikibooks.org/wiki/PHP_Programming


New in 2017: Stefan Priebsch curates this site on Software Craftsmanship (advanced)

https://php-craftsmanship.info/


New in 2016: Jeffrey Way brings his considerable teaching skills to a set of PHP courses here:

https://laracasts.com/series/php-for-beginners


While you are mastering the basics of PHP you will have many questions that others have had before you.  This is a worthwhile resource for many of those questions.

https://phpbestpractices.org/


Once you have mastered the basics of PHP, there is an excellent online resource from the authors of the Slim Framework.

http://www.phptherightway.com/


A set of paid courses on PHP is available from lynda.com.  It is not clear to me whether these are up-to-date.


A set of paid courses on PHP, from active PHP experts, is available from PHP Architect.  The company also publishes books and newsletters, and runs conferences covering PHP topics from the most introductory "bootcamp" to advanced object-oriented design.  If you want to learn from the best in the business, this is the place to come.


Learning PHP from "That Guy" -- NOT!

You've heard the expression, "Don't be that guy."  I'm going to add a recommendation here: "Don't learn PHP from 'That Guy'."  The internet is littered with examples of terrible PHP code.  Mostly it's code from half-taught programmers who don't understand the principles of computer science, don't understand the principles of programming security and don't take the time to clean up after themselves.  It turns up in forums all the time.  It may be untested.  It's almost always undocumented.  And it's something you would use at your own peril.  It's usually worth exactly what you paid for it!  So just say "no" to that stuff and stick to the links published here.  It will save you a lot of grief.


Summary

Don't feel awkward or embarrassed if you're new to PHP.  Ignorance is no sin and we were all new to the language once.  Instead, embrace the learning resources documented here, build your own personal library of code examples, post plenty of PHP questions at Experts-Exchange and most importantly give yourself time to work with the language, so that you become familiar with the commonly used parts.  The gift of time and study is one that only you can give yourself, and it may be the most valuable gift of all. And if you're doing it right, learning PHP is fun.


Welcome to PHP!


Please give us your feedback!

If you found this article helpful, please click the "thumb's up" button below. Doing so lets the E-E community know what is valuable for E-E members and helps provide direction for future articles.  If you have questions or comments, please add them.  Thanks!

 

30
15,189 Views

Comments (4)

Most Valuable Expert 2011
Author of the Year 2014

Author

Commented:
Hi, lherrou, and thanks for your comments.  I'll add the final reminder (and I may have a few other edits now that I am looking at it with fresh eyes after a few days).  I do not have any inferiority complex about StackOverflow.  Anyone who posts a "newbie" question on SO gets flamed.  It's not the kind of community that is welcoming to people who are just trying to find their way.

In my experience, the title of an article may not really be relevant.  I've found that posting a link in response to a question is when my articles get read.  It seems to me that spontaneous readership is not all that big of a deal.  Perhaps this is because PHP is a narrow-enough field that we don't get a lot of folks browsing the shelves for an interesting read.

;-)  And thanks for your help,
~Ray
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
Excellent. (Of course, I expect nothing less from Ray).

I have forwarded this on to my cousin, who I am teaching programming (He's 10).  And we'll be using this as our lesson plan for our weekly class.

Thank you for putting this into words. It's great.

-DrDamnit.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Another thing that I have had to almost beat into some people's heads is that you can't 'logically' figure these things out.  While computers at the lowest levels operate logically, the things that people actually deal with like programming or even just using a program are based on the Choices made by someone somewhere.  You can't logically out-guess the choices made by someone else, you need to look them up in the documentation and find the rules that apply.  

The PHP developers are constantly trying to make function calls as uniform and predictable as they can... but there are a lot of things that have slipped in over the years.  And another truckload of things that are simply non-obvious.  Even those of us who write PHP code virtually every day have to do that.  People pay me to fix and modify other people's PHP code.  Like Ray, I have a browser window open to php.net almost all the time.
Most Valuable Expert 2011
Author of the Year 2014

Author

Commented:
@DaveBaldwin: Yes, that is the way!  Just look at an apple pie and see if it can tell you how to bake an apple pie.  It's the same with any recipe and the same with computer code.  There is not always a light unto my feet and a path before me unless I care for the details along  the way.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.