Cursussen/Courses Codesnippets     Top 
PHP - Language parts


1. The PHP-code
1. The PHP code is inserted between the HTML lines. You then provide a start tag (<?php) and an end tag (?>). You have to end all lines with a semicolon!
2. You can also put code between HTML tags. You can then omit the semicolon. You can also use the abbreviated notation (e.g. <?=$color?>)
3. The page consists of only PHP code and the HTML code is generated with an output command echo or print.
The designation “\n” creates a new line in the HTML source code.
<?php
	require "navigatie.php";
	$kleur = "w3-blue";
?>
<link rel="stylesheet" type="text/css" href="w3.css">
<div class="<?php echo $kleur ?>">Een witte tekst op een blauwe achtergrond.</div>

<?php
echo "<p>Een eerste paragraaf.</p>\n";


2. Comment
You can comment on a single line by starting the line with the two slashes (//) or the pound sign (#). The text after these characters is ignored by the PHP parser.
You can type multi-line comments between the characters (/*) and (*/).
You can comment at the end of a line of PHP code, but then there is a chance that if the line is long, you will no longer see the comment.
<?php
	// testprogramma
/*
	toepassing: test
	bestand: test.php
	nota: 
*/


3. Variables
A variable in PHP must start with a $ sign and the first character must be a letter (A-Z, a-z) or a hyphen (_). With long names, the words are usually separated by a hyphen. The variable names are also case sensitive; $Var is not the same as $var. That is why most programs only use lowercase letters.
You can also capitalize each new word in a variable name. Make sure you apply the naming consistently!
$afbeelding_url = "afbeeldingen\afbeelding_01.jpg";
$aantal_paginas = ceil($aantal_records / $records_per_pagina);


4. Variable Types
Variables do not need to be declared in advance. They are assigned a type on first use.
For example, you can assign an integer, a decimal number or a string to a variable. A "boolean" variable can be assigned the value True or False.
When assigning a value to a string, you can use the single quotes (') or the double quotes ("). Always use the double quotes because then the escape characters (e.g. \n, \t, \\ , ...) are interpreted correctly and the contents of the variables are displayed! More examples can be found in the PHP manual.
$teller = 1;
$naam = "Bill Gates";
$percent = 0.5;

$actief = true;
if ($actief == true) {
	echo "de record wordt getoond.";
}


5. Variable scope
A variable is known within the same PHP page and in the pages that are called after it with the include or require commands. The variables used in a function are only known within that function.
The use of global variables is no longer possible from the PHP version 4.2.0 because the configuration setting “register_globals” has been given the value “Off” by default. You can change this setting, but it is not recommended. By the way, all hosting providers set this setting to “Off”.
An exception to this rule are the “superglobals”. These are variables that have a global scope (eg $_POST, $_GET, $_REQUEST, ...).
<?php
if (isset($_GET['id'])) {
	echo "De record id is " . $_GET['id'];
}
$naam = htmlspecialchars($_POST["naam"]);


6. Expressions
If you give a variable a value (eg $a = 5;) then you use an expression. You also use an operator (eg =).
There are several operators: arithmetic, assignment, bit, comparison, increment, decrement, logical, string, array, and type operators. You can find examples of each operator type in the PHP manual.
Regular expressions.
If you want to check a certain string for validity in the programs you are going to write (e.g. a valid E-mail address or a URL) then you use a pattern. You pass this pattern and the string to be checked as arguments to the function ereg(“pattern”, “string”) or the function eregi(“pattern”, “string”). The ereg() function distinguishes between uppercase and lowercase letters. The eregi() function does not. The ereg() and eregi() functions can no longer be used in PHP version 7. Alternatively, you can use preg_match().
More information can of course also be found in the PHP manual. A search in the manual will take you to the correct description of the function.
An example of an E-mail check:
<?php
$input = "mijn.email@host";
echo $input . "<br>";
$result = preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $input );
if ($result) {
	echo "geldig e-mail adres<br>";
} else {
	echo "ONgeldig e-mail adres<br>";
}