Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

A step-by-step guide to learn PHP

Authors

Getting Started with PHP

Getting Started with PHP - Part 1: Overview

Sep 4 2012 12:00AM by grantwparks   

Before saying anything else, I'll give a list of great PHP reference links. php.net is the PHP programmer's standard manual and any function can be looked up in the browser by typing php.net/function-name (like php.net/echo) in the address bar.

PHP was introduced in 1995 and has remained one of the most popular Web programming languages for years because it has continued to add powerful support for large-scale systems, combined with a syntax and structure that is relatively easy to learn. Its popularity has fulfilled itself, in a way. Because it has been popular for so many years, there is an unlimited supply of information for PHP developers and the community is an extremely open one.

The letters "P-H-P", originally stood for "Personal Home Page Tools" when Rasmus Lerdorf put together a set of Perl scripts to perform some of the work required to build dynamic Web pages. As opposed to "static" Web pages in which the content doesn't change, dynamic, or server side Web pages consist of content that is built by calling a program with information about the browser request.

html-vs-php

To do so at the time required working directly with CGI (Common Gateway Interface), using Perl scripts or compiled C programs, which could be tedious and error-prone. More importantly, it often meant "re-inventing the wheel". Rasmus' scripts would hide some of that repetitive work, but to keep things dynamic there has to be a way for each page to tell the scripts what to do. That's the PHP language; a set of instructions that tells the PHP engine what to do, and all the fine details of communicating with the operating system, databases, etc. are left up to PHP. (Before releasing it to the public, he rewrote the Perl in C, which can be compiled and run faster.)

The need for his tools was apparent and interest bred support from other developers. Two young men, Zeev Suraski and Andi Gutmans rewrote the parser in 1997 and founded Zend Technologies to support and further evolve the language. (Zend is the authority on PHP.)

PHP is a "scripting language”, which means no explicit compile step is necessary to turn your source code into something the computer can run. One simply edits and saves the source file. (The truth is that PHP code gets compiled and cached the first time it's requested.) Just refresh the browser to see the results. It can be run from a Web server (most common) or from the command line.

In the case of Web pages, the Web server is configured to know from the name of the file in the URL (e.g. "index.php" in http://www.someplace.com/index.php) to call the PHP interpreter with the file contents. PHP works by parsing the requested file first and executing any PHP code it finds before sending the final HTML document to the browser.

I began using PHP in 2006. Before that I didn't know it was suited to serious Web application development. I like it for the same reasons that I like Javascript: it supports object oriented design in a straightforward way, but doesn't force everything to be an object, which can be constraining during early development of an application.

PHP runs incredibly fast for a scripting language. And many open source extensions exist to provide a performance boost when scale demands it. (Load balancing, memcache, etc.)

PHP Web Pages

When you edit a PHP "page", it can look a lot like a normal HTML file, with the notable exception that it contains PHP code surrounded by the markers "". A very simple example is:

<html>
<head>
  <title>Hello world</title>
</head>
<body>
  <p><?php print ('Hello World, the date is: ' . date('D, d M Y')); ?></p>
 </body>
</html>

If you display the page in your browser, you will see the message using whatever default styling gets applied. If we look at the source of the page in the browser, we see the expected HTML:

<html>
<head>
  <title>Hello world</title>
</head>
<body>
  <p>Hello World, the date is: Wed, 11 Apr 2012</p>
 </body>
</html>

The PHP was executed and the output was inserted into the page.

Including Other PHP Files

The ease with which we can mix PHP and HTML also makes it easy to write very messy Web pages that are hard to maintain, which has led to a bad reputation in some circles. But it would be wrong to blame the language for its poor use; it is easy to write PHP Web sites following good structured software techniques. In a large system, one will find many PHP files that have no HTML in them at all. These scripts get included and called from within pages to work with the file system, databases and implement business logic.

In such a design, we would separate the meaningful PHP code from the HTML by creating a simple page that includes and calls other PHP. First, the print statement is moved into a function inside a separate file that we can include on any page. Name it world.php.

<?php
/**
 * Returns a hello message with the date
 * 
 * @return string
 */
function hello () {
  return "Hello World, the date is: " . date('D, d M Y');
}

Then the original page is changed to include the new file and call the new function.

<?php
  include 'world.php';
?>
<html>
<head>
  <title>Hello world</title>
</head>
 <body>
  <p><?php print (hello ()); ?></p>
 </body>
</html>

Notice in world.php that I didn't close the opening should be omitted, because it can cause unintended output to be introduced into our stream when multiple files are being included.

PHP Command Line (CLI)

PHP can be run from the command line. With its easy connectivity and file system access, it makes a powerful alternative to native shell scripting when you need to write a utility tool. (The script may likely be usable on multiple platforms, by virtue of it being written in PHP, rather than in the native shell scripting language of the platform.)

function ShowBigFiles ($dir, $sz) {
    $_dir = new RecursiveDirectoryIterator($dir);
    foreach ($_dir as $file) {
    	if (filesize ($file) > $sz)
    		echo "$file\n";
    }
}

if ($argc == 1 || trim($argv[1]) == '') {
    $_dir = dirname(".");
}
else {
    print("Usage: bigFiles [dir]\n");
    exit(99);
}

ShowBigFiles ("/Users/grantwparks/Downloads/", 100000000);

The ShowBigFiles function takes a directory to scan and tests all the files against the $sz argument. If the file size is bigger than $sz, the file name is output.

/Users/grantwparks/Downloads\eclipse-SDK-3.7.2-win32-x86_64.zip
/Users/grantwparks/Downloads\gnucash-2.4.8-setup.exe
/Users/grantwparks/Downloads\hp_LJ_P1005_P1505_Full_Solution_ROW.exe
/Users/grantwparks/Downloads\ProsperDataPrivateExport_xml.zip
/Users/grantwparks/Downloads\ubuntu-10.04.4-desktop-amd64.iso

I have used PHP to automate a variety of processes, including unit tests, file conversions using XSL and Web page scrapers. You'll have access to all of PHP's functionality from the command line. URL (http://us3.php.net/manual/en/function.curl-exec.php) and other extensions exist to simplify HTTP access.

Popular Web Server Language

A (surprisingly) large number of well-known sites are powered by PHP. Based on recent analysis of the top 40 most visited sites as reported by Google, it looks like Facebook, Wikipedia and Apple all use PHP. Another often-referenced index, the TIOBE Programming Community Index, shows programming language popularity based on the number of vendors, courses and skilled engineers. PHP is still in the top 10 list. In fact, the top 10 is mostly mature languages and not the newer, more seemingly popular ones.

Reasons to choose PHP for development:

It's completely free -- free to download, free to distribute in your application (no licensing issues), free to update -- you can even freely create your own version of PHP. In addition, the popular Web servers (Apache), databases (Postgres, mySQL) and IDEs (Eclipse, Netbeans) which PHP is often developed and deployed with are all free.

It's a mature and robust language, with over 15 years worth of development by Zend and collaboration with open source developers around the globe. The result is an almost unlimited supply of language extensions, code tutorials and a giant online developer community.

It's easy to learn, yet powerful enough for high-traffic commercial use.

Reasons to learn PHP:

Being proficient in such a popular programming language means being a more marketable engineer.


grantwparks
174 · 1% · 280
8



Submit

Your Comment


Sign Up or Login to post a comment.

"Getting Started with PHP - Part 1: Overview" rated 5 out of 5 by 8 readers
Getting Started with PHP - Part 1: Overview , 5.0 out of 5 based on 8 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]