Monday, February 15, 2016

INTRODUCTION

From what you hear being said about it you could be forgiven for thinking it to be the panacea for all that ails the Web development world. There are those who would argue that you would be right in thinking that. While I may not quite go to that extreme, I will join those who will not argue against the notion! It is indeed a very useful weapon in the Web Designer's arsenal. So what exactly is it? Where can you get it? How much does it cost? How do you use it? Whoa there, Nelly! That's what this little series is all about. This is an introduction to PHP intended to give you a foundation for its use and to guide to resources you can use to get deeper into the PHP world. PHP is gaining ground quite rapidly and is being accepted and used in some very demanding and traditionally cautious environments. There are good reasons, so you are encouraged to read on!

PHP started its life in 1994 in the mind and hands of one Mr. Rasmus Lerdorf. Recognizing the virtue of his creation a number of very talented programmers have lent their skills to its development, and have included a number of complete rewrites along the way. The latest release, PHP4 comes from a company called Zend. PHP is an Open Source project, meaning that its source code is available for you to study, use and modify, should you so wish. Its name originally stood for Personal Home Page, but it has since adopted "PHP Hypertext Preprocessor" (GNU, the open source project is fond of recursive names like its own "GNU's Not Unix".) PHP is free. It can be downloaded from www.PHP.net or, for PHP4, www.Zend.com.

PHP is a Server Side scripting language. Compare that to JavaScript, which is a client side scripting language. The main difference between Server Side and Client Side concerns what data you can manipulate and when. If you want to manipulate data that is in databases on the server, then you need to use server-side technology. The web pages themselves also reside on the server, and so are also manipulated by server side technology. Once the page has been displayed on the client machine you need client side technology to manipulate data associated with it. For example, if you have displayed a form on which your site visitor is entering data and you wish to validate that data, allowing the visitor to make corrections, before sending the data back to the server, then all that validation must be performed by client side technology.

PHP FUNCTION


What is a Function?
In the last part of this tutorial series we looked at controlling logic flow in PHP.  This included logic flowing in a linear fashion, but with loops of logic at various points and with decision making along the way.  The overall flow was a straight line - the loops were processed where they occurred, and the logic continued along its line from the beginning of the program routine to its end.  There are times, however, when a series of instructions has to be repeated from a variety of different points within the main routine, or where a series of instructions needs to be followed that doesn't really fit within the main routine.  This all sounds horribly complicated -- let's see if a real world example will clarify the picture!
Suppose, for the moment, you have a bunch of freshly washed laundry, some had to be hung on a clothes line, some was dried in the drier.  Now it all has to be put away.  Some of the items have to be folded, some ironed and some hung on hangers.  First we need a set of instructions for removing the items from the clothes line, next a set for the drier.  As each item is removed from either source, we have to decide ("if" instructions, possibly?) whether the item needs to be folded, ironed or hung.  Then we need a set of instructions for each of these operations -- folding, ironing and hanging.  The processes (folding, ironing and hanging) are the same whether the item was removed from the clothes line or the drier.  Consequently, we can use the same folding, ironing and hanging routines whether we are calling them from the "remove from the clothes line" process or the "remove from the drier" process.
OK! Let's break it down!  (And you thought you knew how to do the laundry!)  First, our main routine is going to have two primary sections -- one for the clothes line and one for the drier. As an item is removed, we have to decide whether it should be folded, ironed or hanged, and process it accordingly.   Let's say that we know what "remove" means.  This allows us to say something like this (by the way, this kind of representation of what a program is to do is called "pseudocode" and is one of the most valuable tools available to a designer  -- beginner or seasoned expert alike!) ( let me repeat myself -- one of THE MOST VALUABLE TOOLS!!  Different software engineers use different styles of pseudocode, but pretty much any experienced one of them could understand any style.):
While (laundry still on the line)

PHP CODDING

Coding Standards are an important factor for achieving a high code quality. A common visual style, naming conventions and other technical settings allow us to produce a homogenous code which is easy to read and maintain. However, not all important factors can be covered by rules and coding standards. Equally important is the style in which certain problems are solved programmatically - it’s the personality and experience of the individual developer which shines through and ultimately makes the difference between technically okay code or a well considered, mature solution.
These guidelines try to cover both, the technical standards as well as giving incentives for a common development style. These guidelines must be followed by everyone who creates code for the TYPO3 Flow core. Because Neos is based on TYPO3 Flow, it follows the same principles - therefore, whenever we mention TYPO3 Flow in the following sections, we equally refer to Neos. We hope that you feel encouraged to follow these guidelines as well when creating your own packages and TYPO3 Flow based applications.


BEFORE PHP


