PHP Modes of Use Primer Tutorial

PHP Modes of Use Primer Tutorial

PHP Modes of Use Primer Tutorial

PHP is a very flexible programming language. It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or “surfing the net”). Let’s list three modes of use of PHP below:

  1. Via http transport layer (ie. web browsing, or “surfing the net”) in an Internet mode of use (eg: http://localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser
  2. Via curl in an Internet mode of use (eg. curl http://localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) at Linux or unix or Windows command line to default output (and you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
  3. Via command line PHP in Linux or unix or Windows command line in a command or Intranet mode of use (eg. php in_tra_ter_net.php SERVER_NAME=localhost) at a Linux or unix or Windows command line (where the PHP code has been placed, in our example (ie. it is not a URL, but is a file specification of your (Intranet) server)) to default output (or you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)

Does it matter how the PHP is accessed? Well, yes, it does … sometimes, but not if the code is restricted to a particular mode of usage (but part of what this tutorial is about is to get across how flexible and powerful PHP can be). With the last option above, lots of $_SERVER[] variables are undefined, for example.

  1. $_SERVER[‘HTTP_COOKIE’] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage). Most $_SERVER[] variables are defined, but $argv[] array is not.
  2. $_SERVER[‘HTTP_COOKIE’] is undefined, but a lot of the rest of $_SERVER[] variables are as above.
  3. Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[‘SCRIPT_FILENAME’] which may help you with Intranet application usage. You cannot enter $_GET[] arguments (on the command line (ie. you will get a syntax error if you try)) so these are undefined, but $argv[] array can be used in that awkward way shown, so that $argv[1] can be arranged to be a lot like $_SERVER[‘QUERY_STRING’] (ie. the bits after ? in a URL).

So, maybe you have the one mode of use in mind, but you need to think a bit to support all the modes of use, if they are all to have a role with the PHP you write, because, despite these differences it is good to use $_SERVER[] and $_GET[] and $_POST[] and $_SESSION[] and $argv[] and whatever else in terms of variable information PHP offers in terms of attempting to write generic code.

Hope you get something out of today’s discussion of our Mac OS X MAMP Apache local Web server environment example (hence the http://localhost:8888/ you see in the URLs above), but please bear in mind that there would be other modes of use once you think of other client HTML element ways of accessing PHP (all a lot like the “web browsing” of above) or other possibilities within PHP code itself via PHP commands exec and file_get_contents the latter of which can be very useful during Intranet usage where there is no curl available, perhaps. Instead of curl you could use wget as another approach.

You may want to see a live run (web browsing) here at the rjmprogramming.com.au domain or you may want to download the PHP programming source code you could call in_tra_ter_net.php as you require.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *