oefening 3
Het script maakt op alle dagen behalve zondag een backup van de folder /var/log/ in de vorm van een tar.gz-bestand met als naam backup_dagmaandjaar.tar.gz
#!/usr/bin/php5
<?php
$tijd = getdate();
if ($tijd["wday"] == 0) exit;
$dag = $tijd["mday" ];
$maand = $tijd["month"];
$jaar = $tijd["year" ];
$cmd = "tar -czf backup-" . $jaar
. "_" . $maand . "_" . $dag
. ".tar /var/log";
system($cmd);
?>
Oefening 4
Om de minuut test het script of het bestand met de opgegeven naam bestaat. Van zodra het bestand bestaat wordt de melding "naam_bestand now exists" getoond en wordt het script gestopt.
#!/usr/bin/php5
<?php
if ($argc < 2) exit;
for (;;) {
if(file_exists($argv[1])) {
echo "Hoera\n";
exit;
}
sleep(60);
}
?>
Oefening 5
Maak een menu met volgende keuzemogelijkheden:
1) Kalender
2) Backup
3) Manpage
4) Einde
De menu-opdrachten voeren het volgende uit:
Kalender
De kalender van het huidige jaar wordt pagina per pagina getoond (verder gaan met enter).
#!/usr/bin/php5
<?php
for (;;) {
system("clear");
echo "1. kalender\n";
echo "2. backup\n";
echo "3. manpage\n";
echo "4. exit\n";
$input = readline("maak een keuze: ");
if ($input == "1") kalender();
if ($input == "2") backup();
if ($input == "3") manpage();
if ($input == "4") exit;
}
function kalender() {
for ($maand = 1; $maand < 13; $maand++) {
system("clear");
system("cal -m $maand");
readline("druk enter om verder te gaan");
}
}
function backup() {
for (;;) {
$filename = readline("De naam van je backup: ");
$dir = readline("Directory voor backup: ");
if ((!empty($filename)) && (!empty($dir))) break;
}
if (file_exists($dir)) {
$command = "tar -czf backup-" . $filename
. ".tgz " . $dir . " > /dev/null";
system($command);
} else {
echo("De map bestaat niet. \n");
}
}
function manpage() {
$command = readline("Kies een commando: ");
system("man " . $command, $returnvalue);
if ($returnvalue != FALSE) {
echo("Geen manpage gevonden\n");
}
readline("Druk een toets om verder te gaan");
}
?>
Oefening 6
Het bestand namen.txt bevat een aantal namen. Maak een scriptje dat als argument op de commandolijn een naam heeft.
Indien de naam gevonden wordt in het bestand namen.txt:
Er wordt op het scherm een alfabetische lijst met deze namen afgedrukt.
Na deze lijst wordt er getoond hoeveel namen dit zijn. Geef deze melding de volgende vorm: "Gevonden: 3" -> indien er drie namen gevonden werden.
Indien de naam niet gevonden werd, geef je de melding "Niets gevonden".
#!/usr/bin/php5
<?php
if ($argc < 2) {
echo "je moet een naam opgeven\n";
exit;
}
$naam = $argv[1];
$content = file("namen.txt", FILE_IGNORE_NEW_LINES);
sort($content);
if (in_array($naam, $content)) {
foreach($content as $line) {
echo $line . "\n";
}
echo "Het bestand bevat " . count($content) . " namen.\n";
}
?>
User-defined functions
A function may be defined using syntax such as the following:
Example #1 Pseudo code to demonstrate function uses
<?php
function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
?>
Any valid PHP code may appear inside a function, even other functions and class definitions.
Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
See also the Userland Naming Guide.
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.
Example #2 Conditional functions
<?php
$makefoo = true;
/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
bar();
if ($makefoo) {
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}
/* Now we can safely call foo()
since $makefoo evaluated to true */
if ($makefoo) foo();
function bar()
{
echo "I exist immediately upon program start.\n";
}
?>
Example #3 Functions within functions
<?php
function foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
foo();
/* Now we can call bar(),
foo()'s processing has
made it accessible. */
bar();
?>
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
Both variable number of arguments and default arguments are supported in functions. See also the function references for func_num_args(), func_get_arg(), and func_get_args() for more information.
It is possible to call recursive functions in PHP. However avoid recursive function/method calls with over 100-200 recursion levels as it can smash the stack and cause a termination of the current script.
Example #4 Recursive functions
<?php
function recursion($a)
{
if ($a < 20) {
echo "$a\n";
recursion($a + 1);
}
}
?>
