PHP Overview

 

PHP Overview

PHP is a server-side scripting language, which is used to manage dynamic 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

  1. Handling Forms: PHP can handle form operations. It can gather data, save data to a file and send data through emails.
  2. Database Operations: PHP can also create, read, update and delete elements in your database.
  3. Encryption: It can perform advanced encryption and encrypt data for you.
  4. Dynamic Page Content: It can generate dynamic page content.

 

Basic Syntax PHP

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
?>

Displaying output in php

In php,Output is displayed on the browser using echo as follows:

<?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>

 

PHP Comments

PHP Comments

A comment is a part of the coding file that the programmer does not want to execute, rather the programmer uses it to either explain a block of code or to avoid the execution of a specific part of code while testing.

 

PHP supports several ways of commenting:

Single Line Comments

<?php
// This is a single-line comment
# This is also a single-line comment
?>

 

Multiple-Line Comments

<?php
/*
This is a
multiple line
Comment.
*/
?>

  

Variables in PHP

 

Variables in PHP

Variables are containers that can store information which can be manipulated or referenced later by the programmer within the code.

 

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.


  • All variables should be denoted with a 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.
  • Variable names can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • Variables must start with a letter or the underscore “_” character.
  • Variables are case sensitive
  • Variable names cannot start with a number.

 

For Example:

<?php
$txt = "Hello world!";  # Type String
$x = 5;                        # Type int
$y = 10.5;         # Type Float
?>

 

Variable Scope

 

Variable Scope

The scope of the variable is the area within which the variable has been created. Based on this a variable can either have a local scope or a global scope or a static scope in PHP.

 

Global Variable:

A variable which was created in the main body of the code and that can be accessed anywhere in the program is called Global Variable. Global variables can be directly accessed or used in or outside of a function with GLOBAL keyword before variable. However, we can also call them without the global keyword. 


For Example:

<?php  
    $name = "Maxon";        //Global Variable  
    function global_var()  
    {  
        global $name;  
        echo "Variable inside the function: ". $name;  
        echo "</br>";  
    }  
    global_var();  
    echo "Variable outside the function: ". $name;  
?>  

Output:

Variable inside the function: Maxon
Variable outside the function: Maxon

 

Local Variable:

A local variable is created within a function and can be only used inside the function. This means that these variables cannot be accessed outside the function, as they have local scope.

For Example:

<?php  
    function mytest() 
    {  
        $capital = "Delhi";  
        echo "Capital of India is: " .$capital;  
    }  
    mytest(); //Calling the function
    //using $capital outside the function will generate an error  
    echo $capital;  
?>  

Output: 

Capital of India is: Delhi                                                            Notice: Undefined variable: capital in D:\xampp\htdocs\program\var.php on line 28

 

Static Variable:

PHP has a feature that deletes the variable once it has finished execution and frees the memory. When we need a local variable which can store its value even after the execution, we use the static keyword before it and the variable is called static variable. 

These variables only exist in a local function and do not get deleted after the execution has been completed.

 

For Example:

<?php  
    function static_var()  
    {  
        static $num1 = 3;    //static variable  
        $num2 = 6;          //Non-static variable  
        //increment in non-static variable which will increment its value to 7
        $num1++;  
        //increment in static variable which will increment its value to 4 after first execution and 5 after second execution
        $num2++;  
        echo "Static: " .$num1 ."</br>";  
        echo "Non-static: " .$num2 ."</br>";  
    }  


//first function call  
    static_var();  

//second function call  
    static_var();  
?>  

Output:

Static: 4
Non-static: 7
Static: 5
Non-static: 7