Cursussen/Courses Codesnippets     Top 
PHP - Functions


1. Own functions
You can define a function with the reserved word “function”. You choose a name and you fill in the statements between the curly brackets.
The function can be called in a program by specifying the function name and two parentheses. You can call a function several times in different places in your program.
You don't have to specify a return value, but if you do then the result of the function can be used as a test value on the condition of a control structure (if, switch) or a loop structure (for, while). The function usually returns a boolean value (return false; or return true;). If the function returns a different value, you should of course test for the expected values ​​(using the comparison operator ==).
A function can also contain one or more arguments. If the statements within the function e.g. If you expect 3 arguments and only 2 arguments are passed when the function is called, then the value of the third argument will be “null” (“nonexistent”, “nothing”). With the PHP function isset($variable) you can check whether a variable exists or not.
As mentioned, the range of the variables is limited to the statements within the function. However, with the global statement you can give a variable a global scope (the variable is the same inside the function as outside it). However, this method is not recommended.
In the function definition you can assign start values ​​to the arguments. This makes these arguments optional when calling the function. If you do enter a different value, this value will be used in the function.


2. PHP functions
You can use built-in PHP functions for a lot of actions that need to be performed in a program. In the PHP manual you will find the overview under “Function reference” or you can use the categories in the appendix “K. Consult Extension Categorization”.
Most PHP textbooks also include lists of key PHP functions. For example, you can find categories for:
• functions for arrays;
• file functions;
• string functions;
• regular expression functions;
• functions for dates, calendars and times;
• E-mail functions;
• functions for MySQL;
• file system functions;
• functions for variable types;
• printing functions;
• functions for HTML;
• random number functions;
• functions for strings;
• URL functions;
• session features;
• functions for various purposes;
• functions for installed libraries;
• …


3. Example
In this example all table names are retrieved from the database 'movies' (= schema name).
The field names, field types and data types are then retrieved from each table.
A function is used each time to retrieve the data.
The results are nicely presented in a table.
<?php
// db_info.php
echo "<link rel='stylesheet' type='text/css' href='w3.css'>";
$schema = "films";
echo "<h3>&nbsp;Database naam: " . $schema . "</h3>";
$tabellen = haal_tabelnamen($schema);
foreach($tabellen as $tabelnaam) {
	echo "<table class='w3-table w3-table-all w3-half'>";
	echo "<tr><td colspan='3' class='w3-blue'>Tabelnaam: " . $tabelnaam . "</td></tr>";
	$veldinfo = haal_velden($schema,$tabelnaam);
	echo "<tr><th>Veldnaam</th><th>Veldtype</th><th>Datatype</th></tr>";
	for($j = 0 ; $j < count($veldinfo[0]) ; $j++) {
		echo "<tr>";
		echo "<td>".$veldinfo[0][$j]."</td>"."<td>".$veldinfo[1][$j]."</td>"."<td>".$veldinfo[2][$j]."</td>";			
		echo "</tr>";
	}
	echo "</table>";
}
return;

function haal_tabelnamen($schema) {
	$schema = str_replace('&amp;','&',$schema);
	$conn = new PDO("mysql:host=localhost;dbname=$schema", "root", "root");
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = :schema";
	$result = $conn->prepare($sql);
	$result->execute(array('schema' => $schema));
	$tabellen = array();
	while($row = $result->fetch(PDO::FETCH_ASSOC)){
		$tabellen[] = iconv('', 'utf-8', $row["table_name"]);                
	}
	return $tabellen;
}
function haal_velden($schema,$tabel) {
	$conn = new PDO("mysql:host=localhost;dbname=$schema", "root", "root");
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$sql = "SELECT column_name,column_type,data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table AND TABLE_SCHEMA = :schema";
	$result = $conn->prepare($sql);
	$result->execute(array('table' => $tabel,'schema' => $schema));
	$velden = array();
	$veldtypes = array();
	$datatypes = array();
	while($row = $result->fetch(PDO::FETCH_ASSOC)){
		$velden[] = iconv('', 'utf-8', $row["column_name"]);                
		$veldtypes[] = $row["column_type"];                
		$datatypes[] = $row["data_type"];                
	}
	return $veldinfo = array($velden,$veldtypes,$datatypes);
}