Before you can take advantage of PHP extensive capabilities, it must be installed and available on the server hosting your website. Here's some good news! PHP is platform friendly. It runs on Windows, Linux and many flavors of UNIX, so the chances are it can be installed on your server if it's not already there. Because of its popularity most hosting companies these days offer support for PHP. If you're not sure about yours, call and ask them. If they don't support it, ask them why not, then choose another host!

Other server side languages, competitors if you like, include PERL, ASP (Active Server Pages) (&ASP.Net), JSP (Java Server Pages), Alaire's Cold Fusion and others. PHP has some advantages over its competitors. It's free - a tough price to beat! It's very efficient, making it capable of handling millions of daily hits on one inexpensive server (check out the benchmarks on http://www.Zend.com ) It's portable, meaning that it's easily moved from one platform to another (it's not identical on Windows and Unix/Linux, but it's very close - there would be only a few things to be changed.) It integrates with a variety of Database Management Systems (DBMS) including MySQL, which is also free! The list of DBMS is quite extensive and includes support for any systems that provides an Open Database Connectivity Standard (ODBC) driver, as most do.

Another big advantage of PHP is that it's easy to learn. That's what we're all about here. As previously mentioned, this introductory piece is to lead you into a new tutorial series that will provide a foundation for you to begin using PHP. We will be covering the basics of the language and providing some usable examples. In addition, we will be guiding you to some resources that you can use to increase the depth of your understanding of PHP and to expand the range of things you can accomplish with it. Gone are the days of the old static web page (well, almost!) Welcome to the dynamic world of interactivity and database connectivity on the Web!

USE OF PHP

This very first page you make that includes some PHP code deserves to be something really useful.  Don't let the simplicity of this little page fool you!  This is a very useful tool -- as you will see when you try it.   This page is going to verify the availability of PHP on your server, and provide you with a lot of information about the PHP itself and the server it's running on.  Here's the code:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php phpinfo(); ?>
</body>
</html>
That's it!  Small, but then so are diamonds!  Let's break this little gem down.
Almost all of this is instantly recognizable to you as plain old HTML code -- all except that one curious line in the middle:   <?php phpinfo(); ?>   That's the PHP code.
A PHP code snippet is surrounded by an opening and a closing delimiter.  The opening delimiter is <?php and the closing is ?>  the code in between is PHP code.  In this example we are calling the PHP function "phpinfo".  The function name is followed by opening and closing parentheses which surround any parameters being passed to the function -- in this case there are none.  The function call is terminated with a semicolon.
Go ahead and create a file containing this code -- the file name should end with a .php suffix -- send it up to your server and pull it up in your web browser just like you would any other web page.  you should see lots of interesting info about the server system and PHP itself.  Take a look at the source of the resulting page (View Source).  You'll notice that the HTML from your original page is still there, but the PHP code has been replaced by all the information you see.  That's basically how the engine works.  The PHP code you write is replace in the resulting web page by the results of running the PHP code.  Nobody gets to see your actual PHP code -- only the result of it running.  That's significant, as I imagine you already realize!
For a little further note about PHP syntax; the code snippet above, contained between the beginning and ending delimiters is similar to HTML tags in appearance.  It is in fact called a PHP tag.  There are four forms of PHP tags.  The one we've used here is called an XML style tag and is the one we will continue to use throughout this tutorial.  It is the most common form. another style is called script style and will also look somewhat familiar to you if you have written any other script code such as JavaScript.  This example is exactly equivalent to our snippet above:
<script language=php>
phpinfo():
</script>
The other two forms are called the short style of tag and the ASP style.  Both of these require special settings in the PHP configuration files.  They are less commonly used and I will only provide this one example of each in this series.  Here is the same code in the Short style and ASP style, respectively:
<? phpinfo(); ?>
<% phpinfo(); %>
As I previously mentioned, each statement in your PHP code is ended with a semicolon.  Leaving this semicolon off is a common syntax error and when something isn't working the way you expect it to should be one of the first things you check.
Whitespace (spaces, tabs and newlines) are ignored in the syntax of PHP.  These three examples are completely equivalent:
<?php phpinfo(); ?>
<?php     phpinfo();     ?>
<?php
phpinfo();
?>
As in all forms of programming, adding comments to you code is a very useful idea.  Make your code self-documenting and you will love yourself later when you come back to if after having forgotten why you did things a certain way!  You can also add comments to you code in a variety of ways similar to some other language syntaxes.  You can use /* multi-line comment here */ like in C, or add comments to individual lines like in C++ or shell scripts with // and # respectively.  Here are some examples:
<?php
phpinfo();
/*  This is a
     multi-line comment following
     the C language style of comments.
*/
?>
<?php  // this is a C++ style comment on the one code line
phpinfo();
?>
<?php  # this is a shell script style comment on the one code line
phpinfo();
?>