Every PHP program starts with a <?php ?> tag which encloses your PHP code. You
may omit the closing tag if your PHP file only has PHP code. Each command ends
with a semicolon and PHP doesn't care about spacing.
When it comes to variables you have normal, static and global variables, as well as constants and magic constants (predefined). Since PHP is loosely typed, PHP will implicitly cast your variables. You can also explicitly cast them if need to:
// variables start with a letter or _
$player = "John";
$team_members = ['Bob', 'Ross', 'Jeff' ];
global $IS_ADMIN;
define("ROOT_LOCATION", "/usr/local"); // create a constant
echo $ROOT_LOCATION;
echo __FILE__; // predefined constants or magic constants
static $count = 0; // static variables keep their value each function call
$area = (int) ($length / $width)You can use arrays in two ways. The normal way and the short syntax. The normal way is useful for declaring two-dimensional arrays.
$names = array('John', 'Bob', 'Jeff');
$names = ['John', 'Bob', 'Jeff'];
// $names[0] = 'John';Arrays also have a lot of functions here are some of the most useful ones:
count: return the number of elements in an arraysort: directly sort the array (SORT_NUMERIC, SORT_STRING)shuffle: randomize an array directlyexplode: converts a string into an array
Beside your typical arrays, PHP also has associative arrays which are like hashes, maps, dictionaries or objects in other languages.
$team = array(
'forward' => "John",
'midfielder' => "Bob",
'goalkeeper' => "Jeff"
);
echo $team['forward'];You typical arithmetic, assignment and comparison operators are also supported:
++,-,*=,%=,==,<>. Beyond that PHP also has logical operators&&,||, '!' which uou can use as short circuits.
As for conditionals, you have if, elseif, switch statements and the short hand ternary operator.
$bank_account = 0;
$transfer = 1000;
if (@$transfer > 1000) {
$transfer_fee = $transfer * 0.05;
$bank_account += $transfer - $transfer_fee;
} elseif ($transfer > 500) {
$transfer_fee = $transfer * 0.02;
$bank_account += $transfer - $transfer_fee;
} else {
$transfer_fee = $transfer * 0.01;
$bank_account += $transfer - $transfer_fee;
}
$bank_account = 0;
$transfer = 1000;
switch ($transfer) {
case $transfer > 1000:
$transfer_fee = $transfer * 0.05;
$bank_account += $transfer - $transfer_fee;
break;
case $transfer > 500:
$transfer_fee = $transfer * 0.02;
$bank_account += $transfer - $transfer_fee;
break;
default:
$transfer_fee = $transfer * 0.01;
$bank_account += $transfer - $transfer_fee;
break;
}
$bank_account += $transfer > 500 ? $transfer * 0.95 : $transfer * 0.99;When converted to a string
TRUEis converted to the string value "1" andFALSEbecomes an empty string "".
String concatenation uses the period echo "Hello " . "World\n"; while
variable interpolation uses curly braces echo "Hello {$name}\n";. For
multiline strings you have two options. You can use put multiple
lines between quotes or you use a here-document - heredoc.
echo <<<_END
New high score: {$score}
Congratulation
_END;Functions are declared with the function keyword and can you can optionally add type declaration:
function today($timestamp): string
{
// static variables keep their value each function call
static $count = 0;
$count++;
$current_date = date("l F jS Y", $timestamp);
return "# of times executed: {$count}, date: {$current_date}";
}
echo today(time());PHP comes with a plethora of built-in functions. When writing your own functions
or when you want to use libraries by other programmers you can use the include
and require commands.
include: attempts to load a PHP file and produces a warning upon failureinclude_once: same asincludebut it will check for already included filesrequire: attempts to load a PHP file and produces a fatal error upon failurerequire_once: same asrequirebut it will check for already included files
PHP provides your common loop structures: for, while, do while and
foreach as.
while ($player_status == "active") {
echo "Moving forward 1 step";
if ($steps > 1000) break;
}
do {
if ($skip_frame) continue;
echo "Rendering another frame";
} while ($game_running == true);
for ($attacks = 0; $attacks < 10; $attacks++) {
$attack_strength = random_int(1, 25);
echo "Attack round {$attacks}: You lost {$attack_strength}HP";
}
$team_members = array('John', 'Bob', 'Jeff');
foreach($team_members as $member)
{
echo "{$member} joined the group";
}
$team = array(
'forward' => "John",
'midfielder' => "Bob",
'goalkeeper' => "Jeff"
);
foreach($team_members as $position => $member)
{
echo "{$member} plays as a {$position}";
}As seen earlier with the
switchstatement, you can also use abreakstatement within loops to break out of the loop and you can use thecontinuestatement to stop the current iteration and go directly to the next iteration.
For Object-Oriented Programming, PHP has the class keyword which you can
use to encapsulate your data and methods and exposing interfaces.
class User
{
public $email, $password;
function __construct($email, $password)
{
$this->email = $email;
$this->password = $password;
}
function save()
{
echo "User was saved";
}
static function password_error_message()
{
echo "Password is too short";
}
}
$john = new User("johndoe@example.com", "123456");
$john->email;
User::password_error_message();Static functions and variables called directly on a class not an object. You use the scope resolution operator to access them.
Once you have a class you can also derive subclasses from it using the extends
keyword. If a method has the same name in a subclass and it's parent class, the
subclass's statement will overwrite the parent's method. You can avoid this by
using the parent keyword to access the parent's method. When possible favor
composition over inheritance.
class Admin extends User
{
public $privileges;
function __construct($email, $password, $privileges)
{
parent::__construct($email, $password);
$this->privileges = $privileges;
}
function save()
{
parent::save();
echo "Admin was saved";
}
}While functions like print and echo work fine for most purposes, the
printf function gives you more control over the format of your output. You
can convert between values a specifier, control the precision and pad strings.
// %b = binary, %c = ASCII, %d = int, %e = scientific notation
// %f = float, %o = octal, %s = string, %u = uint, %x/%X = lower/uppercase hex
printf("There are %d player in your team", 11);
printf("The current temperature is %.2f Celsius", 15.54321) // 15.54
// justify right = %10, justify left = %-10
printf("The current temperature is %10.2f Celsius", 15.54321)
// The current temperature is 15.54 CelsiusIf you don't want to output the result but want to use it somewhere else, you
use sprintf:
$message = sprintf("The current temperature is %.2f Celsius", 15.54321)If you want to work with time and dates you can use the time, mktime and
date functions. PHP uses the Unix timestamps (Unix epoch).
// current time
echo time();
// time in 1 week = time + 7 days * 24h * 60m * 60s
echo time() + 7 * 24 * 60 * 60
// December 2025 = 0h, 0m, 0s, 12 month, 24 day, 2025 year
echo mktime(0, 0, 0, 12, 24, 2025);
// format the current time - Monday January 1st, 2025 - 1:30pm
echo date("l F jS, Y - g:ia", time());To work with files in PHP you also have a lot of functions at your disposal:
file_exists: check if a file already existsfile_get_contents: fetch an entire file from disk or the internetfopen: opens a file in a given mode (r=read, w=write, a=append)fwrite: writes content into the filefgets: reads from a file by grabbing a whole linefread: read one or more linescopy: makes a copy of a given filerename: moves a fileunlink: deletes a filefseek: move the file pointerflock: lock file to queue all other requests to the file to avoid corruptionfclose: cleanup by closing the file manually and avoid open files
Uploading files in PHP is as simple as having a multipart HTML form and calling
the move_uploaded_files function.
<form method='post' action='upload.php' enctype='multipart/form-data'>
Select File:
<input type='file' name='filename' size='10'>
<input type='submit' value='Upload'>
</form>// move the file from a temporary location to a permanent one and done
$name = $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], $name);Sometimes you want to run a command on your system programmatically and for that
PHP has the exec and escapeshellcmd functions.
exec(escapeshellcmd("ls -laGh"), $output, $status);PHP has support for various database systems. The most popular option for PHP is MySQL. We use the official mysql docker image to speed things up. Furthermore , we install the mysqli extension to work with our mysql database:
// connect to database
$conn = new mysqli(host, user, username);
// check for errors
if ($conn->connect_error) {
echo 'ERROR: failed to connect to the database'.$conn.connect_error;
}
// perform an SQL query
$conn->query('SQL QUERY GOES HERE');
// display latest error
$conn->error;You can access form field properties with the $_POST/$_GET variables which holds all the post parameters and queries respectively:
$name = $_POST['name'];
$conn = new msqli(init, db, connection);
// don't trust user input
$conn->escape_string($name);As always, never trust user input. Any user can sneak in SQL statements into our database if we don't clean up the input previously. For those reasons clean and double check user inputs before inserting it into an SQL query.
Session variables persits across sites until the browser is closed or until we terminate it explicitly. Sessions work by storing all the information using a UID in a cookie. We can use Sessions in PHP like this:
session_start();
<html>
...We can then retrieve and set data in our Session using the $_SESSION variables:
// set data
$_SESSION['name'] = 'Alexander';
$_SESSION['country'] = 'Germany';
// get data
$_SESSION['name'];
// check if data exists
if (isset($_SESSION['name'])) {
echo 'Name is already set';
} else {
echo 'Name is not set';
}
// remove specific session data
unset($_SESSION['name'])
// remove all session data
session_destroy();