Introduction

Introduction

Installation

Syntax

Comments

Output

Variables

Conditional Statements

Loops

End

PHP Tutorial For Beginners

Introduction

PHP stands for Hypertext Preprocessor, it is a scripting language. PHP was created by the Rasmus Lerdorf in 1995. It has been 25 years since it's first appearance. PHP was called as Personal Home Page back then. PHP code is processed on the server by a PHP interpreter. PHP is a dynamic and weakly typed language. It is a server side scripting language, it is used to generate dynamic content on a web page.


Installation

Install XAMPP to easily setup a server on your computer, you can download XAMPP installer here. It is the most popular PHP development environment. XAMPP is a package of Apache server, MariaDB, PHP and Perl.

If you want to manually install PHP, you can download the latest version of PHP from downloads page of it's official website and refer to the installation and configuration manual.


Syntax

PHP code is always enclosed between <?php and ?> tags. In PHP all the keywords, classes, functions are case-insensitive, and the variables are case-sensitive.

<?php
    // code
?>

Comments

Comments are ignored when the script is executed. Comments are used to make your code understandable to the people who read the code. The comments may be of a single line or multiple lines.

In PHP a single line comment is represented using // or # 

<?php
    // a comment
    # another comment
?>

A multi line comment is represented using /* */

<?php

/*
   this is a comment
   that spans over multiple lines
*/

?>

Output

To display an output, we can use echo or print statements.

<?php 

// using echo statement
echo 'Hello world';

// using print function
print('Hello world');

// echo function
echo('Hello world');

?>

Variables

The variables are used to store the data temporarily. As PHP is a loosely (weakly) typed language, the variables need not to be declared with their type.

<?php

// declaring a variable
$person_name = 'John Doe';

?>

In the above example, we have declared a variable of type string.

You can use double quotation marks " " or single quotation marks ' ' to denote a string value.


Conditional Statements

If statement

The if statement executes the code inside it if the condition given is true.

<?php

if (condition) {
   // code to execute
}

?>

If else statement

If the condition in the if statement is false, the code inside the else statement is executed.

<?php

if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

?>

Else if statement

It is used when there is a need to check multiple conditions

<?php

if (condition) {
    // code to be executed if condition is true
} elseif (condition) {
    // code to be executed if first condition is false
} else {
    // code to be executed if both conditions are false
}

?>

Switch statement

The switch statement checks each of the cases mentioned, if any of the case is satisfied, the code inside it is executed, if all the cases conditions are false, the default case is executed. Each case must end with a break statement to avoid looping to the next case.

<?php

switch (v) {

  case label_one:
    // code to be executed if v = label_one
    break;

  case label_two:
    // code to be executed if v = label_two
    break;

  default:
    // code to be executed if all the cases are not satisfied

}

?>

You can use as many cases as you want, here we have taken only two cases to keep it simple.


Loops

In PHP we have four different types of loops

For loop

<?php

for (initialisation; condition; counter) {
    // code to be executed
}


// example
for ($x = 1; $x <= 10; $x++) {
    echo $x;
}
// prints 1 to 10

?>

While loop

<?php

initialisation
while (condition) {
    counter
    // code to be executed
}

// example
$n = 0;
while ($n < 10) {
    $n++;
    echo $n;
}
// prints 1 to 10

?>

Do while loop

<?php

initialisation
do {
    counter
    // code to be executed
} while (condition);

// example
$i = 0
do {
    $i++;
    echo $i;
} while ($i < 10);
// prints 1 to 10

?>

A do while loop executes atleast once, even if the condition is not satisfied. This is because, we are executing the code inside the do first and then checking the condition inside the while.

Foreach loop

<?php

//example
$brands = array('Nike', 'Puma', 'Adidas');
foreach ($brands as $brand) {
    echo $brand;
}

?>

A foreach loop iterates through collectives. In the above example, each element of $brands array is output using foreach loop


If you found this article helpful, feel free to share it.

Tags