Search This Blog

Sunday, May 24, 2009

PHP

PHP

What is PHP?
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

PHP is now officially known as PHP: HyperText Preprocessor. It is a server-side scripting language usually written in an HTML context. Unlike an ordinary HTML page, a PHP script is not sent directly to a client by the server; instead, it is parsed by the PHP binary or module. HTML elements in the script are left alone, but PHP code is interpreted and executed. PHP code in a script can query databases, create images, read and write files, talk to remote servers— the possibilities are endless.

The output from PHP code is combined with the HTML in the script and the result sent to the user.

PHP was created for the web - PHP started as a collection of C applications that
the author wrote for use on his own web site. It evolved into a server-side
scripting language built for building logic into web pages. This
single-mindedness in design has resulted in a language that is both flexible and
streamlined for use on the web.

Finally, it is OPEN SOURCE.

Popularity:

  • According to NetCraft (http://www.netcraft.com), PHP was running on more than 1 million hosts in November 1999. As of February 2000, that figure had already risen to
    1.4 million hosts.
  • According to E-Soft, PHP is the most popular Apache module available, beating even ModPerl.
  • Visit http://www.php.net/usage.php to check how popular PHP is!

* Learning Curve:
- of both the languages and the framework/tools
- Assuming that the developer is only familiar with a basic language like C / C++ that is taught in most academia

PHP's syntax is fairly like C.
- Function calls are "function_name()"
- Lines end in semicolons
- Whitespace is mostly insignificant
- "if", "for", "while" and "switch" syntax is pretty much the same
- Most of the mathematical, boolean, assignment and comparison operators are the same

The biggest difference in syntax is that variables in PHP always begin with a dollar sign, e.g. $foo.
However, PHP is a much higher level language than C. You rarely need to worry about casting variables to a different type, and never have to deal with pointers. Strings are a basic data type in PHP, and don't need to be treated as an array of characters. Indeed, there isn't a character data type -- characters are just strings with length 1.

PHP has a lot more functions built in to the language, compared to C where a lot of functionality (e.g. database connectivity, regular expressions, networking functions) needs to be imported through libraries.

PHP4 has a certain amount of OO support, and PHP 5 has almost as much OO support as Java does.

The learning curve for PHP is very fast.

http://php.net/quickref.php has information about all PHP functions. The docs have various examples and user submitted examples.

The only concern is understanding the nuances of HTML & CSS.
Things to focus on from the start:
- Understand HTML forms
- Understand how PHP receives information (superglobal $_GET[] and $_POST[]) from the forms.

* IDE / Editor Support:
PHP code can be written in ANY text editor, even simple editors like notepad. But for faster developments, IDEs are a better option.

IDEs usually provide features like:
  • Syntax highlighting: Good syntax highlighting improves code readability a lot.
  • Code completion: Automatic code suggestions can help the developer avoid having to type so much. If it supports custom classes and phpDoc, it can even save you from having to read project documentation. Good code completion can also prevent typos.
  • Navigation: One of the most boring things is trying to find where a certain variable has been defined or used. Some good IDEs can help with “GoTo” actions, like go to definition.
  • Errors and warnings highlighting: On-the-fly syntax checking can prevent various typos and common programming mistakes.
  • Debugging: Debugging is not so critical in PHP because you can add echos or use something like FirePHP without even having to recompile your code. But for complex applications in which you need to add echo after each line to see what’s going on, debugging can save you hours.
  • Client-side features: Using PHP alone is very rare. CSS and JavaScript are almost always somewhere in your application. So, good code completion, highlighting, navigation and perhaps some refactoring would be just as beneficial for the other languages and technologies you use in conjunction with PHP.
How To Choose A Good One?
Every IDE provides a lot of features. Some of those features are very useful, some are not. Here are some guidelines to follow to narrow down the one for you:
  • Try free ones first. Their feature set may be enough for you, and you wouldn’t need to pay for a license.
  • First, make sure the features you want are ones you really need. If they are, check that they work properly in your IDE of choice.
  • If you find one IDE that fits well but is missing one or two features, try specialized tools.
  • Once you choose an IDE, play with it for a week before implementing it in a big project. You may find your current working habits are too strong to allow you to feel comfortable with it.

You can take a look of comparisions of various IDEs at:
* Development time:
- Compiled languages usually involve a "build" step that can slow down development time.
Because PHP allows the separation of HTML code from scripted elements, there is a significant decrease in development time on many projects. In many instances, the coding stage of a project can be separated from the design and build stages. Not only can this make life easier for a programmer, it also can remove obstacles that stand in the way of effective and flexible design.

- Amount of code needed to achieve same functionality could be different for different frameworks/languages
PHP provides many basic functions allowing programmers to just focus on the logic rather than implementing minor functions. It has a HUGE amount of useful functions (graphic, texts, database, ports,
mailing, encryption and so on).
Also, PHP has frameworks like PEAR and PECL which provide various libraries for better and faster development.

The only concern is for developers who come from .NET background. They are used to drag-drop UI elements, while PHP does not have a built in support for direct UI elements. But since PHP supports OOPs, classes can be written for UI elements which are required very often.

* Library/Framework support for:
- Authentication / Authorization:
PHP has a very good framework and distribution system for reusable PHP components called PEAR - PHP Extension and Application Repository. PEAR has various authentication and authorization libraries.
There are also lot of opensource user submitted libraries which could be selected based on specific requirements.

- ORM / DB support (connection pooling, caching, etc):
PHP does not support connection pooling. The problem is that traditionnaly, PHP is used for “read-more, write-less”
kind of application. Meaning MySQL/MyISAM whithout transaction support. In this
case, the connection cost is low and isn't noticable.

PHP does support persistent connections. Persistent connections are links that do not close when the execution of the script ends. When a persistent connection is requested, PHP checks if there's
already an identical persistent connection (that remained open from earlier) -
and if it exists, it uses it. If it does not exist, it creates the link.

  1. ORM:
  2. Database Extensions(PHP has built in support for various databases):


- MVC:
Top 10 PHP MVC Frameworks based on users reviews:
  1. Symfony: Based on Mojavi and inspired by Rails
  2. Mojavi: The first MVC framework I fell in love with
  3. CakePHP: Inspired by Rails PHP4/5
  4. PHPOnTrax: a Rails port - PHP5 Only
  5. Prado: The winner of Zend coding contest
  6. Studs: A Java-Struts port to PHP
  7. Phrame: A Java-Struts port
  8. Achievo: A good RAD framework
  9. WACT: Web Application Component Toolkit
  10. Ambivalence: A Java-Maverick Port

- AJAX / RPC:
  • Sajax: Sajax is an open source tool to make programming websites using the Ajax framework — also known as XMLHTTPRequest or remote scripting — as easy as possible. Sajax makes it easy to call PHP, Perl or Python functions from your webpages via JavaScript without performing a browser refresh. The toolkit does 99% of the work for you so you have no excuse to not use it.
  • Xajax: xajax is an open source PHP class library that allows you to easily create powerful, web-based, Ajax applications using HTML, CSS, JavaScript, and PHP. Applications developed with xajax can asynchronously call server-side PHP functions and update content without reloading the page.
  • Cake PHP: Cake is a rapid development framework for PHP which uses commonly known design patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.
  • Saja: Saja is a lightweight, open-source AJAX scripting engine for PHP4/5, with optional secured data transfer. It is designed for the speedy creation of simple, secure, and maintainable AJAX applications, without the need to write any JavaScript.
  • Ajason: PHP 5 library and JavaScript client for AJAX. Fetch data asynchronously and develop interactive GUI-like Web applications. Call PHP functions and object methods from JavaScript and exchange even complex data types between client and server.

A list of various other AJAX libraries can be found at http://ajaxpatterns.org/PHP_Ajax_Frameworks
- Inversion of Control
- Aspect oriented programming

* Performance:
- CPU and Mem requirements
- Multi threading support
- involves app boot/load time ( Time needed to start the app ) too.

PHP by its nature is one of the fastest web scripting languages available, having been more or less written for mod_php, the Apache module used for most PHP installations. Unlike Java, PHP is sufficiently dynamic to run literally as lightweight as you require - for example, you could write a web service in just ten lines of code that could easily handle millions of hits a day, as opposed to the overhead of pulling in a significant chunk of enterprise-sized libraries
that you probably don't need.

Because of the powerful Zend engine, PHP compares well with ASP in benchmark tests, beating it in some tests. Compiled PHP leaves ASP far behind.

The syntax is based on C - Being based on C makes it easier for Java, C, even Javascript programmers to pick up.
One feature of the language where it breaks from the C syntax is it’s object-oriented nature. It supports the creation of custom classes and allowing other classes to inherit from your custom classes. Future releases should futher enhance it’s object-oriented capabilities.PHP is much
faster than CGI - It’s not as fast as JSP’s, which are compiled, but it is faster and uses less system resources than CGI programs do.

PHP code run faster because there is no overhead of communicating with different COM objects

* Scalability:
PHP is highly scalable. Here are a few links which support this:
* Debugging ( Server side ):

* Security:
http://www.php.net/manual/en/security.php

PHP is a powerful language and the interpreter, whether included in a web server as a module or executed as a separate CGI binary, is able to access files, execute commands and open network connections on the server. These properties make anything run on a web server insecure by default. PHP is designed specifically to be a more secure language for writing CGI programs than Perl or C, and with correct selection of compile-time and runtime configuration options, and proper coding practices, it can give you exactly the combination of freedom and security you need.

As there are many different ways of utilizing PHP, there are many configuration options controlling its behaviour. A large selection of options guarantees you can use PHP for a lot of purposes, but it also means there are combinations of these options and server configurations that result in an insecure setup.

The configuration flexibility of PHP is equally rivalled by the code flexibility. PHP can be used to build complete server applications, with all the power of a shell user, or it can be used for simple server-side includes with little risk in a tightly controlled environment. How you build that environment, and how secure it is, is largely up to the PHP developer.

* Code Maintenance:
- Object Oriented / Functional / Procedural - Statically/Dynamically typed
Low maintenance and development cost.
Dynamically typed.
Supports both functional as well as OOPs. So easy code maintainability.

* Testing support:
- Unit , Performance, stress testing,etc
  • Amock
    • Description: Amock is a mock object library written in PHP 5, inspired by EasyMock. Mock objects for classes or interfaces are generated on the fly using a source code generator.
    • Requirement: POSIX
  • izh_test
    • Description: izh_test is a xUnit-like framework which allows users to test php pages using the console version of php, uses file compare for checking results of tests, and can test session state content and db state content too
    • Requirement: Windows
  • PHP Assertion Unit Framework
    • Description: Unit testing framework based on assertions which helps PHP developers test their code. Failing assertions about the program state are tracked in a Reporter window of a DOM-compliant browser such as IE5+, Mozilla, Netscape 6+, etc.
    • Requirement: tbc
  • PHPUnit
    • Description: Unit testing framework for PHP based on the "JUnit" framework for Java. PHPUnit is a family of PEAR packages (PHPUnit2 for PHP 5, PHPUnit for PHP 4) that supports the development of object-oriented PHP applications using the
      concepts and methods of Agile Software Development, Extreme Programming, Test-Driven Development and Design-by-Contract Development by providing an elegant and robust framework for the creation, execution and analysis of Unit Tests.
    • Requirement: OS Independent, PHP
  • SimpleTest
    • Description: Unit testing, web testing and mock objects framework for PHP. Additional features are generation of server stubs, integration of PhpUnit and PEAR test cases, on-line tutorials and documentation. The web testing won't be fully
      finished until version 1.0, but the other functionality is stable.
    • Requirement: PHP
  • Spike PHPCheckstyle
    • Description: Spike PHPCheckstyle is an open-source tool that helps PHP programmers adhere to certain coding conventions. The tools checks the input PHP source code and reports any deviations from the coding convention.
    • Requirement: PHP 5.0 and newer
  • Spike PHPCoverage
    • Description: Spike PHPCoverage is an open-source tool for measuring and reporting code coverage provided by the test suite of a PHP application. Spike PHPCoverage can instrument and record the line coverage information for any PHP script at runtime.
    • Requirement: PHP
  • Testilence
    • Description: Testilence is a unit-testing library for programs written in PHP 5. Although it is similar to other JUnit-inspired unit testing libraries, Testilence is
      written with an emphasis on what is useful, not what is usual. That said, most
      clever ways of doing useful things for unit test authors and users have already
      been discovered. Testilence combines original ideas with the best features found
      in different unit-testing toolkits into a coherent set.
    • Requirement: POSIX

* Language Features:
- Object Oriented / Functional - Statically/Dynamically typed
  • It has a HUGE amount of useful functions (graphic, texts, database, ports, mailing, encryption, regular expressions, XML parsing, cryptography and so on). You can compile in different libraries that allow you to manipulate graphics.
  • It’s cross-language - What does that mean? It’s a term I
    coined to show that PHP can use objects in languages other than PHP. It can
    call Windows COM objects and Java objects, making it easier to separate the
    business logic from the user interface. The PHP team is
    even working on letting it call .NET assemblies.
  • Dynamically typed.
  • Supports both functional as well as OOps.
  • Ability to embed into HTML code
* Deployment:
- Dev / Test / Production node deployments
- Hot deployment supported ?
PHP is designed to run on many operating systems and to cooperate with many servers and databases. You can build for a UNIX environment and shift your work to NT without a problem. You can test a project with Personal Web Server and install it on a UNIX system running on PHP as an Apache module.

* Web server support:
Compatible with servers IIS and Apache

* OS level support:
- e.g Even though there is Mono, newer Microsoft technologies may not available on other OSs.
- newer frameworks might not have good performance/support for all OSs
It’s cross-platform - PHP runs on UNIX, Linux, Mac OSX, and Windows.

* Community / Eco system
:
- around the framework & language
- size & helpfulness

http://www.php.net/support.php
  • It is maintained by an excellent team at www.php.net.
  • Bugs are usually resolved in days.
  • The Php community is adding features all the time, experienced programmers report problems, give solutions to difficult pieces of code and so on.
  • Another reason for choosing PHP over other languages has to do with the popularity of the languages. Thousands of web pages are running PHP and it has a dedicated following. If you choose to learn PHP, you can be assured that you can find enough resources to answer any question you have and in most cases you’ll find that someone has already written code that does exactly what you need and is giving it away for free.
SCRATCH-PAD:

http://bogambilya.asti.dost.gov.ph/manual/en/faq.languages.php

Taking a look at PHP 6

While most web hosts are still in the PHP 4 era, the PHP developers are
already planning and working on PHP 6. Lets have a look at whats been keeping
them busy.

Unicode support

When youre creating a website, you hardly have to think about the character
encoding. You only have to decide how you tell the user agent what
encoding youre using, but with a little help of Apaches .htaccess file [slashdot.org], you only have to make that
decision once. However, if youre building an application, the character encoding
might become a problem. Thats where PHPs new Unicode support comes in handy.
With its support, PHP can automatically encode and decode the in and output of
the script making sure both the database and the user agent receive the encoding
they need without the need of any extra functions for the encoding
conversion.

The big cleanup

PHP is already being used for a long time, creating a big user base, but also
a lot of bad habits. Bad habits often result in slow scripts or even security
holes. But these bad habits are not always the cause of the developer. Of
course, he (lets just assume were dealing with a stereotype developer here for
simplicity's sake) is the one whos using it in his application, but sometimes
the developer is not even aware hes using it.


Im, of course, talking about the register_globals [php.net], magic_quotes [php.net] and safe_mode [php.net] functions. These three functions
are hell for every PHP programmer so Im sure everyone will be happy to hear that
these functions will disappear in PHP 6.


In other related cleanup news, register_long_arrays and the long
versions of the super globals like $HTTP_COOKIE_VARS are also gone in
PHP 6. Same goes for zend.ze1_compatibility_mode which dealt with the
backwards compatibility of PHP 5 classes.

Alternative PHP Cache

Caching is a very good way to improve the performance of an application.
Thats why there was a large demand for a good opcode cache in the default
distribution of PHP. And when theres a demand, theres probably also a person or
a group to meet that demand. The result is APC [php.net]:
Alternative PHP Cache. Of course, APC was already available a long time ago
(01-07-2003), but the PHP developers have decided to include this extension in
the core as the default caching framework.

OO Functionality

The improved OO model was probably the biggest improvement to PHP in version
5.0. PHP 6 tries to improve this even further by adding namespaces. If youre
familiar with XMLs namespaces or maybe C++, you will probably have an idea of
how namespaces work. If not: Namespaces can group variables, functions or
objects under a certain name. This allows the developer to use the same name for
a variable, function or object multiple times. In case youd like to learn more
about the possibilities of namespaces, I find this C++
tutorial
[cplusplus.com] about namespaces quite useful.

Changes to the
extensions

PHP is basically a collection of extensions which are all put together to
form what we have now. However, these extensions change and so does the
collection. Take, for instance, the XML Writer extension. A great extension to
write XML files. Its brother, XML Reader, was already added and enabled in the
core distribution in PHP 5.1, and now XML Writer will follow its example in PHP
6, forming a great duo to easily work with XML files.


Another change in the core distribution is the removal of the ereg regular
expressions library which is going to be made an extension. ereg is currently
used as an competitor of PCRE (preg_match, etc.), but apparently its
causing some problems. Therefore, the developers decided to remove it from the
core and make it an extension.


Yet another change we see is the Fileinfo extension which will be dealing
with media type detection. At the moment, media type detection isnt very good in
PHP. We have the mime_magic [php.net] extension, but that isnt really reliable.
So in PHP 6, the Fileinfo extension will take over mime_magics place and become
part of the core while mime_magic will be moved from the core and made into an
extension.


So weve seen quite some interesting changes so far. To me, PHP 6 doesnt
really look like a massive feature update, but more as a big cleanup while
improving a lot of existing functions along the way. And I think thats good! Im
working with PHP on an almost daily basis and looking at the things noted above,
Im only seeing improvements. So hopefully developers of popular applications
like phpBB
[phpbb.com] will make their applications work properly on PHP 5 making it easier
for web hosts to switch their servers to PHP 5. But at the current state of PHP
5 support, I dont see PHP 6 becoming widely adopted if it were released today.
So hopefully this will change by the time PHP 6 will be released.


For all the planned changes to PHP 6 (including the ones I noted above),
check out these meeting notes
[php.net].


http://www.corephp.co.uk/archives/19-Prepare-for-PHP-6.html


No comments: