PHP
Catégorie | Cours |
---|
Cours de Julie Chaumard
To Do
Chapters | Homeworks | Exercises | Weeks | Dates |
---|---|---|---|---|
PHP | 2 | 09/06/2025 | ||
2 | 09/06/2025 | |||
Book - Chapter 11 - P.493
- Connolly, R. and Hoar, R. (2018). Fundamentals of Web Development (2nd ed.). Pearson.
Server-Side development
PHP is a server-side scripting language
PHP code is executed on the server before the page is sent to the browser.
The browser never sees the PHP code, only the generated HTML result.
What is the main difference between client-side and server-side scripts?
The fundamental difference between client and server scripts is that in a client-side script the code is executed on the client browser, whereas in a server-side script, it is executed on the web server. As you saw in Chapter 8, client-side JavaScript code is downloaded to the client and is executed there. The server sends the JavaScript (that the user could look at, if they wished), but you have no guarantee that the script will even execute.
In contrast, server-side source code remains hidden from the client as it is processed on the server. The clients never get to see the code, just the HTML output from the script
Why is server-side code generally more secure than client-side code?
- It is hidden from users: Only the server processes the code, so users cannot see or modify it.
The location of the script also impacts what resources it can access. Server scripts cannot manipulate the HTML or DOM of a page in the client browser as is possible with client scripts. On the other hand, a server script can access resources on the web server whereas the client cannot. Understanding where the scripts reside and what they can access is essential to writing quality web applications.
Web server
A web server has many responsibilities beyond responding to requests for HTML files. These include handling HTTP connections, responding to requests for static and dynamic resources, managing permissions and access for certain resources, encrypting and compressing data, managing multiple domains and URLs, managing database connections, cookies, and state, and uploading and managing files.
Apache is a software that provides a web server. Another one is NGINX.
Apache
Apache runs as a daemon on the server. A daemon is an executing instance of a program (also called a process) that runs in the background, waiting for a specific event that will activate it. As a background process, the Apache daemon (also known by its OS name, httpd) waits for incoming HTTP requests. When a request arrives, Apache then uses modules to determine how to respond to the request.
In Linux, daemons are usually configured to start running when the OS boots and can be manually started and stopped by the root user. Whenever a configuration option is changed (or a server process is hung), you must restart Apache.
PHP
PHP is usually installed as an Apache module (though it can alternately be installed as a CGI binary). The PHP module mod_php5 is sometimes referred to as the SAPI (Server Application Programming Interface) layer since it handles the interaction between the PHP environment and the web server environment.
- In PHP, a SAPI is an interface between the web server (like Apache or NGINX) and the PHP interpreter.
- Usage: It is very specific to the internal workings of PHP.
- Examples of SAPI:
- apache2handler (PHP integrated with Apache)
- cgi-fcgi (CGI mode with FastCGI)
- cli (command line interface, not a web server)
When we will have the serveur web and PHP running we will be able to use this command in a php file to request what is the SAPI used : php_sapi_name();
PHP itself is written in the C programming language and is composed of three main modules:
PHP core. The Core module defines the main features of the PHP environment, including essential functions for variable handling, arrays, strings, classes, math, and other core features.
Extension layer. This module defines functions for interacting with services outside of PHP. This includes libraries for MySQL (and other databases), FTP, SOAP web services, and XML processing, among others.
Zend Engine. This module handles the reading in of a requested PHP file, compiling it, and executing it

Installing Apache, PHP, and MySQL for Local Development
One of the true benefits of the LAMP web development stack is that it can run on almost any computer platform. Similarly, the AMP part of LAMP can run on most operating systems, including Windows and the Mac OS. Thus it is possible to install Apache, PHP, and MySQL on your own computer.
LAMP = Linux + Apache + MySQL + PHP
It is a popular stack for hosting websites like WordPress, Drupal, Joomla
While there are many different ways that one can go about installing this software, you may find that the easiest and quickest way to do so is to use an all-in-one management software that bundles popular tools together. The easyPHP (www.easyphp.org) or XAMPP (www.apachefriends.org) packages or the MAMP for Mac (www.mamp.info) package will install and configure Apache, PHP, and MySQL (or MariaDB, which is the new open-source equivalent replacement for MySQL) using a graphical user interface.
For instance, once the XAMPP package is installed, you can then run the XAMPP control panel (as you can see in this screen capture, we did not install all the components). You may need to click the appropriate Start buttons to launch Apache (and later MySQL). Once Apache has started, any subsequent PHP requests in your browser will need to use the localhost domain (or the equivalent IP address 127.0.0.1),

XAMPP (Windows)
Whatever approach you take to having a web host you are ready to start creating your own PHP pages. If you used the default XAMPP installation location, your PHP files will have to be saved somewhere within the C:\xampp\htdocs
folder.
MAMP (Macos)
- Start the services on MAMP or XAMPP
- locate the folder monitored by the AMP
- XAMPP =
C:\xampp\htdocs\
C:\xampp\apache\conf\httpd.conf
- MAMP = choose it on the control panel or
/Applications/XAMPP/xamppfiles/etc/httpd.conf
- XAMPP =
- Look at the phpinfo
- create a
index.php
file
<?php phpinfo(); ?>
- open the php file on a browser http://localhost:8888/
- or http://localhost/test.php / http://localhost:8888/test.php if you made a test.php file
- create a
- port
- XAMPP Control Panel
- MAMPP setting
- What is a port ?
- A port is like a virtual doorway on a computer connected to a network. Each port allows data to be sent or received for a specific service.
- The IP address is like the address of a building. The port is the apartment number
Example:
Your computer can handle many types of connections at the same time:
- a website,
- a video call,
- a database,
- a file transfer…
Each service uses a different port, so the data reaches the right program.
Common ports
Port Usage 80 HTTP (non-secure websites) 443 HTTPS (secure websites) 3306 MySQL (database) 21 FTP (file transfers) 22 SSH (secure remote connection)
PHP Syntax
The most important fact about PHP is that the programming code can be embedded directly within an HTML file. However, instead of having an .html extension, a PHP file will usually have the extension .php.
PHP programming code must be contained within an opening <?php tag and a matching closing ?>tag in order to differentiate it from the HTML. The programming code within the <?php and the ?> tags is interpreted and executed, while any code outside the tags is echoed directly out to the client.
For pages with only PHP code (i.e., no HTML), the official documentation recommends omitting the closing ?> tag (it potentially improves output buffering).
<? & echo & date()
<?php
$user = "Randy";
?>
<!DOCTYPE html>
<html>
<body>
<h1>Welcome <?php echo $user; ?></h1>
<p>
The server time is
<?php
echo "<strong>";
echo date("H:i:s");
echo "</strong>";
?>
</p>
</body>
</html>
You can see that the time is not good.
We are going to configure the timezone of the server.
php.ini
- XAMPP : XAMPP Control Panel
- Apache / Config / PHP (php.ini)
- MAMP
- PHP Version is the first line oh the phpinfo
<?php phpinfo(); ?>
- /Applications/MAMP/bin/php/php8.2.0/conf/
- Change timezone by opening php.ini file (restart AMP)
date.timezone = Europe/Paris
Comment
// One line
/* multiple
line
*/
Variables
Variables in PHP are dynamically typed, which means that you as a programmer do not have to declare the data type of a variable. Instead the PHP engine makes a best guess as to the intended type based on what it is being assigned. Variables are also loosely typed in that a variable can be assigned different data types over time.
$count = 42;
$user = "Randy";
You should note that in PHP the name of a variable is case sensitive, so $count and $Count are references to two different variables. In PHP, variable names can also contain the underscore character, which is useful for readability reasons.
Data Type | Description |
---|---|
Boolean | A logical true or false value |
Integer | Whole numbers |
Float | Decimal numbers |
String | Letters |
Array | A collection of data of any type (covered in next chapter) |
Object | Instances of classes |
undefined
If you do not assign a value to a variable and simply define its name, it will be undefined. You can check to see whether a variable has been set using the isset()
function, but what's important to realize is that there are no “useful” default values in PHP. Since PHP is loosely typed, you should always define your own default values in initialization.
And defined()
for Constant
Constant
A constant is somewhat similar to a variable, except a constant's value never changes … in other words it stays constant. A constant can be defined anywhere but is typically defined near the top of a PHP file via the define()
function. The define()
function generally takes two parameters: the name of the constant and its value. Notice that once it is defined, it can be referenced without using the $ symbol.
From PHP 5.3+, you can use const
in a oriented-object program
IF … ELSE
if () {
do something
} else {
do another thing
}
if () :
do something
else :
do another thing
endif;
<?php
define("DATABASE_NAME", "DBtest");
const DB_HOST = "localhost";
$user = "Julie";
?>
<!DOCTYPE html>
<html>
<body>
<h1>Welcome <?php if (isset($user)) { echo $user; } else { echo "$user is empty"; } ?></h1>
<p>
The server time is
<?php
echo "<strong>";
echo date("H:i:s");
echo '<br>';
if (defined("DATABASE_NAME")) {
echo "Database name = ", DATABASE_NAME, " to reach your DB";
} else {
echo "DB_HOST is not defined.";
}
echo '<br>';
if (defined("DB_HOST")) :
echo "Database host = " . DB_HOST . " to reach your DB";
else :
echo "DB_HOST is not defined.";
endif;
echo "</strong>";
?>
</p>
</body>
</html>
FOR & ARRAY & count & print_r & input
<?php
define("DATABASE_NAME", "DBtest");
const DB_HOST = "localhost";
$user = "Julie";
?>
<!DOCTYPE html>
<html>
<body>
<h1>Welcome <?php if (isset($user)) { echo $user; } else { echo "$user is empty"; } ?></h1>
<div>
The server time is
<?php
echo "<strong>";
echo date("H:i:s");
echo '<br>';
if (defined("DATABASE_NAME")) {
echo "Database name = ", DATABASE_NAME, " to reach your DB";
} else {
echo "DB_HOST is not defined.";
}
echo '<br>';
if (defined("DB_HOST")) :
echo "Database host = " . DB_HOST . " to reach your DB";
else :
echo "DB_HOST is not defined.";
endif;
echo "</strong>";
?>
</div>
<div>
<?php
// array
$artists = [
1 => "picasso",
2 => "raphael",
3 => "cezanne",
4 => "rembrandt",
5 => "giotto",
"toto" => "painters"
];
echo "List of " . $artists[toto] . " has " . count($artists) . " elements";
echo "<br>";
for ($i = 1; $i <= count($artists); $i++) {
echo $artists[$i];
echo "<br>";
}
echo "<br>";
echo $artists;
echo "<br>";
print_r($artists);
echo "<br>";
// array start with 0
$fruits = ["apple", "banana", "cherry"];
echo "List of fruits has " . count($fruits) . " elements";
echo "<br>";
for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i];
echo "<br>";
}
echo "<br>";
echo $fruits;
echo "<br>";
print_r($fruits);
echo "<h1>FORM</h1>";
?>
<form>
<label for="fruits">Fruits :</label>
<select id="fruits" name="fruits">
<option value=""></option>
<?php
for ($i=0; $i < count($fruits); $i++) {
echo "<option value='" . $fruits[$i] . "'>" . $fruits[$i] . "</option>";
}
?>
</select>
</form>
</div>
</body>
</html>
Include files
The difference between include and require lies in what happens when the specified file cannot be included (generally because it doesn't exist or the server doesn't have permission to access it). With include, a warning is displayed and then execution continues. With require, an error is displayed and execution stops. The include_once and require_once statements work just like include and require but if the requested file has already been included once, then it will not be included again (preventing re-declarations, and increased memory demands on your scripts). This might seem an unnecessary addition, but in a complex PHP application written by a team of developers, it can be difficult to keep track of whether or not a given file has been included. It is not uncommon for a PHP page to include a file that includes other files that may include other files, and in such an environment the include_once and require_once statements are certainly recommended.
include "somefile.php";
include_once "somefile.php";
require "somefile.php";
require_once "somefile.php";
Exercise 1
Go to the Exercise we did on chapter “HTML”.
Include the head, header, and footer sections in separate files so you don’t have to repeat them in every page.
filename & magic constant
In the head, put the <title> element based on the file name.
File path : __FILE__ is a magic constant in PHP that returns the full absolute path of the current script file. __DIR__ The directory of the file
basename() function
ucfirst() function
<?php echo "File absolut path = ",__FILE__, "<br>"; ?>
<?php echo "File relative path = ",$_SERVER['PHP_SELF'], "<br>"; ?>
<?php echo "File name = ",basename($_SERVER['PHP_SELF']), "<br>"; ?>
<?php echo "File name without extension = ",basename($_SERVER['PHP_SELF'], ".php"), "<br>"; ?>
<?php echo "File name without extension and capitalize = ",ucfirst(basename($_SERVER['PHP_SELF'], ".php")), "<br>"; ?>
Agence digitale Parisweb.art
Tout savoir sur Julie, notre directrice de projets digitaux :
https://www.linkedin.com/in/juliechaumard/