Monday, March 31, 2008

How hard is PHP to use and learn?

PHP is not rocket science, but at the same time, do not expect to become an expert in five minutes. If you are a design-oriented person, you may find it takes time to get used to the way PHP is written. What we like about it very much is that it is succinct. For instance, in classic ASP, to display each word of a sentence on a separate line, you have to type out all this:

<%@ Language=VBScript %>

<% Option Explicit %>
<%
Dim strSentence, arrWords, strWord
strSentence = "ASP uses far more code to do the same as PHP"
arrWords = Split(strSentence, " ", -1, 1)
For Each strWord in arrWords
Response.Write(strWord)
Response.Write("
")
Next
%>

In PHP, it is simply

$sentence = 'ASP uses far more code to do the same as PHP';
$words = explode(' ', $sentence);
foreach ($words as $word) {
echo "$word
";
}
?>

That may not seem a big difference, but the extra typing gets very tiresome over a long script. PHP also makes it easy to recognize variables, because they always begin with $. Most of the functions have very intuitive names. For example, mysql_connect() connects you to a MySQL database. Even when the names look strange at first sight, you can often work out where they came from. In the preceding example, explode() “blows apart” text and converts it into an array of its component parts.

Perhaps the biggest shock to newcomers is that PHP is far less tolerant of mistakes than browsers are with XHTML. If you omit a closing tag in XHTML, most browsers will still render the page. If you omit a closing quote, semicolon, or brace in PHP, you will get an uncompromising error message. This is not just a feature of PHP, but of all server-side technologies, including ASP, ASP.NET, and ColdFusion. It is why you need to have a reasonable understanding of XHTML and CSS before embarking on PHP. If the underlying structure of your web pages is shaky to start with, your learning curve with PHP will be considerably steeper.

PHP is not like XHTML: you ca not choose from a range of PHP editors that generate all the code for you automatically. Dreamweaver does have considerable support for PHP, and it automates a lot of code generation, mainly for integrating web pages with the MySQL database.

No comments: