php_cookbook-13-20
php_cookbook-13-20
Figure 1.2: XAMPP window after a successful installation with Apache and MySQL enabled
• Place your web project inside the htdocs directory. In the common case, if you installed XAMPP directly inside the C: drive
of your PC, the path to this folder would be: C:xampphtdocs
To test the services are up and running you can just enter localhost in your address bar and expect the welcoming page.
PHP Programming Cookbook 5 / 63
The aim of this section is to introduce the general syntax you should expect in PHP.
There are four ways the PHP parser engine can differentiate PHP code in a webpage:
This is the most popular and effective PHP tag style and looks like this:
<?php...?>
• Short-open Tags
These are the shortest option, but they might need a bit of configuration, and you might either choose the --enable-short-
tags configuration option when building PHP, or set the short_open_tag setting in your php.ini file.
<?...?>
• ASP-like Tags
In order to use ASP-like tags, you’ll need to set the configuration option in the php.ini file:
<%...%>
You can define a new script with an attribute language like so:
<script language="PHP">...</script>
Just like other languages, there are several ways to comment PHP code. Let’s have a look at the most useful ones:
Use # to write single-line comments
<?
# this is a comment in PHP, a single line comment
?>
The result of the above statements would be the same: "Hello World". But why are there three different ways to output?
Any type of variable in PHP starts with a leading dollar sign ($) and is assigned a variable type using the = (equals) sign. The
value of a variable is the value of its most recent assignment. In PHP, variables do not need to be declared before assignment.
The main data types used to construct variables are:
The following snippet shows all of these data types declared as variables:
<?
$intNum = 472;
$doubleNum = 29.3;
$boolean = true;
$string = ’Web Code Geeks’;
$array = array("Pineapple", "Grapefruit", "Banana");
Conditional statements are used to execute different code based on different conditions.
The If statement
The if statement executes a piece of code if a condition is true. The syntax is:
if (condition) {
// code to be executed in case the condition is true
}
Because the condition is false, the result in this case would be:
You are an adult
elseif (condition2) {
// code to be executed in case condition2 is true
}
else {
// code to be executed in case all conditions are false
}
In PHP, just like any other programming language, loops are used to execute the same code block for a specified number of times.
Except for the common loop types (for, while, do. . . while), PHP also support foreach loops, which is not only specific to PHP.
Languages like Javascript and C# already use foreach loops. Let’s have a closer look at how each of the loop types works.
The for loop
The for loop is used when the programmer knows in advance how many times the block of code should be executed. This is
the most common type of loop encountered in almost every programming language.
for (initialization; condition; step){
// executable code
}
The result of this code snippet would be just the same as before:
This is loop number 0
This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
To have a clearer idea of the flow of these two loops, look at the graphic below:
The do...while loop is used when we want to execute a block of code at least once and then as long as a test expression is
true.
do {
// executable code
}
while (condition);
This time the first loop number would be 1, because the first echo was executed only after variable incrementation:
This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
This is loop number 5
This time the first loop number would be 1, because the first echo was executed only after variable incrementation:
Element is a
Element is b
Element is c
Element is d
Element is e
Arrays are used to store multiple values in a single variable. A simple example of an array in PHP would be:
<?php
$languages = array("JS", "PHP", "ASP", "Java");
?>
PHP Programming Cookbook 11 / 63
Array elements are accessed like this: $arrayName[positionIndex]. For the above example we could access "PHP" this
way: $languages[1]. Position index is 1 because in programming languages the first element is always element 0. So, PHP
would be 1 in this case.
There are three types of arrays in PHP:
Indexed Arrays
We can create these kind of arrays in two ways shown below:
<?php
$names = array("Fabio", "Klevi", "John");
?>
<?php
// this is a rather manual way of doing it
$names[0] = "Fabio";
$names[1] = "Klevi";
$names[2] = "John";
?>
// RESULT
My friends are Fabio, Klevi and John
<?php
// this is a rather manual way of doing it
$namesAge[’Fabio’] = "20";
$namesAge[’Klevi’] = "18";
$namesAge[’John’] = "43";
?>