|Lessons||PHP Application Functions|
The die() function outputs a given message and terminates the PHP script. Its syntax follows:
We often use the following code to print a message and stop the execution of our script upon the failure to connect to the database:
$connection = mysql_connect("servername", "user", "password") or die ("Database Error! Can't connect to database.");
The exit statement is used to terminate the execution of the current PHP script at the point where the exit statement is used.
sleep() and usleep()
The sleep() and usleep() functions, when called, put a pause, or a delay, at a given point in the execution of our PHP code. The syntax of these functions follows:
The only difference between sleep() and usleep() is that the given wait period for sleep() is in seconds, and the wait period for usleep() is in microseconds.
Program Execution Functions
PHP’s built-in program execution functions are used to execute programs residing on our system, such as encryption programs, third-party image manipulation programs, and so forth. It is important for all program execution functions that the PHP user must have proper permissions to execute the given program(s).
The exec() function is used to execute an external program as follows:
exec(command, [array], [return_var]);
If an array is specified, the output of the exec() function will append to the array. If return_var is specified, it will be assigned a value of the program’s return status. The syntax of the exec() function follows:
$command = "ping www.pickatutorial.com";
exec($command, $result, $rval);
passthru() Just like the exec() function, the passthru() function is also used to execute an external program. The only difference between the two is that passthru() returns the raw output of the action. The syntax follows:
The system() function is also used to execute an external program and display output as the command is being executed. The sysntax follows:
If return_var is specified, it will be assigned a value of the program's return status.