04 - Basics of PHP

PHP scripts can be written using any text editor like: Notepad, Notepad++, simple text or vi. PHP files end with extension .php. This extension signifies to the server that it needs to parse the PHP code before sending the resulting HTML code to the client’s web browser screen.

Use of start and end tag

Every PHP script exists in between start and end tag. <?php tag is the start tag and ?> is the end tag of php script. PHP interpreter ignores all other code outside of these tags.

In a PHP program we can use multiple start and end tags.

PHP is case sensitive scripting language. Every PHP statement always ends with a semicolon.

Sample program of PHP:

    <TITLE> My First PHP Program </TITLE>
    echo "Hello World!!!";

Save above code in file with extension hello.php and then open this file in web browser, it will show the following output:

We can use many blank lines or white spaces in PHP code that does not create any error in program.

Comments in PHP scripts:

Comments are specifically used to provide additional information to users. The lines those are written in between the comments are ignored by interpreter. In PHP comments are of following types:

Single line comment:

This type of comment is used to make single line as comment. For it we use //

Symbol just like C++, Java and other programming languages.

Multiline comment:

This type of comment is used to make a block of lines as comment just like C language, we use /*...................*/ symbol for this.

Special characters in PHP

Sometimes we need to enter special characters like newline or tab in our script. In PHP we can use following special characters:



New line




Escape character


Working with variables and constants

A constant is the memory location which contains the value that is fixed during the program execution. Constant is typically named with capital letter. Constant name begin with a letter or an underscore and cannot begin with a number. Names are case sensitive.

<TITLE> My First PHP Program </TITLE>
        define("Lang", "PHP language");         // constant Lang declaration
        echo "My Favourite scripting language is: "; 
        echo Lang;

Output of above code is:

Variables in PHP:

Variables are the memory locations used to store some values. These values meant to be changed during the program execution. In PHP variables are loosely typed, means there is no need to define data type of variables. Variables do not need to be defined or declared and simply assigned when needed. In PHP variables are prefixed with a $(dollar sign) and are case sensitive. The first letter of a variable name must be an underscore or letter and it cannot be a number. In PHP5 at the time of function calling, all the variables are passed by reference.

<title> My PHP page</title>
        define("Lang","PHP Language");
        echo "My Favourite scripting language is";
        echo Lang;
        echo "<br>";
        Echo "I am using PHP $version";

Output of above code is:

Data Types in PHP

In PHP following types of basic data types are available:

String: text or information treated as text (numbers enclosed in quotes are also string)

Integer: whole numbers

Float: decimal numbers

Boolean or Bool: true or false

Introduction to typed languages

Languages are of following types:

              Strongly typed and weakly typed

        C++, java like languages are strong typed with predefined types of data

PHP is weakly typed as there is no need to define data type of variables.

              Static and dynamic type checking

In static type all data types are defined before running the program.

In dynamic variables are allowed to assign values at runtime.

               Type juggling and type casting

Type juggling is the process in which PHP rules are automatically applied

Where as in type casting we convert one data type into some other type of data.

Operators in PHP

In PHP operators can be broadly classified in following types:

           Arithmetic operators

These operators are used to do mathematical operations. Following are the types of arithmetic operators:

*Multiplicationa* b

         Comparison operators

These types of operators are used to compare two values. These operators return result in form of true or false. Following types of comparison operators are in PHP

==Check whether two values are equal or nota==b
!=Check whether two values are not equala!=b
Check if the value is less thana<b
Check if the value is greater thana>b
<=Check if the value is less than or equal toa<=b
>=Check if the value is greater than or equal toa>=b

          Logical operators

Many times the situation occurs when we have to take decision depends on multiple conditions; at that time we should use logical operators to combine the conditions.

Following types of logical operators found in PHP:

andLogical and, returns true if both the given conditions are true
orLogical or, return true if any of the given condition is true
&&Logical and, returns true if both the given conditions are true
||Logical or, return true if any of the given condition is true
!Logical not operator, used to reverse the result. Means, if the condition is true it will return false and if the condition is false it will return true.

      Assignment operators

These types of operators are used to assign value of right hand side expression or variable in left hand side variable. Following are the types available of assignment operators in PHP:

=Assign value in LHS variable$a=$b
+=Assign value in LHS variable with addition of some value$a+=$b is same as $a=$a+$b
-=Assign value in LHS variable after subtraction of some value$a-=$b is same as 
*=Assign value in LHS variable after product of some value$a*=$b is same as 
/=Assign value in LHS variable after division of some value$a/=$b is same as 
%=Assign value in LHS variable with remainder operation of some value$a%=$b is same as 
.=Assign value in LHS string variable the concatenation of some string value$a.=”hello” is same as 
$a=$a .“hello”


           Conditional or ternary operators

This operator belongs to category of ternary operator. In this operator three operands are used, first operand is an expression that is to be evaluated, if this expression is true then the expression defined in second operand will be executed otherwise the expression defined in third expression will be executed.

?:Conditional operator$a>$b? $a:$b
If $a is greater than $b, it will return $a otherwise $b will be returned

Like us on Facebook