Table of contents
PHP recursively stands for PHP Hypertext Preprocessor. It is a server-side scripting language that allows us to create dynamic websites efficiently. It runs on a server and the result is returned to the browser. So, we need a local server and a database to work with PHP. XAMPP is a popular free cross-platform package that provides us with an Apache server and MariaDB on our local computer.
Getting Started
To start writing PHP code:
You can keep all your PHP projects in 'htdocs' folder located inside the XAMPP installation directory. Then, run it through localhost in the browser.
If you want to run PHP in custom directories, then use the "PHP Server" and "PHP Intelephense" extensions from VS-Code.
PHP Syntax
PHP code always starts with "<?php" and ends with "?>". Eg:
<?php
// PHP Code
?>
We can write the PHP code inside HTML tags. Eg:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
// "echo" is used to display output in PHP
echo "Hello World";
?>
</body>
</html>
Variables
Variables in PHP start with a '$' sign followed by the variable name but we use a 'define()' function to create constants. We don't have to specify the data type for variables and constants. Eg:
<?php
// Variables
$myname = "Giver Khadka"; // String
$age = 21; // Number
$is_student = true; // Boolean
// Constant
define("PI", 3.14);
// Output: I am Giver Khadka
echo "I am" . $myname;
// Output: My age is 21
echo "My age is $age";
// Output: 1
echo "$is_student";
// Output: Value of pi is 3.14
echo "Value of pi is " . PI;
?>
NOTE: The variable value only gets displayed directly when used inside double quotes("$variable"). If used inside a single quote('$variable'), then that will be considered as a string. Eg:
<?php
$myname = "Giver Khadka";
// Output: My name is Giver Khadka
echo "My name is $myname";
// Output: My name is $myname
echo 'My name is $myname';
?>
String
Dot (.) is used to do string concatenation and string[i] is used to retrieve an ith character from a string.
<?php
$firstName = "Giver";
$lastName = " Khadka";
// Output: My name is Giver Khadka
echo "My name is " . $firstName . $lastName;
// Output: My name starts with G
echo "My name starts with $firstName[0]";
?>
String methods like strlen(), strtoupper() and strtolower() return string length, uppercased string and lowercased string respectively.
<?php
// Output: My name is of length: 5
echo "My name is of lenght: " . strlen($firstName);
// Output: My name in capital is: GIVER
echo "My name in capital is: " . strtoupper($firstName);
// Output: My name in small letter is: giver
echo "My name in small letter is: " . strtolower($firstName);
?>
str_replace() is used to replace substrings of a string with another string. It takes 3 arguments:
The substring that is needed to be replaced
A string that replaces that substring
The original string from where the substring will be replaced
<?php
// Output: Replaced string is Student
$replaced = str_replace($firstName, "Student", $firstName);
echo "Replaced string is $replaced";
?>
Math and Arithmetic
PHP also supports basic arithmetic operations like +, -, /, *, etc. Additional operators are **, ++, --, +=, -=, *=, etc.
<?php
$radius = 5;
$pi = 3.14;
// Power Output: Area is 78.52
echo "Area is " . $pi * $radius**2;
$radius++;
// Output: Incremented radius is 6
echo "Incremented radius is $radius";
$radius--;
// Output: Decremented radius is 5
echo "Decremented radius is $radius";
$radius += 20;
// Output: Increased radius is 25
echo "Increased radius is $radius";
$radius -= 10;
// Output: Decreased radius is 15
echo "Decreased radius is $radius";
$radius *= 2;
// Output: Multiplied radius is 30
echo "Multiplied radius is $radius";
$radius /= 4;
// Output: Divided radius is 7.5
echo "Divided radius is $radius";
?>
There are many math functions like floor(), ceil(), pi(), etc.
<?php
// Output: Floor value of pi is 3
echo "Floor value of pi is " . floor($pi);
// Output: Ceil value of pi is 4
echo "Ceil value of pi is " . ceil($pi);
// Output: Predefined value of pi is 3.1415926535898
echo "Predefined value of pi is " . pi();
?>