A Beginner’s Guide to PHP
A Beginners Guide to PHP
1) What is it?
2) What does it do?
3) What do you need?
4) How does it work?
What is it?
PHP is a recursive acronym for Hypertext Preprocessor. It is an open source scripting language used often in web development and can be embeded directly into HTML and other markup. PHP started life in 1994 by Rasmus Lerdorf who created a set of scripts written in Perl which he called “Personal Home Page Tools” to maintain his blog. PHP has matured into a full-blown, object-oriented scripting language.
What does it do?
PHP is primarily a “server-side” scripting language. This means that all of the code runs on a server and the results are output as part of the HTML. You can have scripts written in PHP that perform tasks, but the viewer never sees the code as only the output is viewable.
PHP is capable of generating dynamic content (content that must change on the fly), collecting user form data, reading and writing to a database and even more advanced things like command-line scriptiting. Major websites like Yahoo, YouTube, Wikipedia, Facebook and many others use PHP for its dynamic content abilities.
PHP’s database support is very strong allowing great flexibility. SQL and MySql are the most commonly used, but PHP will support any database using the Open Database Connection standard via the ODBC extension.
In addition to text-processing features, PHP has been extended to standardize all of the xml extensions adding SimpleXML, XMLReader and XMLWriter support.
PHP’s output abilities include creating HTML, images, PDF files and even Flash movies generated dynamically.
What do you need?
PHP must be installed on a server or computer in order to run scripts written for it. Most web servers include PHP these days though often times it will have to be installed. You can run PHP on Mac and Windows if you are doing local development on your computer. There are many ways of doing this, but its most easily facilitated through apps such as MAMP for the Mac and WAMP if you’re using windows. Both applications are free. When lanched they create a local instance of a web server and you may excecute PHP scripts from whatever folder is defined by the application. Both MAMP and WAMP give you mySQL databases so they are excellent environments for writing PHP code.
How does it work?
Assuming you are on a PHP enabled web server or you are running MAMP or WAMP with proper setup, PHP requires two things.
First – your document extensions must be .php – this will tell the server there is code there to render.
Secondly – you use delimiters to excecute PHP scripts. Delimiters are much like tags in HTML, but they allow you to go into “PHP Mode” and tell the server you are now coding in PHP.
PHP delimiters include <?php to open and ?> to close. So you might insert a script into your HTML that looks like this:
<h1>We will now excecute a script:</h1>
echo "Hello World";
Delimiters are useful for several reasons. They allow you to clearly separate HTML output from PHP scripts. They also allow you to go in and out of “PHP Mode” as often as you need – usually many times inside one HTML document.
You can and often do have documents that only contain PHP scripts. They still require the delimiters to work properly.
Notice in the script above what we’ve written inside our delimiters. The word “echo” is a command telling PHP to output what’s next. We use quotes to specify what is output. Our script ends with a semi-colon. This acts as punctuation telling PHP we are through with our “coding sentence”.