Saturday, 9 March 2013

Remember that php is not just a web scripting language but can be run as a command line program as well.

Its important for new PHPers to know that php is a scripting language that is used predominately in web development but that you can also write command line programs in PHP as well. You must understand how both of these processes work. A web-server is typically a dumb terminal. You ask it for a web page and it knows how to server the contents of a file. It doesn't know anything about the internals of the file but rather it just knows how to open the two types of files that exist and how to serve them those file types are Text files and binary files. So how does a php file get processed? actually this is quite simple the web-server is still as dumb in the regard of serving two types of files but now it checks the extension of the file in question and passes it to an external program that will return a stream of data which could be binary or text based. 

OK so now our web-server knows how to serve a text file, a binary file and how to serve a data stream form an external program. (note if you created an hello world program in any language you could configure a web-server to call that program when any file with an extension of .hello was requested.)

There are different restrictions when you are working with PHP in these two environments. The first environment is in web development. Most PHP development will be done using a Web Server to serve the page. The Second is when you want to run a script on the command line.

WEBSERVER:::
  1. Most web servers will limit your page execution to a max of 30 seconds. Typically you will have scripts that will execute in 1-3 seconds with 5-10 seconds considered long execution times. The 30 second barrier imposed by most web servers is to stop infinity loops killing your web-server These are typically a web-server setting for all scripting languages not a php specific setting. To lengthen the time you need to reconfigure your web-server. After the time limit the web-server will kill the process. 
  2. A web-server might sandbox the php process and limit what it can do. 
  3. Session information a web-server tracks user requests and ip addresses allowing you to have persistant information across page requests. 
  4. The web-server will tell php extra settings that will appear in the $_SERVER variable that are related to the machine that is requesting the page. 
COMMAND LINE:::
  1. There is not timeout on your scripts as they are now considered to have full reign on the machine based on the permissions of the user that is executing the script. 
  2. The session environment does not exist as there is only a single instance. 
  3. You can write a php script to parse a huge data file if you wanted but perl would probably do that job faster and quicker for you. 
I've often needed to batch process a lot of files and have used php to loop through a directory and create me a bat file that will run each set of commands on each file as needed. I'm sure there were probably beter ways of doing it but none quicker. As I knew PHP and knew I could use the directory functions to build the bat file to execute really quickly