PHP stands for hyper text pre-processor
It is a server side scripting language.
It is used to create dynamic websites.
It supports OOP from version 5
PHP Tags
The php code is enclosed between <?php and ?>
Example,
<?php # opening tag
# code
?> # closing tag
Hello World in PHP
We can use echo or print statements to output any data
<?php
# using echo statement
echo 'Hello world';
# using print function
print('Hello world');
?>
The echo statement can also be used with parentheses
<?php echo('Hello world'); ?>
Variables in PHP
The variables are declared using a $ symbol preceding the variable name
For example,
<?php
# declaring a variable
$username = 'Joe';
?>
Data Types
- Integer
- Double (float)
- String
- Boolean
- Array
<?php
# integer
$total = 24;
# double (float)
$percent = 91.1;
# string
$name = 'Joe';
# boolean
$is_even_number = true;
# array (single dimension)
$students = array('Harry', 'John', 'Doe', 'Kalle');
?>
Loops
- for
- while
- foreach
<?php
# for loop
for ($i = 0; $i <=10; $i++) {
echo $i;
}
# while loop
$i = 0;
while ($i <=10) {
echo $i;
$i++;
}
# foreach loop
$fruits = array('Apple', 'Mango', 'Banana');
foreach ($fruits as $fruit) {
echo $fruit;
}
?>
Conditional statements
if - else example,
<?php
$i = true;
if ($i) {
echo 'i is equal to true';
} else {
echo 'i is not equal to true';
}
?>
if - else if - else example,
<?php
if (condition_1) {
# task 1
} else if (condition_2) {
# task 2
} else {
# task 3
}
?>