PHP Introduction | PHP Tutorial | PHP Basics

Introduction to PHP:

PHP was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page but now it stands for the recursive acronym, PHP: Hypertext Preprocessor. Popular websites like Facebook, Yahoo, Wikipedia etc. are developed using PHP.

PHP is a widely-used open source general-purpose scripting language for web development. It allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites. PHP is mainly focused on server-side scripting so you can collect form data, generate dynamic page content, or send and receive cookies. Code is executed in a server that is why you’ll have to install a sever-like environment enabled by programs like XAMPP which is an Apache distribution.

 

XAMPP Setup:

XAMPP is a free and open source cross-platform web server solution developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages. In order to make your PHP code execute locally, first install XAMPP.

  • Download XAMPP
  • Install the program (check the technologies you want during installation)
  • Open XAMPP and click on "Start" on Apache and MySQL (when working with databases)


Characteristics of PHP:

Five important characteristics make PHP's practical nature possible:

  • Simplicity  
  • Efficiency
  • Security  
  • Flexibility  
  • Familiarity


Advantages of PHP:

Since PHP is designed for the web in the first place, it brings many advantages to web development:

  • Simple: PHP is quite easy to learn and get started.
  • Fast: PHP websites typically run very fast.
  • Stable: PHP is stable since it has been in existence for a long time.
  • Open-source and free: PHP is open source and free. It means that you don’t have to pay a license fee to use PHP to develop software products.
  • Community support: PHP has an active online community that helps you whenever you face an issue.


Uses of PHP:

Here are a few applications of PHP:

Server-side scripting: This is the most used and main target for PHP. You need three things to make this work the way you need it. The PHP parser (CGI or server module), a web server and a web browser. You need to run the web server where. You can access the PHP program output with a web browser, viewing the PHP page through the server.

Command line scripting: You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.

Writing desktop applications : PHP may not the very best language to create a desktop application with a graphical user interface, but if you know PHP very well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write such programs. You also have the ability to write cross-platform applications this way. 


How PHP Works:

First, the web browser sends an HTTP request to the web server, e.g., index.php.

Second, the PHP preprocessor that locates on the web server processes PHP code to generate the HTML document.

Third, the web server sends the HTML document back to the web browser.



Variables in PHP:

  • Any type of variable in PHP starts with a leading dollar sign ($).
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.  
  • Variables can, but do not need, to be declared before assignment.
  • Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a number or a string of characters.  
  • Variables used before they are assigned have default values.


The main data types used to construct variables are:

  • Integers: whole numbers like 23, 1254, 964 etc
  • Doubles: floating-point numbers like 46.2, 733.21 etc
  • Booleans: only two possible values, true or false
  • Strings: set of characters, like ‘PHP supports string’
  • Arrays: named and indexed collections of other values
  • Objects: instances of predefined classes

 


Operators and Expressions:

 PHP language supports following type of operators.  

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Conditional (or ternary) Operators



Conditional Statement:

Conditional statements are used to execute different code based on different conditions.


If Statement:

The if statement executes a piece of code if a condition is true.


Syntax:

if (condition)

{

// code to be executed in case the condition is true

}

 

Example:

 

<?php

$year=2020;

if($year %4==0)

{

    echo "$year is a leap year";

}

?>

 

Output:

2020 is a leap year

 

 

If. . . Else statement :

The If. . . Else statement executed a piece of code if a condition is true and another piece of code if the condition is false.

 

Syntax:

 if (condition)

{

 // code to be executed in case the condition is true

}

else

{

// code to be executed in case the condition is false

}


Example:

<?php

$num=12;

if($num%2==0)

{

    echo "$num is even number";

}

else

{

    echo "$num is odd number";

}

?>

 

Output:

12 is even number

 

 

If. . . elseif. . . else statement:

 This kind of statement is used to define what should be executed in the case when two or more conditions are present.

 

Syntax:

 if (condition1)

 {

 // code to be executed in case condition1 is true

 }

elseif (condition2)

{

// code to be executed in case condition2 is true

}

else

{

// code to be executed in case all conditions are false

 }


Example:

<?php 

$marks=80;     

if ($marks<40)

{   

        echo "fail";   

}   

else if ($marks>=40 && $marks<50)

{   

        echo "D grade";   

}   

else if ($marks>=50 && $marks<60)

{   

          echo "C grade";  

}   

else if ($marks>=60 && $marks<70)

{   

          echo "B grade";  

}   

else if ($marks>=70 && $marks<80)

{   

          echo "A grade";   

}

else if ($marks>=80 && $marks<100)

{   

          echo "A+ grade";  

else

{   

        echo "Invalid input";   

}   

?> 


Output:

A+ grade



Iteration in PHP (Loops in PHP):

 

In PHP, just like any other programming language, loops are used to execute the same code block for a specified number of times. Except for the common loop types (for, while, do. . . while), PHP also support foreach loops.

 

For Loop:

The for loop is used when the programmer knows in advance how many times the block of code should be executed. This is the most common type of loop encountered in almost every programming language.


Syntax:

 for(initialization; condition; update expressein)

{  

//code to be executed  

}  

 

Example:

<?php

for($x=1;$x<=10;$x++)

{

    echo "The number is: $x <br>";

}

?>

 

 Output:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

 

 

Example:

 <?php

for($x=10;$x<=100;$x=$x+10)

{

    echo "The number is: $x <br>";

}

?>

 

Output:

The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
The number is: 60
The number is: 70
The number is: 80
The number is: 90
The number is: 100

 

 

While Loop:

The while loop is used when we want to execute a block of code as long as a test expression continues to be true.


Syntax:

 while(condition)

{  

//code to be executed  

}  

 

Example:

<?php

$x=1;

while($x <=10)

{

    echo " The number is : $x <br>";

    $x++;

}

?>

Output:

The number is : 1
The number is : 2
The number is : 3
The number is : 4
The number is : 5
The number is : 6
The number is : 7
The number is : 8
The number is : 9
The number is : 10

 

 

Do. . . While loop:

The do...while loop is used when we want to execute a block of code at least once and then as long as a test expression is true.


Syntax:

do

{  

//code to be executed  

}

while(condition);  

 

Example:

<?php

$x=1;

do

{

  echo "The number is: $x <br>";

  $x++;

}

while($x<=10);

 

?>

Output:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

 

 

 Foreach Loop:

It works only on array and object. It will issue an error if you try to use it with the variables of different datatype. It provides an easiest way to iterate the elements of an array.

 

Syntax:

foreach ($array as $value)

{
 code to be executed;
}

 

Example:

<?php

//declare array

$days= array("Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday");

//access array elements using foreach loop 

foreach($days as $value)

{

    echo "$value <br>";

}

?>

 

Output:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday




Post a Comment

0 Comments