Notes, comments

include, require

In many of our web pages we had few lines like:

mysql_connect('mysql.webzdarma.cz','zpp','[password]')
 or die('Error: '.mysql_error() );
mysql_select_db('zpp');

Those lines are always the same, and they content important constant, which should be changed, if you change the SQL server. You can write it to separate php file, for example with the name "connect.php", and to each of your scripts you can write only the include directive :

<?php
include "connect.php";

$result=mysql_query('....

The example has been abbreviated. The new file, "connect.php", should start with the "<?php " direstive and ends with "?>" .

This is strongly recommended to keep all files on the same server. If you include a file from the other server using url, the file cannot have the php extension, or it will be interpreted on that server.

Typical extension of an included file is the ".php", also ".inc" is often used.

Lines from the included file will be really interpreted as being included to this point of the main php script. Only in the case of an error, the line number and name of included file will be reported.

You can imagine, that you will include file, which itself contain include. It will be correctly nested. For more complicated application, you can mistakenly include for example some definition twice. For this, you can use the include_once directive, which will check this.

If the included file doesn't exist, the server will announce "Warning", but script will continue. If it should fail in this case, you can use the require, or require_once directive.

Execute php locally

php programs are texts. But for many tasks, it could be useful to create simple programs in php (many libraries, databases, image generation... ). To execute php program from the console (the term application allows to use console), give its name as a parameter of the "php" command:

/var/www/home> php mypgm.php

The page will be generated to the standard output. To catch it, you can use redirection:

/var/www/home> php mypgm.php >/home/myuser/myfile.html

If the php command is not in the path, try /usr/bin/php . If not working, your installation of php module for Apache (Apache2) is not complete.

The same way you can execute php programs regularly, using crontab. Beware directory path - while executing locally, real path is visible; while executing as a web page, the html root directory path, "/var/www/home", is suppressed. Again, it is a good idea, to keep everything in one directory and don't solve path.

Execute external command from php

Although the php has wide spectrum of libraries, not all can be done with php. Typical tasks are processor port manipulation, or device communication. It was done for purpose, because those tasks can be dangerous (for server stability). So, if you like to use a computer for opening gates or executing washmachine, you need to execute an external program (written for example in C++). For example, output to an obsolete parallel port (only for computers, where this port is on a parallel bus, not on the PCIexpress), can be done by:

$mycommand = "/usr/local/bin/parashell 0x378 0xF7";
exec ($mycommand);

On this parallel port, you can have for example a LED.