Credits / Notes taken from:
[6h30m PHP For Absolute Beginners | 6.5 Hour Course - Traversy Media (dec-2020)](https://youtu.be/2eebptXfEvw) |
Table of Contents:
.php
files can contain text, HTML, CSS, JavaScript, and PHP codePHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
PHP Frameworks
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.
What’s new in PHP 7? (released December 2015)
<=>
)Note: PHP 8 was released on November 2020.
In order to run PHP, we need run an Web Apache Server that processes .php
files and sends the output to the browser.
For this, we can install XAMPP distribution (X-Cross Platform, Apache, MariaDB/MySQL, PHP, Perl).
To check if Apache server is running, open your browser and type http://localhost/dashboard/
To check if MySQL is running, open http://localhost/phpmyadmin/
Add C:\xampp\php
to Environment Variables (Windows)
→ path
.
php -v
If we want the Apache and MySQL to autostart at Windows Startup, we need to open XAMPP Control Panel as Administrator, click on red X to install Apache and MySQL as Window’s Services → click on Config
→ check on Autostart modules: Apache, MySQL
.
https://bitnami.com/stack/xampp#wordpress
All the php
files can be accessed from localhost from the C:\xampp\htdocs
path. The Apache Server can open all the php
files located within than folder. For projects with multiple php
files, the path will be C:\xampp\htdocs\myProject
.
For the text editor, we can choose:
Useful VSCode extensions for PHP:
Other extensions from blog article on codewall.co.uk
Now, we can start Apache and MySQL server from XAMPP Control Panel, navigate with Windows Explorer to C:\xampp\htdocs\phptutorialsetup
, open our CMD Prompt in that location and type code .
.
(phptutorialsetup
is obtained from this github repo from PHP Tutorial - Traversy Media)
Now, we need to navigate to our http://localhost/phptutorialsetup/
address in Chrome Browser.
By default, the password for root
is an empty string ""
(so we don’t have any password).
(Optional) To set a new password for root in MySQL in XAMPP: Configure it with the “XAMPP Shell” (command prompt). Open the shell from the XAMPP control panel and execute this command: mysqladmin.exe -u root password secret
this sets the root password to ‘secret’.
After that, we need to edit the C:\xampp\phpMyAdmin\config.inc.php
file, and restart both Apache and MySQL servers from XAMPP Control Panel.
Now you can access http://localhost/phpmyadmin/.
A PHP script starts with <?php
and ends with ?>
:
<?php
// PHP code goes here
?>
The default file extension for PHP files is “.php
”. (unlike Python Django
, PHP will not work within .html
files !!!)
A PHP file normally contains HTML tags, and some PHP scripting code.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "<h2>Hello World!</h2><br>";
ECHO "Hello World!<br>";
EcHo "Hello World!<br>";
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
In PHP, keywords (e.g. if
, else
, while
, echo
, etc.), classes, functions, and user-defined functions are not case-sensitive. :open_mouth:
However; all variable names are case-sensitive! :ok_hand:
<?php
/*
only the first statement will display
the value of the $color variable!
This is because $color, $COLOR, and $coLOR
are treated as three different variables:
*/
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
In PHP, a variable starts with the $
sign, followed by the name of the variable. In PHP (as it’s dynamically written language) variables don’t have types.
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $x + $y;
echo "<hr/>";
$name = "Radu";
echo "$txt my name is $name";
Rules for PHP variables:
$
sign, followed by the name of the variable$age
and $AGE
are two different variables)In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a “Fatal Error” on a type mismatch.
https://www.w3schools.com/php/php_variables_scope.asp
PHP has three different variable scopes:
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
The global
keyword is used to access a global variable from within a function.
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
/* Note, you can't declare global variables like this:
global $x = 5; // it will give a Parse error...
global $y = 10;
function myTest() {
$y = $x + $y;
}
myTest();
*/
/* Note, if you don't declare the varibles as global outside function
$x = 5;
$y = 10;
global $x, $y;
function myTest() {
$y = $x + $y; // it will give Undefined variable: x, y
}
myTest();
*/
?>
PHP also stores all global variables in an array called $GLOBALS[*index*]
. The index
holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static
keyword when you first declare the variable:
<?php
function myTest2() {
static $x = 0;
echo $x."<br>";
$x++;
}
myTest2(); // 1
myTest2(); // 2
myTest2(); // 3
/* each time the function is called,
that variable will still have the information
it contained from the last time the function was called. */
?>
(Thu, February 04, 2021) Okay, maybe it’s just me… but I’ve noted that, I don’t have any console in PHP …
However, instead of using echo
, var_dump()
, get_type()
, print_r()
functions and render directly in HTML code… I came across this function
function console_log( $data ){
echo '<script>';
echo 'console.log('. json_encode( $data ) .')';
echo '</script>';
}
// Usage:
$myvar = array("Banana", "Apple", "Orange");
console_log( $myvar );
Now we can use console_log();
to print to Browser’s console, just like in JavaScript (console.log();
)… except, we are actually using JavaScript ofc. :laughing:
The PHP var_dump()
function returns the data type and value (basically prints out all the information of a variable).
The print_r()
prints human-readable information about a variable
The PHP function gettype($name)
function returns only the data type.
PHP supports the following data types:
$txt = "Hello world!"; // 12 characters
var_dump($txt); // string(12)
echo gettype($txt); // string
// We can concatenate strings using . (dot)
echo $txt . " How are you?" . "<br/>"; // Hello world! How are you?
// or
echo "$txt How are you?<br/>"; // Hello world! How are you?
$x = 52;
var_dump($x); // int(52)
echo gettype($x); // integer
$x = 10.365;
var_dump($x); // float(10.365)
echo gettype($x); // double
$x = true;
$y = false;
var_dump(x); // bool(true)
echo gettype($x); // boolean
echo $x; // 1
echo $y; //
// Whenever booleans are converted into strings:
// true is converted into "1"
// false is converted into empty string "" (which will not show anything in html)
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
// array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
$nested_array = array(array("red", "green"), "blue");
// array(2) { [0]=> array(2) { [0]=> string(3) "red" [1]=> string(5) "green" } [1]=> string(4) "blue" }
// If a variable is created without a value, it is automatically assigned a value of NULL.
// Variables can also be emptied by setting the value to NULL:
$x = "Hello world!";
$x = null;
var_dump($x); // NULL
echo gettype($x); // NULL
echo $x; //
// null is converted into an empty string "" as well
/* with __construct() function,
PHP will automatically call this function when you create an object from a class.
*/
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
// or
// return "My car is a $this->color $this->model!";
}
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message(); // My car is a black Volvo!
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message(); // My car is a red Toyota!
var_dump($myCar); // object(Car)#2 (2) { ["color"]=> string(3) "red" ["model"]=> string(6) "Toyota" }
echo gettype($x); // object
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP. A common example of using the resource data type is a database call.
Variable checking functions
var_dump("Hello"); // string(5) "Hello"
echo gettype("Hello"); // string
is_string($name); // false
is_int($age); // true
is_double($height); // true
is_bool($isMale); // true
/* However these functions will not print anything in HTML
... and you cannot use echo is_double($var)... because if
it's false, it will not print anything in HTML...
So, the safest way to check is with: */
var_dump(is_string($name)); // bool(false)
var_dump(is_int($age)); // bool(true)
var_dump(is_double($height)); // bool(true)
var_dump(is_bool($isMale)); // bool(true)
Check if variable is declared/defined
$name = "Alex";
isset($name); // true
isset($address); // false
To declare CONSTANT variables (const) in PHP, we use define
(just like in C/C++):
define(name, value, case_insensitive);
Example:
define('PI', 3.14);
// to use/print a constant, we don't use $ anymore
echo PI; // 3.1415
// predefined constants in PHP
echo PHP_INT_MAX.'<br>'; // 9223372036854775807
echo PHP_FLOAT_MAX.'<br>'; // 1.7976931348623E+308
Example:
<?php
define("GREETING", "Welcome aboard, Captain!", true);
echo greeting; // Welcome aboard, Captain!
?>
PHP Constant Arrays
In PHP7, you can create an Array constant using the define()
function.
<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0]; // Alfa Romeo
?>
NOTE: Constants are automatically global and can be used across the entire script.
https://www.w3schools.com/php/php_operators.asp
PHP Arithmetic Operators
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $x + $y |
Sum of $x and $y |
- | Subtraction | $x - $y |
Difference of $x and $y |
* | Multiplication | $x * $y |
Product of $x and $y |
/ | Division | $x / $y |
Quotient of $x and $y |
% | Modulus | $x % $y |
Remainder of $x divided by $y |
** | Exponentiation | $x ** $y |
Result of raising $x to the $y’th power |
PHP Comparison Operators
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $x == $y |
Returns true if $x is equal to $y |
=== | Identical | $x === $y |
Returns true if $x is equal to $y, and they are of the same type |
!= | Not equal | $x != $y |
Returns true if $x is not equal to $y |
<> | Not equal | $x <> $y |
Returns true if $x is not equal to $y |
!== | Not identical | $x !== $y |
Returns true if $x is not equal to $y, or they are not of the same type |
> | Greater than | $x > $y |
Returns true if $x is greater than $y |
< | Less than | $x < $y |
Returns true if $x is less than $y |
>= | Greater than or equal to | $x >= $y |
Returns true if $x is greater than or equal to $y |
<= | Less than or equal to | $x <= $y |
Returns true if $x is less than or equal to $y |
<=> | Spaceship | $x <=> $y |
Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. |
PHP Logical Operators
Operator | Name | Example | Result |
---|---|---|---|
and |
And | $x and $y |
True if both $x and $y are true |
or |
Or | $x or $y |
True if either $x or $y is true |
xor |
Xor | $x xor $y |
True if either $x or $y is true,but not both |
&& |
And | $x && $y |
True if both $x and $y are true |
\|\| |
Or | $x \|\| $y |
True if either $x or $y is true |
! |
Not | !$x |
True if $x is not true |
PHP Increment / Decrement Operators
Operator | Name | Description |
---|---|---|
++$x |
Pre-increment | Increments $x by one, then returns $x |
$x++ |
Post-increment | Returns $x, then increments $x by one |
--$x |
Pre-decrement | Decrements $x by one, then returns $x |
$x-- |
Post-decrement | Returns $x, then decrements $x by one |
PHP String Operators
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $txt1 . $txt2 |
Concatenation of $txt1 and $txt2 |
.= | Concatenation assignment | $txt1 .= $txt2 |
Appends $txt2 to $txt1 |
PHP Array Operators
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $x + $y |
Union of $x and $y |
== | Equality | $x == $y |
Returns true if $x and $y have the same key/value pairs |
=== | Identity | $x === $y |
Returns true if $x and $y have the same key/value pairs in the same order and of the same types |
!= | Inequality | $x != $y |
Returns true if $x is not equal to $y |
<> | Inequality | $x <> $y |
Returns true if $x is not equal to $y |
!== | Non-identity | $x !== $y |
Returns true if $x is not identical to $y |
PHP Conditional Assignment Operators
Operator | Name | Example | Result |
---|---|---|---|
?: | Ternary | $x = expr1 ? expr2 : expr3 |
Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE |
?? | Null coalescing | $x = expr1 ?? expr2 |
Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2. Introduced in PHP 7 |
https://www.w3schools.com/php/php_numbers.asp
echo 0.2 + 0.1; // 0.3
/* Note that the result with echo is trimmed and we don't see a floating point number */
// But
var_dump(.1 + .2); // float(0.30000000000000004441)
var_dump(.2 + .1 === 0.3); // bool(false)
# Python
print(0.2 + 0.3) # 0.30000000000000004
// JavaScript (Node.js)
console.log(0.2 + 0.3); // 0.30000000000000004
PHP Integers and floats/doubles
<?php
$x = 10.365;
var_dump(is_float($x)); // bool(true)
$x = 10
var_dump(is_int($x)); // bool(true)
?>
PHP Infinity
A numeric value that is larger than PHP_FLOAT_MAX is considered infinite.
PHP has the following functions to check if a numeric value is finite or infinite:
echo PHP_FLOAT_MAX.'<br>'; // 1.7976931348623E+308
$x = 1.9e411;
var_dump($x); // float(INF)
var_dump(is_infinite($x)); // bool(true)
var_dump(is_finite($x)); // bool(false)
var_dump(is_finite(43)); // true
PHP NaN (Not a Number)
NaN is used for impossible mathematical operations. PHP has the following functions to check if a value is not a number:
// Invalid calculation will return a NaN value
$x = acos(8);
var_dump($x); // float(NAN)
PHP Numerical Strings
The PHP is_numeric()
function can be used to find whether a variable is numeric. The function returns true if the variable is a number or a numeric string, false otherwise.
$x = 5985;
var_dump(is_numeric($x)); // bool(true)
$x = "5985";
var_dump(is_numeric($x)); // bool(true)
$x = "59.85" + 100;
var_dump(is_numeric($x)); // bool(true)
$x = "Hello";
var_dump(is_numeric($x)); // bool(false)
// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast; // 23465
echo "<br>";
// Cast string to int
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast; // 23465
// Cast string to float
$float_cast = (float)$x;
Other ways of casting using functions:
$strNumber = '12.23';
$number = intval($strNumber);
var_dump($number); // int(12)
echo "<br/>";
var_dump(floatval("12.23")); // float(12.23)
number_format($number, $decimals = 0, $dec_point = '.', $thousands_sep = ',')
$number = 123456789.12345;
echo number_format($number, 2, '.', " "); // 123 456 789.12
echo number_format($number, 2, '.', ","); // 123,456,789.12
https://www.w3schools.com/php/php_math.asp
PHP pi()
function
echo(pi()); // returns 3.1415926535898
PHP min() and max() Functions
echo(min(0, 150, 30, 20, -8, -200)); // returns -200
echo(max(0, 150, 30, 20, -8, -200)); // returns 150
PHP abs() (absolute value) Function
echo(abs(-6.7)); // returns 6.7
PHP sqrt() Function
echo(sqrt(64)); // returns 8
PHP round() Function
echo(round(0.60)); // returns 1
echo(round(0.49)); // returns 0
PHP floor and ceil
echo(floor(0.60)); // 0
echo(ceil(0.49)); // 1
Random Numbers
echo(rand()); // 1381167960
echo(rand(10, 100)); // 97
Function | Description |
---|---|
abs() | Returns the absolute (positive) value of a number |
acos() | Returns the arc cosine of a number |
acosh() | Returns the inverse hyperbolic cosine of a number |
asin() | Returns the arc sine of a number |
asinh() | Returns the inverse hyperbolic sine of a number |
atan() | Returns the arc tangent of a number in radians |
atan2() | Returns the arc tangent of two variables x and y |
atanh() | Returns the inverse hyperbolic tangent of a number |
base_convert() | Converts a number from one number base to another |
bindec() | Converts a binary number to a decimal number |
ceil() | Rounds a number up to the nearest integer |
cos() | Returns the cosine of a number |
cosh() | Returns the hyperbolic cosine of a number |
decbin() | Converts a decimal number to a binary number |
dechex() | Converts a decimal number to a hexadecimal number |
decoct() | Converts a decimal number to an octal number |
deg2rad() | Converts a degree value to a radian value |
exp() | Calculates the exponent of e |
expm1() | Returns exp(x) - 1 |
floor() | Rounds a number down to the nearest integer |
fmod() | Returns the remainder of x/y |
getrandmax() | Returns the largest possible value returned by rand() |
hexdec() | Converts a hexadecimal number to a decimal number |
hypot() | Calculates the hypotenuse of a right-angle triangle |
intdiv() | Performs integer division |
is_finite() | Checks whether a value is finite or not |
is_infinite() | Checks whether a value is infinite or not |
is_nan() | Checks whether a value is ‘not-a-number’ |
lcg_value() | Returns a pseudo random number in a range between 0 and 1 |
log() | Returns the natural logarithm of a number |
log10() | Returns the base-10 logarithm of a number |
log1p() | Returns log(1+number) |
max() | Returns the highest value in an array, or the highest value of several specified values |
min() | Returns the lowest value in an array, or the lowest value of several specified values |
mt_getrandmax() | Returns the largest possible value returned by mt_rand() |
mt_rand() | Generates a random integer using Mersenne Twister algorithm |
mt_srand() | Seeds the Mersenne Twister random number generator |
octdec() | Converts an octal number to a decimal number |
pi() | Returns the value of PI |
pow() | Returns x raised to the power of y |
rad2deg() | Converts a radian value to a degree value |
rand() | Generates a random integer |
round() | Rounds a floating-point number |
sin() | Returns the sine of a number |
sinh() | Returns the hyperbolic sine of a number |
sqrt() | Returns the square root of a number |
srand() | Seeds the random number generator |
tan() | Returns the tangent of a number |
tanh() | Returns the hyperbolic tangent of a number |
https://www.php.net/manual/en/ref.math.php
Print the number of seconds since 01.01.1970
echo time(); // 1631120754
Print the current date using formatting:
echo date('Y-m-d H:i:s'); // 2021-09-08 19:00:23
Print the yesterday’s date using a timestamp parameter, where we specify the time minus the day in seconds (there are 60 * 60 * 24
seconds in a day)
// Print yesterday's date
echo date('Y-m-d H:i:s', time() - 60 * 60 * 24); // 2021-09-07 19:02:56
// Print tomorrow's date
echo date('Y-m-d H:i:s', time() + 60 * 60 * 24); // 2021-09-07 19:02:56
More examples using date formats
echo date('F j Y, H:i:s'); // September 8 2021, 19:04:34
Another example of getting the current date in PHP in a specific format:
date()
formatting parameters heredate()
function from php.netfunction getTodayDateFormatted() {
return date("l, F d, Y");
}
echo getTodayDateFormatted(); // Saturday, September 04, 2021
Parse dates from HTML forms (will return an associative array):
$parseDate = date_parse('2021-08-12 09:00:00');
echo "<pre>";
var_dump($parseDate);
echo "</pre>";
/* returns
array(12) {
["year"]=>
int(2021)
["month"]=>
int(8)
["day"]=>
int(12)
["hour"]=>
int(9)
["minute"]=>
int(0)
["second"]=>
int(0)
["fraction"]=>
float(0)
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(0)
["errors"]=>
array(0) {
}
["is_localtime"]=>
bool(false)
}
*/
Parse a date from in a different format (form a different source, other than HTML form) using date_parse_from_format
. Here we need to specify the format of the date received in a way that PHP will print it using it’s date()
formatting options:
$dateString = 'February 12 2021 12:54:34';
$parseDate = date_parse_from_format('F j Y H:i:s', $dateString);
echo "<pre>";
var_dump($parseDate);
echo "</pre>";
/* returns
array(12) {
["year"]=>
int(2021)
["month"]=>
int(2)
["day"]=>
int(12)
["hour"]=>
int(12)
["minute"]=>
int(54)
["second"]=>
int(34)
["fraction"]=>
float(0)
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(0)
["errors"]=>
array(0) {
}
["is_localtime"]=>
bool(false)
}
*/
Note that it will also work using just date_parse()
$dateString = 'February 12 2021 12:54:34';
$parseDate = date_parse($dateString);
(Friday, February 05, 2021)
Strings concatenation (Note that the use of single or double quotes matters)
$name = "Alex";
echo 'Hi I am '.$name.'!<br/>'; // Hi I am Alex!
echo 'Hi I am $name!<br/>'; // Hi I am $name!
echo "Hi I am $name!<br/>"; // Hi I am Alex!
strlen()
- Return the Length of a String
echo strlen("Hello world!"); // outputs 12
trim()
- Removes whitespace from both sides of a string
For right or left side, use rtrim()
/ ltrim()
echo trim(" Hi, World! "); // Hi, World!
str_word_count()
- Count Words in a String
echo str_word_count("Hello world!"); // outputs 2
strrev()
- Reverse a String
echo strrev("Hello world!"); // !dlrow olleH
strpos()
- Search For a Text Within a String (case sensitive), returns the first characters position of the first match, if no match is found, returns false
echo strpos("Hello world!", "world"); // 6
stripos()
- Search For a Text Within a String (ignore case sensitive)
echo strpos("Hello World!", "world"); // 6
substr()
- Returns part of a string (substring)
echo substr("Hello World", 4); // o World
echo substr("Hello World", -4); // orld
echo substr("Hello World", 4, 3); // O W (takes only 3 characters from pos 4)
str_replace()
- Replace Text (all occurrences) within a Stringstr_ireplace()
(ignore case sensitive)
echo str_replace("world", "Captain", "Hello world!"); // Hello Captain!
echo str_ireplace("hello", "Captain", "Hello? hello!"); // Captain? Captain!
strtolower()
and strtoupper()
converts string to Lower Case / Upper Case
echo strtolower("Hello World"); // hello world
echo strtoupper("Hello World"); // HELLO WORLD
ucfirst()
and lcfirst()
converts to Upper Case / Lower Case only the first character
echo ucfirst("hello world"); // Hello world
echo lcfirst("HELLO WORLD"); // hELLO WORLD
ucwords()
Upper Case all words in string
echo ucwords("hello world"); // Hello World
nl2br()
- newline to break line
$longText = "
Hi,
I'm Alex,
I like back-end.
";
echo $longText.'<br>'; // Hi, I'm Alex, I like back-end.
echo nl2br($longText).'<br>';
// Hi,
// I'm Alex,
// I like back-end.
We can access string character’s with [ ]
:
echo "Hello"[0]; // H
echo "Hello"[1]; // e
echo "Hello"[-1]; // o
Unlike JavaScript and Python… Strings in PHP are mutable (content can be changed without creating a new object).
$myStr = "hello";
$myStr[0] = "j";
$myStr[-1] = 'y';
echo $myStr; // jelly
explode()
Breaks a string into an array (in JavaScript/Python we have str.split()
)
explode ( string `$separator` , string `$string` , int `$limit` = `PHP_INT_MAX`) : array
$pizza = "piece1 piece2 piece3";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
implode()
Returns a string from the elements of an array (join()
does the same, is an alias of implode()
)
$arr = array('Test1', 'Test2', 'Test3');
$str = join(",", $arr);
echo $str; // Test1,Test2,Test3.
Function | Description |
---|---|
addcslashes() | Returns a string with backslashes in front of the specified characters |
addslashes() | Returns a string with backslashes in front of predefined characters |
bin2hex() | Converts a string of ASCII characters to hexadecimal values |
chop() | Removes whitespace or other characters from the right end of a string |
chr() | Returns a character from a specified ASCII value |
chunk_split() | Splits a string into a series of smaller parts |
convert_cyr_string() | Converts a string from one Cyrillic character-set to another |
convert_uudecode() | Decodes a uuencoded string |
convert_uuencode() | Encodes a string using the uuencode algorithm |
count_chars() | Returns information about characters used in a string |
crc32() | Calculates a 32-bit CRC for a string |
crypt() | One-way string hashing |
echo() | Outputs one or more strings |
explode() | Breaks a string into an array |
fprintf() | Writes a formatted string to a specified output stream |
get_html_translation_table() | Returns the translation table used by htmlspecialchars() and htmlentities() |
hebrev() | Converts Hebrew text to visual text |
hebrevc() | Converts Hebrew text to visual text and new lines (\n) into |
hex2bin() | Converts a string of hexadecimal values to ASCII characters |
html_entity_decode() | Converts HTML entities to characters |
htmlentities() | Converts characters to HTML entities |
htmlspecialchars_decode() | Converts some predefined HTML entities to characters |
htmlspecialchars() | Converts some predefined characters to HTML entities |
implode() | Returns a string from the elements of an array |
join() | Alias of implode() |
lcfirst() | Converts the first character of a string to lowercase |
levenshtein() | Returns the Levenshtein distance between two strings |
localeconv() | Returns locale numeric and monetary formatting information |
ltrim() | Removes whitespace or other characters from the left side of a string |
md5() | Calculates the MD5 hash of a string |
md5_file() | Calculates the MD5 hash of a file |
metaphone() | Calculates the metaphone key of a string |
money_format() | Returns a string formatted as a currency string |
nl_langinfo() | Returns specific local information |
nl2br() | Inserts HTML line breaks in front of each newline in a string |
number_format() | Formats a number with grouped thousands |
ord() | Returns the ASCII value of the first character of a string |
parse_str() | Parses a query string into variables |
print() | Outputs one or more strings |
printf() | Outputs a formatted string |
quoted_printable_decode() | Converts a quoted-printable string to an 8-bit string |
quoted_printable_encode() | Converts an 8-bit string to a quoted printable string |
quotemeta() | Quotes meta characters |
rtrim() | Removes whitespace or other characters from the right side of a string |
setlocale() | Sets locale information |
sha1() | Calculates the SHA-1 hash of a string |
sha1_file() | Calculates the SHA-1 hash of a file |
similar_text() | Calculates the similarity between two strings |
soundex() | Calculates the soundex key of a string |
sprintf() | Writes a formatted string to a variable |
sscanf() | Parses input from a string according to a format |
str_getcsv() | Parses a CSV string into an array |
str_ireplace() | Replaces some characters in a string (case-insensitive) |
str_pad() | Pads a string to a new length |
str_repeat() | Repeats a string a specified number of times |
str_replace() | Replaces some characters in a string (case-sensitive) |
str_rot13() | Performs the ROT13 encoding on a string |
str_shuffle() | Randomly shuffles all characters in a string |
str_split() | Splits a string into an array |
str_word_count() | Count the number of words in a string |
strcasecmp() | Compares two strings (case-insensitive) |
strchr() | Finds the first occurrence of a string inside another string (alias of strstr()) |
strcmp() | Compares two strings (case-sensitive) |
strcoll() | Compares two strings (locale based string comparison) |
strcspn() | Returns the number of characters found in a string before any part of some specified characters are found |
strip_tags() | Strips HTML and PHP tags from a string |
stripcslashes() | Unquotes a string quoted with addcslashes() |
stripslashes() | Unquotes a string quoted with addslashes() |
stripos() | Returns the position of the first occurrence of a string inside another string (case-insensitive) |
stristr() | Finds the first occurrence of a string inside another string (case-insensitive) |
strlen() | Returns the length of a string |
strnatcasecmp() | Compares two strings using a “natural order” algorithm (case-insensitive) |
strnatcmp() | Compares two strings using a “natural order” algorithm (case-sensitive) |
strncasecmp() | String comparison of the first n characters (case-insensitive) |
strncmp() | String comparison of the first n characters (case-sensitive) |
strpbrk() | Searches a string for any of a set of characters |
strpos() | Returns the position of the first occurrence of a string inside another string (case-sensitive) |
strrchr() | Finds the last occurrence of a string inside another string |
strrev() | Reverses a string |
strripos() | Finds the position of the last occurrence of a string inside another string (case-insensitive) |
strrpos() | Finds the position of the last occurrence of a string inside another string (case-sensitive) |
strspn() | Returns the number of characters found in a string that contains only characters from a specified charlist |
strstr() | Finds the first occurrence of a string inside another string (case-sensitive) |
strtok() | Splits a string into smaller strings |
strtolower() | Converts a string to lowercase letters |
strtoupper() | Converts a string to uppercase letters |
strtr() | Translates certain characters in a string |
substr() | Returns a part of a string |
substr_compare() | Compares two strings from a specified start position (binary safe and optionally case-sensitive) |
**substr_count()** | Counts the number of times a substring occurs in a string |
substr_replace() | Replaces a part of a string with another string |
trim() | Removes whitespace or other characters from both sides of a string |
ucfirst() | Converts the first character of a string to uppercase |
ucwords() | Converts the first character of each word in a string to uppercase |
vfprintf() | Writes a formatted string to a specified output stream |
vprintf() | Outputs a formatted string |
vsprintf() | Writes a formatted string to a variable |
wordwrap() | Wraps a string to a given number of characters |
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or like this:
$cars = ["Volvo", "BMW", "Toyota"];
var_dump($cars);
// array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
echo $cars[0]; // Volvo
echo $cars[1]; // BMW
echo $cars[2]; // Toyota
or we can set elements by index manually (without declaring $cars
first):
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Print all the contents of an array while nicely formated:
$fruits = ["Banana", "Apple", "Orange"];
echo '<pre>';
var_dump($fruits);
echo '</pre>';
array(3) {
[0]=>
string(5) "Banana"
[1]=>
string(5) "Apple"
[2]=>
string(6) "Orange"
}
Loop through an Indexed Array using a for
loop
$fruits = ["Banana", "Apple", "Orange"];
for($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i].'<br>';
}
/*
Banana
Apple
Orange */
(Saturday, February 06, 2021)
Get the length of array with count()
$fruits = ["Banana", "Apple", "Orange"];
echo count($fruits); // 3
sizeof()
it’s an alias of count()
(it’s the same function)
echo sizeof($fruits); // 3
Check if array has an element at index
isset($fruits[2]); // true
var_dump(isset($fruits[2])); // bool(true)
Append element at the end of Array
$fruits[] = 'Kiwi';
var_dump($fruits);
// array(4) { [0]=> string(5) "Banna" [1]=> string(5) "Apple" [2]=> string(6) "Orange" [3]=> string(4) "Kiwi" }
// Note that
$fruits[] = ['Kiwi', 'Mango'];
// will append an array to the end of the array.
array(4) {
[0]=>
string(5) "Banna"
[1]=>
string(5) "Apple"
[2]=>
string(6) "Orange"
[3]=>
array(2) {
[0]=>
string(4) "Kiwi"
[1]=>
string(5) "Mango"
}
}
^^ So with this, we can create an array just by appending using $array[] = "element"
// example: Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cindy";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
We can also append multiple elements to array using array_push()
$fruits = ["Banana", "Apple", "Orange"];
array_push($fruits, "Kiwi", "Raspberry");
print_r($fruits);
// Array ( [0] => Banana [1] => Apple [2] => Orange [3] => Kiwi [4] => Raspberry )
Remove (pop) element from the end of Array (and also return it)
$fruits = ["Banana", "Apple", "Orange"];
echo array_pop($fruits); // Orange
print_r($fruits); // Array ( [0] => Banana [1] => Apple )
Add element at the beginning of the array with array_unshift()
$fruits = ["Banana", "Apple", "Orange"];
array_unshift($fruits, 'Pineapple');
print_r($fruits); // Array ( [0] => Pineapple [1] => Banana [2] => Apple [3] => Orange )
Remove element from the beginning of the array with array_shift()
$fruits = ["Banana", "Apple", "Orange"];
array_shift($fruits);
// Array ( [0] => Apple [1] => Orange )
explode ( string `$separator` , string `$string` , int `$limit` = `PHP_INT_MAX` ) : array
$fruits = explode(",", "Banana,Apple,Orange");
print_r($fruits); // Array ( [0] => Banana [1] => Apple [2] => Orange )
implode ( string `$separator` , array `$array` ) : string
$fruits = ["Banana", "Apple", "Orange"];
echo implode(", ", $fruits); // Banana, Apple, Orange
Check if element exists in Array with in_array()
$fruits = ["Banana", "Apple", "Orange"];
var_dump(in_array('Apple', $fruits)); // bool(true)
var_dump(in_array('Lemon', $fruits)); // bool(false)
Return element’s index in Array (if exists) with array_search()
$fruits = ["Banana", "Apple", "Orange"];
var_dump(array_search('Apple', $fruits)); // int(1)
var_dump(array_search('Lemon', $fruits)); // bool(false)
Return merged array from two or more arrays with array_merge()
$fruits = ["Banana", "Apple", "Orange"];
$veggies = ["Potato", "Cucumber"];
print_r(array_merge($fruits, $veggies));
// Array ( [0] => Banana [1] => Apple [2] => Orange [3] => Potato [4] => Cucumber )
From PHP 7.4
and above, we can use the Spread operator to merge arrays:
print_r( [...$fruits, ...$veggies] );
// Array ( [0] => Banana [1] => Apple [2] => Orange [3] => Potato [4] => Cucumber )
The array_chunk()
function splits an array into chunks of new arrays.
$cars = array("Volvo","BMW","Toyota","Honda","Mercedes");
echo "<pre>";
print_r(array_chunk($cars, 2));
echo "</pre>";
Array
(
[0] => Array
(
[0] => Volvo
[1] => BMW
)
[1] => Array
(
[0] => Toyota
[1] => Honda
)
[2] => Array
(
[0] => Mercedes
)
)
$fruits = ["Banana", "Apple", "Orange"];
sort($fruits);
print_r($fruits); // Array ( [0] => Apple [1] => Banana [2] => Orange )
$numbers = array(4,6,2,22,11);
sort($numbers);
echo(join(', ', $numbers)); // 2, 4, 6, 11, 22
(sort($arr)
mutates the array and returns true on success, false on failure)
Associative arrays are key-value
pairs.
They are the equivalent of dictionaries in Python or objects in JavaScript. All in all, associative arrays are like JSON in PHP’s coat (the same applies for objects in JavaScript, this data format is JSON - read more here).
Example:
$ages = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
// Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Another example:
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games']
];
echo "<pre>";
print_r($person);
echo "</pre>";
Output:
Array
(
[name] => Brad
[surname] => Traversy
[age] => 30
[hobbies] => Array
(
[0] => Tennis
[1] => Video Games
)
)
Let’s compare the same “associative array” to a JavaScript object:
// JavaScript
let person = {
name: "Brad",
surname: "Traversy",
age: 30,
hobbies: ["Tennis", "Video Games"],
};
console.log(person);
Output:
{name: "Brad", surname: "Traversy", age: 30, hobbies: Array(2)}
age: 30
hobbies: (2) ["Tennis", "Video Games"]
name: "Brad"
surname: "Traversy"
Loop Through an Associative Array
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games']
];
foreach($person as $key => $value) {
echo($key.": ");
print_r($value);
echo("<br/>");
}
name: Brad
surname: Traversy
age: 30
hobbies: Array ( [0] => Tennis [1] => Video Games )
Get/Set/Check element in array by key
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games']
];
echo $person['name']; // Brad
$person['age'] = 34;
if (!isset($person['address'])) {
// if $person addres does not exist, add and set the 'address' key to 'unknown' value
$person['address'] = 'unknown';
}
However, from PHP 7.4, we can check and set a value to a non-existing key with double question marks ??
:
$person['address'] ??= 'unknown';
// same as
$person['address'] = $person['address'] ?? 'unknown';
// same as
if (!isset($person['address'])) {
$person['address'] = 'unknown';
}
If we want to print out the keys of an associative array, we use array_keys($arr)
echo '<pre>';
var_dump(array_keys($person));
echo '</pre>';
And to print our an associative array values, we use array_values()
echo '<pre>';
var_dump(array_values($person));
echo '</pre>';
We can sort associative arrays by keys using ksort($assoc_array)
:
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games']
];
ksort($person);
echo "<pre>";
print_r($person);
echo "</pre>";
Or we can sort associative arrays by values using asort($assoc_array)
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games']
];
asort($person);
Sort a associative multidimensional array
If we want to sort by the values of “price” key (source here)
$inventory = array(
array("type"=>"pork", "price"=>5.43),
array("type"=>"fruit", "price"=>3.50),
array("type"=>"milk", "price"=>2.90),
);
/* For PHP < 5.5 */
$price = array();
foreach ($inventory as $key => $row) {
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
/* For PHP > 5.5 */
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
/* For PHP > 7.0 */
// Ascending
usort($inventory, function ($item1, $item2) {
return $item1['price'] <=> $item2['price'];
});
// Descending
usort($inventory, function ($item1, $item2) {
return $item2['price'] <=> $item1['price'];
});
<=>
, the spaceship comparison operator, returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater
<?php
$todos = [
['title' => 'Todo title 1', 'completed' => true],
['title' => 'Todo title 2', 'completed' => false],
['title' => 'Todo title 3', 'completed' => true]
];
(This is how the data might look like when we are using REST API - same as with JavaScript Objects / JSON)
$time_hour = date("H");
if ($time_hour >= "10" && $time_hour < "12") {
echo "Good morning!";
} elseif ($time_hour < "18") {
echo "Good afternoon!";
} else {
echo "Good evening!";
}
var_dump($time_hour); // string(2) "17"
Another example:
$age = 24;
if ($age === 24) echo "Your age is ".$age;
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $x == $y |
Returns true if $x is equal to $y |
=== | Identical | $x === $y |
Returns true if $x is equal to $y, and they are of the same type |
Example of writing if else statement with HMTL:
<?php if( is_product() ): ?>
<div><a class="product-link" href="<?php echo esc_url( get_permalink() ); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
<?php else: ?>
<h3><a class="product-link" href="<?php echo esc_url( get_permalink() ); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php endif; ?>
Another example: https://www.php.net/manual/en/control-structures.elseif.php
$age = 24;
echo $age < 21 ? "Bellow 21 yo" : "Above 21 yo";
Another example:
$a = 5;
$b = 10;
$c = ($a < $b) ? "a is less than b" : "a is not less than b";
echo $c; // a is less than b
Another example (Note that you need to use parenthesis for nested ternary expressions!):
function checkSign($num) {
return $num > 0 ?
"positive" :
($num < 0 ?
"negative" :
"zero");
}
echo checkSign(23); // positive
echo checkSign(0); // zero
echo checkSign(-2); // negative
Short ternary operator ?:
will check if the variable has a falsy value (or if it does not exist), then executes.
$age = 24;
echo $age ?: 18; // 24
If $age
variable was falsy, we would get:
$age = 0;
echo $age ?: 18; // 18
If $age
variable was undefined, we would get a warning, but it will still echo the value ‘18’:
echo $age ?: 18;
// Warning: Undefined variable $age in C:\xampp\htdocs\tutorials\a.php on line 3
// 18
Another example:
$price;
$defaultPrice = $price ?: 15;
echo($defaultPrice);
// Warning: Undefined variable $price in C:\xampp\htdocs\tutorials\a.php on line 4
// 15
Another example (without warning - the better way):
$defaultPrice = isset($price) ?: 15;
echo($defaultPrice); // 15
Another example with Null coalescing operator ??
$defaultPrice = $price ?? 15;
// same as
$defaultPrice = isset($price) ?: 15;
// same as (the most readable)
$defaultPrice = isset($price) ? $price : 15;
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
Another example:
$message = '';
$role = 'author';
switch ($role) {
case 'admin':
$message = 'Welcome, admin!';
break;
case 'editor':
case 'author':
$message = 'Welcome! Do you want to create a new article?';
break;
case 'subscriber':
$message = 'Welcome! Check out some new articles.';
break;
default:
$message = 'You are not authorized to access this page';
}
echo $message; // Welcome! Do you want to create a new article?
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
// 12345678910
Or
$i = 0;
while($i <= 100) {
echo $i.' ';
$i += 10;
}
// 0 10 20 30 40 50 60 70 80 90 100
Another example:
$i = 0;
while($i <= 100) {
echo $i.' ';
if ($i === 5) break;
$i++;
}
// 0 1 2 3 4 5
Example with do while
$i = 1;
do {
echo "$i ";
$i++;
} while ($i <= 5);
// 1 2 3 4 5
for ($i = 0; $i <= 10; $i++) {
echo "$i ";
}
// 0 1 2 3 4 5 6 7 8 9 10
for ($i = 0; $i <= 100; $i += 10) {
echo "$i ";
}
// 0 10 20 30 40 50 60 70 80 90 100
Another example - looping over an array and initializing both $i
and $mySum
variables
$myArr = [9, 10, 11, 12];
for ($i = 0, $mySum = 0; $i < count($myArr); $i++) {
$mySum += $myArr[$i];
}
echo $mySum; // 42
The foreach
loop - Loops through a block of code for each element in an array.
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $color_value) {
echo "$color_value ";
}
// red green blue yellow
And for associative arrays:
$ages = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($ages as $key => $value) {
echo "$key = $value<br>";
}
/*
Peter = 35
Ben = 37
Joe = 43
*/
More examples
$todos = [
['title' => 'Todo title 1', 'completed' => true],
['title' => 'Todo title 2', 'completed' => false],
['title' => 'Todo title 3', 'completed' => true]
];
/* Example 1 */
foreach($todos as $key => $value) {
echo("<pre>");
echo($key." = "); print_r($value);
echo("</pre>");
}
/* Example 2 */
foreach($todos as $id => $todo) {
if(is_array($todo)) {
foreach($todo as $key => $val) {
echo "$key = $val; ";
}
echo "<br>";
}
}
Another example:
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games']
];
foreach($person as $key => $val) {
if (is_array($val)) {
echo "$key = " . implode(", ", $val) . "<br>";
} else {
echo "$key = $val<br>";
}
}
More about functions and strict_types
on w3schools
function getTodayDateFormatted() {
return date("l, F d, Y");
}
echo getTodayDateFormatted(); // Saturday, September 04, 2021
echo toCelsius(80); // 26.666666666667
function toCelsius($fahrenheit) {
return (5/9) * ($fahrenheit - 32);
}
function toFahrenheit($celsius) {
return $celsius * 9 / 5 + 32;
}
Note that functions can be called before they are declared.
Another example:
$inventory = array(
array("name"=>"pork", "price"=>5.00),
array("name"=>"pizza", "price"=>4.50),
array("name"=>"milk", "price"=>1.90),
array("name"=>"oranges", "price"=>2.10)
);
function sumOfPrices($arrOfObj) {
$sum = 0;
foreach($arrOfObj as $obj) {
$sum += $obj["price"];
}
return $sum;
}
echo sumOfPrices($inventory); // 13.5
In JavaScript, we would have write:
// JavaScript
var inventory = [
{ name: "pork", price: 5.0 },
{ name: "pizza", price: 4.5 },
{ name: "milk", price: 1.9 },
{ name: "oranges", price: 2.1 },
];
var sum = inventory.reduce((total, item) => total + item.price, 0);
console.log(sum); // 13.5
Take all the arguments of a function and save as an array
function sum(...$nums) {
return array_sum($nums);
}
echo sum(1,2,3,4,5,6,7,8,9); // 45
function sum(...$nums) {
return array_reduce($nums, fn($carry, $n) => $carry + $n);
}
echo sum(1,2,3,4,5,6,7,8,9); // 45
Another example:
$inventory = array(
array("name"=>"pork", "price"=>5.00),
array("name"=>"pizza", "price"=>4.50),
array("name"=>"milk", "price"=>1.90),
array("name"=>"oranges", "price"=>2.10)
);
$sumPrice = array_reduce($inventory, fn($total, $arr) => $total + $arr["price"], 0);
echo($sumPrice); // 13.5
Another example
$func = function($value) {
return $value * 2;
};
print_r(array_map($func, range(1, 5))); // [2, 4, 6, 8, 10]
Resources on PHP OOP from w3schools:
new
keyword (new Classname();
) to create an instance (object) of a Class->
to set properties of objects (instances)class Person {
public $name;
public $surname;
private $age;
}
// To create an instance (object) of Person class
$personObj = new Person();
$personObj->name = "Radu";
$personObj->surname = "Alex";
// if we try to set a value to a private property we get Fatal Error
// $personObj->age = 24;
echo "<pre>";
var_dump($personObj);
echo "</pre>";
__construct
keyword$this->
keyword to access the properties of a class within that class (eg. in Python Language we would use self.
)class Person {
public $name;
public $surname;
private $age;
public function __construct($name, $surname = "undefined", $age = 0) {
$this->name = $name;
$this->surname = $surname;
$this->age = $age;
}
}
$personObj = new Person('Radu', 'Alex');
echo "<pre>";
var_dump($personObj);
echo "</pre>";
/*
object(Person)#1 (3) {
["name"]=>
string(4) "Radu"
["surname"]=>
string(4) "Alex"
["age":"Person":private]=>
int(0)
}
*/
->
class Person {
public $name;
public $surname;
private $age;
public function __construct($name, $surname = null, $age = null) {
$this->name = $name;
$this->surname = $surname;
$this->age = $age;
}
public function setAge($age) {
$this->age = $age;
}
public function getAge($age) {
return $this->$age;
}
}
$personObj = new Person('Radu', 'Alex');
$personObj->setAge(24);
echo "<pre>";
var_dump($personObj);
echo "</pre>";
/*
object(Person)#1 (3) {
["name"]=>
string(4) "Radu"
["surname"]=>
string(4) "Alex"
["age":"Person":private]=>
int(24)
}
*/
self::
keyword (instead of arrow ->
)Classname::$counter
(with double colon ::
), same with the functions.$counter
that increments its value every time we create a new object form that class (or every time we call the constructor of that class), and we also create a static function getCounter()
.Person
class, therefore we called the __construct
function two times, so the Person’s static property $counter
has a value of two when echoed.class Person {
public $name;
public $surname;
private $age;
public static $counter = 0;
public function __construct($name, $surname = null, $age = null) {
$this->name = $name;
$this->surname = $surname;
$this->age = $age;
self::$counter++;
}
public function setAge($age) {
$this->age = $age;
}
public function getAge($age) {
return $this->$age;
}
public static function getCounter() {
return self::$counter;
}
}
$personObj = new Person('James', 'May');
$personObj->setAge(58);
$personObj2 = new Person('Jeremy', 'Clarkson', 61);
echo Person::$counter; // 2
echo Person::getCounter(); // 2
Person.php
we will only have the Person
class. Then in another file we will use require_once "classes_folder/Person.php"
.Since PHP 7.4 (latest as Aug-2021), we have possibility to specify the types (int, string, etc) of the properties of a class. For example, if we declare a property with private int $age
, we cannot assign a value of null
to it anymore. However, if we add a question mark ?
before its type (like this private ?int $age
), now that property will also accept null (along ints).
Person
class (in Person.php) and a Student
class (Student.php) that will be inherited from Person class.require_once "Person.php"
(and not just require!!! because when we will require the Student class, it will also require again the Person class!! we would get the error Cannot declare class Person, because the name is already in use
)extends
keyword. Like this class Student extends Person
.<?php
// Person.php
class Person {
public string $name;
public string $surname;
private ?int $age;
public static int $counter = 0;
public function __construct($name, $surname = "", $age = null) {
$this->name = $name;
$this->surname = $surname;
$this->age = $age;
self::$counter++;
}
public function setAge($age) {
$this->age = $age;
}
public function getAge($age) {
return $this->$age;
}
public static function getCounter() {
return self::$counter;
}
}
<?php
// Student.php
require_once "Person.php";
class Student extends Person {
}
<?php
// index.php
require_once "Person.php";
require_once "Student.php";
$personObj = new Person('Oliver', 'Doe', 32);
$studentObj = new Student('Emma', 'Doe', 28);
echo "<pre>";
var_dump($personObj);
echo "</pre>";
echo "<pre>";
var_dump($studentObj);
echo "</pre>";
public function __construct($name, $studentId, $surname = "", $age = 0, )
, or else, if we have them like this ($name, $surname = "", $age = 0, $studentId)
, all 4 of them will be required when we make a new Student object)// Student.php
require_once "Person.php";
class Student extends Person {
public string $studentId;
public function __construct($name, $surname, $age, $studentId) {
parent::__construct($name, $surname, $age);
$this->studentId = $studentId;
}
}
// index.php
require_once "Person.php";
require_once "Student.php";
$personObj = new Person('Oliver', 'Doe', 32);
$studentObj = new Student('Emma', 'Doe', 28, 2321);
echo "<pre>";
var_dump($personObj);
echo "</pre>";
echo "<pre>";
var_dump($studentObj);
echo "</pre>";
$age
property is private
and its in the Person class, we cannot the $age
property in the Student
class that inherits Person
.$age
property can be accessed only in its class Person
$age
property to be accessed in the class that inherits from Person class (namely Student class), we need to set the type of $age
property from private
to protected
protected
, even if $age
was declared in inherited Person class, we can still access that property inside Student class with $this->$age
.Public/Protected/Private are called access specifiers or access modifiers.
Let’s simulate two pages of a website, having index.php
and about.php
files.
<!-- index.php -->
<body>
<nav>
<a href="index.php">Home</a>
<a href="about.php">About</a>
</nav>
<h1>Homepage</h1>
</body>
<!-- about.php -->
<body>
<nav>
<a href="index.php">Home</a>
<a href="about.php">About</a>
</nav>
<h1>About us</h1>
</body>
But, instead of having repetitive code (in this case the menu with <nav>
element), we can store the HTML elements (or JS scripts) in another php file (in this example header.php
), which we will access from both index.php
and about.php
.
We will create a folder called partials
where we will have the /partials/header.php
file.
<!-- /partials/header.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tutorial</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
</ul>
</nav>
</header>
Then, on both index.php
and about.php
we will write at the beginning:
<?php include "partials/header.php"; ?>
<h1>Homepage</h1>
</body>
The difference between include()
and require()
in PHP is that… if we use include()
to include a file that doesn’t actually exists, then the PHP will give us a warning that the file was not found, but it will continue to execute the rest of the code.
require()
will stop the execution of entire PHP file if the file that we are trying to include is not found -> it will give us a Fatal error.
There are also include_once
and require_once
parameters. If we require
/include
a file multiple times, it will get included multiple times. include_once
/require_once
will stop that from happening.
We can also create a partials/footer.php
where we add <footer>
element and we close the </body>
,
<!-- /partials/footer.php -->
<footer>
Copyright © <?php echo date("Y");?> Radu B. All rights reserved.
</footer>
</body>
<!-- index.php -->
<?php require_once "partials/header.php"; ?>
<h1>Homepage</h1>
<?php require_once "partials/footer.php"; ?>
<!-- about.php -->
<?php require_once "partials/header.php"; ?>
<h1>About us</h1>
<?php require_once "partials/footer.php"; ?>
Also, we can write php
files with functions
in them, and include the files in other php
files where we can call the functions.
Example:
<?php
/* /functions/calculate.php */
function calculatePrice($price, $taxes) {
if (empty($taxes)) $taxes = 0.05;
return $price * (1 + $taxes);
}
<?php
/* index.php */
require_once "functions/calculate.php";
echo calculatePrice(100, 0.19); // 119
References from w3schools:
But first, let’s explore some definitions:
Magic constants = constants that change their value based on the execution of context.
echo __DIR__; // C:\xampp\htdocs\tutorials\phpTutorial
echo __FILE__; // C:\xampp\htdocs\tutorials\phpTutorial\index.php
echo __LINE__; // 8 -- this prints the current line number in the current file
Other magic constants unrelated to files:
__FUNCTION__
and __METHOD__
as in PHP 5.0.4 where __FUNCTION__
returns only the name of the function while __METHOD__
returns the name of the class along with the name of the function__CLASS__
gets the current class a function is executed within that classSee more PHP Predefined constants.
We can create directories (folders) on server with mkdir('directory_name', 0755);
(where 0755 represents the permissions).
// Create directory
mkdir('new_dir', 0777);
// Rename directory
rename('new_dir', "new_dir_v2");
// Delete directory
rmdir('new_dir_v2');
The readfile()
function reads a file and writes it to the output buffer. The readfile()
function is useful if all you want to do is open up a file and read its contents.
The fopen()
function is a better method to open files. This function gives you more options than the readfile()
function.
The fopen()
function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen()
on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).
Modes for fopen | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
Another method to get a file and retrieve its content is with file_get_contents('lorem.txt')
.
Examples:
<?php
echo file_get_contents("webdictionary.txt");
echo readfile("webdictionary.txt");
file_put_contents("newly_created_file.txt", "Some content"); // creates and appends "Some content"
echo readfile("newly_created_file.txt"); // Some content
unlink("newly_created_file.txt"); // deletes newly_created_file.txt
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile, filesize("webdictionary.txt"));
fclose($myfile);
The first parameter of fread()
contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.
The fclose()
function is used to close an open file.
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
More examples:
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
The feof()
function checks if the “end-of-file” (EOF) has been reached.
The feof()
function is useful for looping through data of unknown length.
Another example: Download a file from another server and put it on your server:
<?php
$url_to_file = "http://staging.testserver.com/website-archive.tgz";
$new_filename = "website-archive-2021-09-12.tgz";
file_put_contents($new_filename, fopen($url_to_file, 'r') );
echo "The file ". $new_filename ." has been uploaded to your server!";
Read files and folders inside a directory with scandir()
$files = scandir('./');
echo "<pre>";
var_dump($files);
echo "</pre>";
/*
array(5) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(9) "about.php"
[3]=>
string(9) "index.php"
[4]=>
string(8) "partials"
}
*/
More useful functions:
file_exists('sample.txt'); // true / false
is_dir('test'); // false if a directory named 'test' doesn't exist in the same folder as this file
filemtime('test.txt'); // file modification time
filesize('test.txt');
ALL OF THESE FUCTIONS AND MORE HERE - php manual filesystem.
$usersJson = file_get_contents("https://jsonplaceholder.typicode.com/users");
echo($usersJson); // returns a string of 5645 characters...
We can convert the retrieved JSON into an array using json_decode()
:
$usersJson = file_get_contents("https://jsonplaceholder.typicode.com/users");
echo "<pre>";
var_dump(json_decode($usersJson));
echo "</pre>";
We can also specify if we want to convert the elements in the main array as associative arrays with key-value pairs (by default the element within main array are objects - the second parameter is false
by default):
$usersJson = file_get_contents("https://jsonplaceholder.typicode.com/users");
$users = json_decode($usersJson, true);
echo "<pre>";
var_dump($users);
echo "</pre>";
PHP File Upload from HTML Form
cURL (a client for URLs) is a library that lets you make HTTP requests in PHP. Or, cURL is a tool that gives us the possibility to interact (remotely) with other services (eg. with an API).
cURL
file_get_contents
, but this function could have security issues. And also file_get_contents
cannot be used if we want to pass some additional headers to the request, and it also can’t hold any information that we want to send along the request. so that’s why we need to use cURL
.cURL is very powerful in retrieving data from API, making POST request to APIs, uploading and downloading files, and authentication.
Here’s a fast simple cURL example from
// Initialize a connection with cURL (ch = cURL handle, or "channel")
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
// Set the HTTP method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// Return the response instead of printing it out
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Send the request and store the result in $response
$response = curl_exec($ch);
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;
// Close cURL resource to free up system resources
curl_close($ch);
curl_init()
function. The curl_init()
function returns a Resource
type (a CurlHandle
object) (https://www.php.net/manual/en/language.types.resource.php)curl_setopt()
in which we will pass the returned $resource
from curl_init()
along with a CONSTANT called CURLOPT_RETURNTRANSFER
and also a boolean true
value.curl_exec()
that will return a $result
(a string with the whole response)$url = "https://jsonplaceholder.typicode.com/users";
$resource = curl_init($url);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($resource);
echo "<pre>";
var_dump($result);
echo "</pre>";
// returns a string of 5645 characters long...
curl_getinfo($resource)
where we specify the Resource returned from curl_init()
. We will get a looot of info.curl_getinfo()
function’s parameters we could also specify the constant CURLINFO_HTTP_CODE
curl_close($resource)
. Note that after we close the resource, we cannot retrieve any more information using curl_getinfo()
$url = "https://jsonplaceholder.typicode.com/users";
$resource = curl_init($url);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($resource);
$status_code = curl_getinfo($resource, CURLINFO_HTTP_CODE);
echo "<pre>";
var_dump($status_code);
echo "</pre>";
curl_close($resource);
// echo "<pre>";
// var_dump($result);
// echo "</pre>";
curl_init($url);
, we will just call it without parameters, and insert our URL along with multiple options within the call of curl_setopt_array()
function. Here in curl_setopt_array()
we’ll pass:
CURLOPT_URL
with API’s URLCURLOPT_RETURNTRANSFER => true
(true which means we want to get the response)CURLOPT_POST => true
(we specify that the request is a POST method, eg. for creating a new user)CURLOPT_HTTPHEADER
where we tell the API the type of content we are sending, in this case it is JSON, so we specify content-type: application/json
CURLOPT_POSTFIELDS
(the data/user object/associative array that we want to send - json encoded).curl_exec($resource)
with the $resource
that we initiated and set options to, that will return a Result
.$url = "https://jsonplaceholder.typicode.com/users";
$resource = curl_init();
$user = [
'name' => 'Johnny Doe',
'username'=> 'john',
'email' => `john.doe@example.com`
];
curl_setopt_array($resource, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['content-type: application/json'],
CURLOPT_POSTFIELDS => json_encode($user)
]);
$result = curl_exec($resource);
$status_code = curl_getinfo($resource, CURLINFO_HTTP_CODE);
curl_close($resource);
echo "<pre>";
var_dump($status_code);
echo "</pre>";
echo "<pre>";
var_dump($result);
echo "</pre>";
(Optional) Interesting observation when using cURL
to POST data from here: https://www.php.net/manual/en/curl.examples-basic.php
It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format
<?php $params=['name'=>'John', 'surname'=>'Doe', 'age'=>36) $defaults = array( CURLOPT_URL => 'http://myremoteservice/', CURLOPT_POST => true, CURLOPT_POSTFIELDS => $params, ); $ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); curl_close($ch); ?>
This produce the following post header:
--------------------------fd1c4191862e3566 Content-Disposition: form-data; name="name" John --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="surnname" Doe --------------------------fd1c4191862e3566 Content-Disposition: form-data; name="age" 36 --------------------------fd1c4191862e3566--
You need to set
CURLOPT_POSTFIELDS
as follow to produce a standard post headerCURLOPT_POSTFIELDS => http_build_query($params), // Which is: // name=John&surname=Doe&age=36
This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in PHP got both format without problem.
Example: Using PHP’s cURL module to fetch the example.com HTML homepage
<?php
// Initialize a connection with cURL (ch = cURL handle, or "channel")
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
if(curl_error($ch)) {
fwrite($fp, curl_error($ch));
}
curl_close($ch);
fclose($fp);
^^ This can also be used to download files (from another server) and save them on your server.
More examples here: 5 PHP cURL examples that includes:
Web scraping with PHP?