PHP is a server-side programming language that powers almost 80% of websites on the internet (you can get the precise numbers here, you get that information below the "Server-Side Programming Languages" section). Even though Python is taking over the internet at an insane rate, PHP is still superior but simple language to learn.
The best thing about PHP is that you can write it in between the normal HTML code, the only condition is that the extension of that file must be .php and not .html
An example would be the following:
<html>
<head>
<title>PHP in HTML</title>
</head>
<body>
<?php
echo "Hello Idiomatic Programmer";
// This statement will print "Hello Idiomatic Programmer"
// in a webpage
?>
</body>
</html>
In the above code, the bold code represents the PHP code which is embedded in an HTML code.
How to install PHP?
Unlike other languages, PHP only runs on a web server. Hence, the most important component to run a PHP code is a web server: Web server refers to server software or hardware dedicated to running said software, that can serve contents to the World Wide Web (WWW).
The most popular web-server that runs PHP is called Apache HTTP Server.
The best way to install this and all the other required platforms and database dependencies is by installing a software called XAMPP (which stands for Cross-Platform (X) Apache MariaDB PHP Perl). As it is mentioned in the name itself, XAMPP is a bundle of all the necessary software for PHP development. XAMPP also provides support of Perl, another programming language similar to PHP.
You can download and install XAMPP, if you need the instructions to install XAMPP in your system, you can check out this video.
Basic Syntax
Since PHP is not a compiled language and can be embedded inside an HTML code, we have to tell the interpreter that this code is a PHP code. We can do this by enclosing the PHP code inside a special piece of code:
<?php
// The PHP code is written inside here.
?>
One of the best thing about PHP that I feel is useful for a beginner is that in PHP, all identifiers are not case sensitive. This includes all keywords like echo, if, while, switch, etc., functions, classes, even user-defined ones. Consider the following code:
<?php
echo "Hello, Idiomatic Programmer!";
Echo "Hello, Idiomatic Programmer!";
ecHo "Hello, Idiomatic Programmer!";
EHCO "Hello, Idiomatic Programmer!";
?>
Every statement in the above code will work without any exception or error.
Also, by this point, you must have noticed that all the statements in PHP end with a semi-colon and does not follow the indentation rule like python (although it is advisable to write code with the indentation for “Good Code Practices”.
Comments
In PHP, you can write comments in three ways, consider the following code:
<?php
// This is a single-line comment
# This is also a single-line comment
/* This is a multi-line comment,
you can use this comment when you have to
write a small documentation for some function. */
?>
There is one other use of multi-line comment, you can use this comment to something in between a statement, consider the following code:
<?php
echo 5 /* + 5 */ + 10;
// This statement will print 15
?>
You can use multi line comment to comment something in the middle of a statement.
Variables
Just like python and other modern programming languages, PHP does not have any data-type for its variables. In PHP, variables are declared and initialised by adding a $ (dollar sign) in front of the identifier, consider the following code:
<?php
$num1 = 5;
$num2 = 7;
echo $num1 + $num2;
// This program prints 12.
?>
Variable Scope
The scope of a variable is the part of the script where the variable can be referenced/used.
In PHP, a variable can have one of three scopes:
- Local
- Global
- Static
1. Local variable scope
Any variable that is declared inside a block (that is in between { and }) is considered as a variable with Local Scope. This variable is only available within that block and no other code outside that block can access the local variable.
2. Global variable scope
Any variable that is declared directly inside the PHP root block (which is ) is considered as a variable with Global Scope. This variable is accessible anywhere in the code. If you want to use a global variable inside a block and that block has a local variable with the same identifier then you have to use global keyword to address the global variable. Consider the following code:
<?php
$x = 5; // This is a global variable
while (1) {
$x = 10; // This is a local variable
echo $x; // This will print 10.
global $x;
echo $x; // This will print 5.
break; // This is used to run the loop only ones.
}
?>
The keyword break is used to get out of a block without proceeding further.
Printing something in PHP
In case you haven’t noticed till now, in PHP you can print (or you can say write) something on the webpage by using echo statement, consider all the above code for its usage.
In addition to that, you can also use print or print() statement, consider the following example:
<?php
print "Hello, Jello";
print("This is a print statement (method)");
?>
Operators
Just like any other programming language, PHP also has some operators that would allow a programmer to perform ALU operation on a higher level. Let’s take a look at these operators:
1. Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
2. Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
3. Increment / Decrement Operators
The PHP increment/decrement operators are used to increment/decrement a variable’s value.
4. Logical Operators
The PHP logical operators are used to combine conditional statements.
5. String Operators
PHP has two operators that are specially designed for strings.
6. Array Operators
The PHP array operators are used to compare arrays.
If you are familiar with C programming language, then you PHP find PHP a lot similar to C or C++. If you don’t agree with me yet, I bet you will when you see how if…else…if statements are done in PHP.
Control Statements
1. if statement
In PHP, if statements are fairly straightforward. Consider the following example:
<?php
$x = 5;
if ($x > 3){
print "Go On";
}
?>
2. if…else statement
If the condition in if statement is false, then the program pointer will move to the else block and this can be written as:
<?php
$x = 5;
if ($x > 3){
print "Go On";
} else {
print "You cannot move forward";
}
?>
3. if…else…elseif
If you want to check multiple conditions then, you can use this statement and the usage is as follows:
<?php
$x = 5;
if ($x > 3){
print "Go On";
} elseif ($x == 3) {
print "You just missed :(";
} else {
print "You cannot move forward";
}
?>
4. switch statements
Managing multiple if…elseif…else statements may become complicated. To solve this problem, switch statements are used to solve this problem. Consider the following example:
<?php
$x = 5;
switch($x){
case 3: print "You just missed :(";
break;
case $x > 3: print "Go On";
break;
default: print "You cannot move forward";
break;
}
?>
This is how it works: First, we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.
5. while loop
The while loop executes a block of code as long as the specified condition is true. Consider this example:
<?php
$x = 1;
while($x <= 5) {
print "The number is: $x
";
$x++;
}
?>
6. do…while loop
Just like while loop, the block executes the code as long as the specified condition is true. But in do…while loop, the loop will run at least one time even if the first condition is wrong. This is because do…while loop is an exit controlled loop which means the loop checks the condition after the iteration is over. Consider the following example:
<?php
$x = 1;
do {
print "The number is: $x
";
$x++;
} while ($x <= 5);
?>
7. for loop
For loop is an entry control loop which means that the condition is checked before entering the loop, just like while loop. Consider this example:
<?php
for ($x = 0; $x <= 5; $x++) {
print "The number is: $x
";
}
?>
8. foreach loop
The foreach loop works only on arrays and is used to loop through each key/value pair in an array. For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by 1 until it reaches the last array element. Consider the following example:
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
print "$value
";
}
?>
Functions
A function is a block of code that is often defined by its role within a greater code structure. Specifically, a function contains a unit of code that works on various inputs, many of which are variables, and produces concrete results involving changes to variable values or actual operations based on the inputs.
In PHP, you can create a function using the function keyword, for example:
<?php
function functionName(arguments) {
# code to be executed;
}
?>
Information can be passed to functions through arguments. An argument is just like a variable. For example:
<?php
function sum($a, $b){
print ($a + $b);
}
sum(5, 6);
// Prints 11
?>
You can also return the result using the return keyword:
<?php
function sum($a, $b){
return ($a + $b);
}
print sum(5, 6);
// Prints 11
?>
These arguments can be optional by assigning a default value to them, for example:
<?php
function sum($a, $b = 7){
return ($a + $b);
}
print sum(5);
// Prints 12
?>
There are more than 1000 built-in functions in PHP, which you can check out at the official PHP documentation
So that is all for PHP basics, if you have any query you can leave a comment and I’ll try to solve your query as soon as possible.
Thank You.