PHP
PHP
web pages, databases and build websites with features like session tracking
and e-commerce. On a day of 1995, Rasmus Lerdorf unleashed the first
version of “Hypertext Preprocessor” also known as the PHP language. It is
also integrated with several popular databases like MySQL, PostgreSQL,
Microsoft SQL Server, Oracle etc.
Uses of PHP
PHP can perform several system functions like opening files, CRUD
operations on data stores, general-purpose scripting, etc. Besides system
operations, there are also other uses like
a. Handling Forms: PHP can handle form operations. It can gather data,
save data to a file and send data through emails.
b. Database Operations: PHP can also create, read, update and delete
elements in your database.
c. Encryption: It can perform advanced encryption and encrypt data for you.
d. Dynamic Page Content: It can generate dynamic page content.
A PHP script can be written anywhere inside the HTML document. A PHP
script starts with <?php tag and ends with ?>. We can write our logic inside
this tag and it will be executed accordingly.
<?php
// PHP code goes here
?>
<?php
echo "hello";
?>
Hello World
A basic PHP Hello World program looks something like this. We will use a
built-in PHP function “echo” to output the text “Hello World!” on our
webpage.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
In PHP, we don’t need to declare the variable type explicitly. The type of
variable is determined by the value it stores. There are some important
things to know about variables in PHP.
<?php
$txt = "Hello world!"; # Type String
$x = 5; # Type int
$y = 10.5; # Type Float
?>
String
Integer
Float
Boolean
Array
NULL
1. String
<?php
$x = "Hello world!";
echo $x;
?>
2. Integer
<?php
$x = 55;
var_dump($x);
?>
3. Float
<?php
$x = 52.55;
var_dump($x);
?>
4. Boolean
<?php
$x = true;
$y = false;
?>
5. Array
6. NULL
Null is a special data type with only one value which is NULL. In PHP, if a
variable is created without passing a value, it will automatically assign itself
a value of NULL.
<?php
$x =null;
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
1. if Statement.
2. if-else Statement
3. If-elseif-else Statement
4. Switch statement
1. if Statement
This statement executes the block of code inside the if statement if the
expression is evaluated as True.
Example:
<?php
$x = "22";
if ($x < "20") {
echo "Hello World!";
}
?>
Copy
2. if-else Statement
This statement executes the block of code inside the if statement if the
expression is evaluated as True and executes the block of code inside the
else statement if the expression is evaluated as False.
Example:
<?php
$x = "22";
if ($x < "20") {
echo "Less than 20";
} else {
echo "Greater than 20";
}
?>
Copy
3. If-else-if-else
This statement executes different expressions for more than two conditions.
Example:
<?php
$x = "22";
if ($x == "22") {
echo "correct guess";
} else if ($x < "22") {
echo "Less than 22";
} else {
echo "Greater than 22";
}
?>
Copy
4. Switch Statement
<?php
$i = "2";
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
1. while Loop
2. do-while Loop
3. for Loop
4. foreach loop
1. While Loop
The While loop in PHP is used when we need to execute a block of code
again and again based on a given condition. If the condition never becomes
false, the while loop keeps getting executed. Such a loop is known as an
infinite loop.
Example:
<?php
$x = 1;
while($x <= 10) {
echo "The number is: $x <br>";
$x++;
}
?>
Copy
2. Do-While Loop
<?php
$x =10;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 9);
?>
3. For Loop
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
4. Foreach loop
The foreach loop in PHP can be used to access the array indexes in PHP. It
only works on arrays and objects.
Example:
<?php
echo "Welcome to the world of foreach loops <br>";
$arr = array("Bananas", "Apples", "Harpic", "Bread",
"Butter");
foreach ($arr as $value) {
echo "$value <br>";
}
?>
Function Basics
Function arguments are variables of some supported data type that are
processed within the body of the function. It can take input as an argument
and return value.
PHP has more than 1000 built-in functions, and in addition, you can also
create your own functions.
Advantages:
User Defined Functions: Apart from built-in functions, We can also create
our own functions and call them easily.
A user-defined function looks something like this:
<?php
Function functionname(){
//Code
}
functionname(); // Calling Function
?>
Note: A function name should only start with letters and underscore only. It
can’t start with numbers and special symbols.
Example:
<?php
function helloMsg() {
echo "Hello world!";
}
helloMsg(); // call the function
?>
Function Arguments
Function Arguments: Argument is just like a variable which can be used to
pass through information to functions.
PHP supports Call by Value, Call by Reference, Default Argument Values
and Variable-length argument.
1. Call by Value
In Call by Value, the value of a variable is passed directly. This means if the
value of a variable within the function is changed, it does not get changed
outside of the function.
Example:
<?php
function incr($i)
{
$i++;
}
$i = 5;
incr($i);
echo $i;
?>
Copy
Output:
Copy
2. Call by Reference
<?php
function incr&$i)
{
$i++;
}
$i = 5;
incr($i);
echo $i;
?>
Copy
Output:
Copy
3. Default Argument Values
If we call a function without arguments, then PHP function takes the default
value as an argument.
Example:
<?php
function Hello($name="Aakash"){
echo "Hello $name <br>";
}
Hello("Rohan");
Hello();//passing no value
Hello("Lovish");
?>
Copy
Output:
Hello Rohan
Hello Aakash
Hello Lovish
Copy
<?php
function add(...$nums) {
$sum = 0;
foreach ($nums as $n) {
$sum += $n;
}
return $sum;
}
echo add(1, 2, 3, 4);
?>
Copy
Output:
10
Arrays
An array is a collection of data items of the same data type. And it is also
known as a subscript variable.
Example:
<?php
$age = array("Virat"=>"35", "Arshdeep"=>"37", "Rohit"=>"43");
echo "Virat is " . $age['Virat'] . " years old.";
?>
Copy
These are arrays with a numeric index where values are stored and
accessed in a linear fashion.
Example:
<?php
$bike = array("TVS", "YAMAHA", "RAJDOOT");
echo "I like " . $bike[0] . ", " . $bike[1] . " and " .
$bike[2] . ".";
?>
Copy
2. Associative Array
These are arrays with string as an index where it stores element values
associated with key values.
Example:
<?php
$age = array("Ben"=>"35", "Stokes"=>"37", "Jimi"=>"43");
echo "Ben is " . $age['Ben'] . " years old.";
?>
Copy
3. Multidimensional Arrays
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold:
".$cars[0][2].".<br>";
?>