Using PHP session variable is a mechanism to store user’s data in server. And sessions will last till we are on the site, it will be destroyed when the user quit the site.
One main example of session usage is login information. When a user login to a site with his user name and password, sessions will be created for that user in server. By using these session variables servers and our PHP programs identify each user.

For each user session will create a unique ID , and other session variables and values are stored based on this ID.

How to start a PHP Session

For storing theWe need to start session before going to store any session variables and all. Function using for this is session_start(). It must be appear in our page before starting any output on the page. Better to use before our<html> tag.
Syntax for a PHP session start will be :
<?php session_start(); ?>

Storing a value in session
$_SESSION is the variable for session. We need to specify the the custom name for each session variable and value associated with that.
For example , if we want to store the user name and password of a user in PHP session, we can store like as below
$_SESSION['user_name']=$username;
$_SESSION['password']=$password;

Then user name will stored in the session variable $_SESSION['user_name'] and password will be stored in the session variable $_SESSION['password']. And we can retrieve these values in any other pages for that domain as long as that user stay on that site.
See the sample program to create and retrieve the PHP session variables

<?php php session_start();?>
<html>
<?php
$username="syam";
$password="mysql";
$_SESSION['user_name']=$username;
$_SESSION['password']=$password;
?>
<body>
<?php
echo "Hi ".$_SESSION['user_name']. " Your password is ".$_SESSION['password'];
?>
</body>
</html>

Output of this program will be :
Hi syam Your password is mysql

Its better to check session variable is stored or not before retrieving its value for any operation to avoid any errors or other issues.
See the sample program with checking session exist or not for counting the page views of a visitor

<?php php session_start();?>
<html>
<?php
$username="syam";
$_SESSION['user_name']=$username;
if(isset($_SESSION['page_views']))
{
$_SESSION['page_views']=$_SESSION['page_views']+1;
}
else
{
$_SESSION['password']=1;
}
?>
<body>
<?php
echo "Hi ".$_SESSION['user_name']. " You have visited ".$_SESSION['page_views']." pages";
?>
</body>
</html>

In this example output will be :
Hi syam You have visited 1 pages
OR
Hi syam You have visited 2 pages
etc.. for each time he visit the page with this code his session value for page visit count will be increment by one. If its his first visit our session exist check will find that session is not existing and will assign the value 1, otherwise it will increment.

PHP Session Destroy

If we want to destroy the session, there are two functions are for this

  • unset()
  • session_destroy()

Difference between these two are

  • Using unset(0 function we can destroy one particular session variable for that user
  • using session_destroy() we can destroy all the sessions for that user

unset($_SESSION['username']) will destroy only the session variable used for storing the username of that user. If we used session_destroy() function all the session variables including $_SESSION['username'] will be destroyed.

There are several other functions used with PHP session , we will discuss about those in our later posts.

We can include other files also in our PHP programs. Its for inserting the codes in other files in the current PHP program. Its a good practice to do make individual files with a piece of code which we need to use in several other pages of our project. Instead of writing the same codes in all pages we can include that file with the same code. Database connection code, reusable function and some global declarations , common header or footer or sidebar etc can be saved in a separate file , and we need to only include that file in the required page.

Here we are going to learn about various statements using for include a file.
There are mainly four statements are there for this in PHP

  • include
  • require
  • include_ once
  • require_ once

include” and “require” statements will simply add the contents of the includes file.Its usage is like include ‘filename’; or require “filename”;( dont forget to provide the exact path to the file)

“include_ once” and “require_ once” statements will do the same job as include and require execpt it will include the file only once. Means even if we have tried to include the file same file more than once in a page using any of these statements it will include only once.

Now we can look at the different between these statements

Difference between include and require

Both will do the same job when including a file. Difference is occurring when the file is missing. include statement will produce only a warning (E_WARNING) and continue the execution of script . But require statement will produce a fatal level error (E_COMPILE_ERROR) and stops the execution of program.

Array is a special variable which can store multiple value. ( Note : a normal variable can store only one value at a time ). Arrays are the oldest and most important data structure which can hold multiple values. Each value in an array is associated with a key. And we can retrieve each value from array using the keys.

Arrays can be mainly two types

  • One-dimensional arrays
  • Multidimensional arrays

Array in PHP can be categorized as

  • Numeric arrays
  • Associative array
  • Multidimensional array

In PHP array can be declared using the construct array()

Numeric arrays are with a numeric index. Here we don’t need specify the keys. Index will start from 0. Means first data can be accessed using the index 0 , second data can be accessed using the index 1 etc
See the Example for a numeric array declaration
$names=array(“Syam”,”Binu”,”Bob”,”cathy”);
Its same as the following declarations
$names[0]=”Syam”;
$names[1]=”Binu”;
$names[2]=”Bob”;
$names[3]=”cathy”;

In Associative arrays each key is associated with a value.Here we need to specify each key and its associated value. And these values can be fetched using the declared keys.
See the example of an Associative array declaration
$age = array(“Syam”=>30, “Binu”=>25, “Bob”=>50, “cathy”=>30);
Here Syam, Binu etc are the keys and values are associated with those keys using => .
In this example we are trying to store the ages of some persons using arrays. So if we want to know the age of one particular person , we only need to know his name ( key ).
The above array can be declared same as
$age["Syam"]=30;
$age["Binu"]=25;
$age["Bob"]=50;
$age["cathy"]=30;

In Multidimensional arrays each element in array can be another array. Its useful for building matrices, tree and table like data storage structures.In some programming scenarios we can avoid the usage of several different one-dimensional arrays using a single Multidimensional arrays for our programming optimization , speed and other things.

For example , consider a case we need to store names , age and location. We can use a multidimensional array. See below
$data=array (“Syam”=>array(“age”=>30,”location”=>”India”),
“Bob”=>array(“age”=>50,”location”=>”USA”) );

Here is a sample program of array in PHP ( using the above array )

<?php
$data=array ("Syam"=>array("age"=>30,"location"=>"India"),"Bob"=>array("age"=>50,"location"=>"USA")  );
echo $data["Bob"]["location"];
?>

Its output will be ;
USA

For the formatting purpose we need to add line breaks in our output or strings to store in MySQL tables and all. Here I am describing about various methods to add line break using PHP.

In HTML we can add a line break using <br> or <br /> tag.
For this purpose in PHP we need to simply echo that tag like following code

<?php
echo "syam is here<br>for teaching you PHP";
?>

Its output will be
syam is here
for teaching you PHP
( We can see that a line break is added after “syam is here”)

But in several other cases line break is applying using “\n” or “\r\n”, like data storage in csv , txt files or mysql tables or data in a text area etc. In that cases we need to use function nl2br()
to convert the “\n” into “<br>” tags to display line break.

See the sample PHP program for display line break using nl2br()

<?php
echo nl2br("syam is here\nfor teaching you PHP");
?>

Its output also will be same as our previous PHP program
syam is here
for teaching you PHP
( we can see that “\n” is decoded to <br> tag by nl2br() )

There is PHP_EOL constant available instead of using “\n: or “\r\n”.

See the following sample PHP program with constant PHP_EOL

<?php
echo nl2br("syam is here".PHP_EOL."for teaching you PHP");
?>

Its output also will be same as our previous PHP programs
syam is here
for teaching you PHP
( here we can see that constant PHP_EOL is converted into line break using nl2br )

Most of the time we can use “<br>” tag itself for displaying the line breaks in our HTML contents or displaying areas. The other things will come under some advanced cases that we can understand in our later postings.

And Bit Operator : &
usage ( $x & $y ), Bits that are set in both $x and $y are set.

Or bit operator ( inclusive or ) : |
usage ( $x | $y ), Bits that are set in either $x or $y are set.

Xor bit operator ( exclusive or ) : ^
usage ( $x ^ $y ), Bits that are set in $x or $y but not both are set.

Not bit operator : ~
usage ( ~$x ), Bits that are set in $x are not set, and viceversa.

Shift Left bit operator : <<
usage ( $x << $y ), Shift the bits of $x $y steps to the left (each step means “multiply by two”)

Shift right bit operator : >>
usage ( $x >> $y ), Shift the bits of $x $y steps to the right (each step means “divide by two”)

Logical Operators are using for comparing between Boolean values and will return a Boolean result. Boolean values are TRUE and FALSE.

And Operator : and
Usage : ( $x and $y ), It will return TRUE if both $x and $y are TRUE, otherwise it will return FALSE.

Or Operator : or
Usage : ( $x or $y ). It will return TRUE if $x or $y is TRUE, otherwise it will return FALSE

Xor Operator : xor
Usage : ( $x xor $y ) , It will return TRUE if either $x or $y is TRUE, but both should not be TRUE. It will return FALSE if both are TRUE or FALSE.

Not Operator : !
usage ( !$x) , It will return TRUE if $x is not TRUE

And Operator : &&
Usage : ( $x && $y ), It will return TRUE if both $x and $y are TRUE, otherwise it will return FALSE.

Or Operator : ||
Usage : ( $x || $y ). It will return TRUE if $x or $y is TRUE, otherwise it will return FALSE

Here we can see that two operators are for And Operator ( and , && )
and
two operators are for Or Operator ( or, || ).
Both will do the same job.

Comparison Operators are used for compare two values.There are several types of comparison operators in PHP programming. See the details below.

Equal Operator : ==
Usage : ($x==$y) , It will return TRUE if value of $x is equal to $y, otherwise it will return FALSE.

Identical Operator : ===
It will check the value and type. Both should be same.
usage : ($x===$y), It will return TRUE if value and type of $x is same as value and type of $y, otherwise it will return FALSE.

Not Equal Operator : !=
Usage : ( $x != $y ), It will return TRUE if value of $x is not equal to value of $y, otherwise it will return FALSE.

Not Equal Operator : <>
Usage : ( $x <> $y ), It will return TRUE if value of $x is not equal to calue of $y, otherwise it will return FALSE.

Not Identical Operator : !==
Usage : ($x !== $y ), It will return TRUE if value of $x not equal to value of $y or type of $x is not same as type of $y.

Less than Operator : <
Usage ( $x < $y ), It will return TRUE if value of $x is less than the value of $y, Otherwise it will return FALSE.

Greater than Operator: >
Usage ( $x > $y ), It will return TRUE if value of $x is greater than the value of $y, Otherwise it will return FALSE.

Less than or Equal to Operator : <=
Usage ( $x <= $y ), It will return TRUE if value of $x is less than or Equal to value of $y, Otherwise it will return FALSE.

Greater than or Equal to Operator : >=
Usage ( $x >= $y ), It will return TRUE if value of $x is greater than or equal to value of $y, Otherwise it will return FALSE

These are the main comparison Operators in PHP. You should be very thorough about all these, because its usage is very much needed in our PHP script programming.

Here we can learn about the operators used for assigning values in PHP.

Operator “=”
Usage Sample program

<?php
$x = 10;
$y = $x;
?>

Operator “+=”
x +=y is same as x = x + y
Usage Sample program

<?php
$x = 10;
$y = 20;
$x += $y;
echo $x ;
?>

Output of this PHP program will be :
30

Operator “-=”
x -=y is same as x = x – y
Usage Sample program

<?php
$x = 30;
$y = 20;
$x -= $y;
echo $x ;
?>

Output of this PHP program will be :
10

Operator “*=”
x *=y is same as x = x * y
Usage Sample program

<?php
$x = 5;
$y = 6;
$x *= $y;
echo $x ;
?>

Output of this PHP program will be :
30

Operator “/=”
x /=y is same as x = x / y
Usage Sample program

<?php
$x = 10;
$y = 5;
$x /= $y;
echo $x ;
?>

Output of this PHP program will be :
2

Operator “.=”
x .=y is same as x = x . y ( Concatenation operation )
Usage Sample program

<?php
$x = "Syam";
$y = "Here";
$x .= $y;
echo $x ;
?>

Output of this PHP program will be :
Syam Here

Operator “%=”
x %=y is same as x = x % y ( Division Remainder )
Usage Sample program

<?php
$x = 10;
$y = 3;
$x %= $y;
echo $x ;
?>

Output of this PHP program will be :
1

There are several operators in PHP available for arithmetic calculations and all.

Addition Operator : +
Use : For the addition of two numbers.
Example

<?php
$a = 30;
$b = 10;
$c = $a + $b;
echo $c;
?>

Output will be ( 30 + 10 ):
40

Subtraction Operator : -
Use : Subtract one value from another value
Example

<?php
$a = 30;
$b = 10;
$c = $a - $b;
echo $c;
?>

Output will be ( 30 – 10) :
20

Multiplication Operator : *
Use: Mutiply one value with another
Example

<?php
$a = 30;
$b = 10;
$c = $a * $b;
echo $c;
?>

Output will be ( 30 * 10) :
300

Division Operator : /
Use : for the division of one value by another
Example

<?php
$a = 30;
$b = 10;
$c = $a / $b;
echo $c;
?>

Output will be ( 30 / 10) :
3

Modulus Operator : %
It will return the division remainder
Example 1:

<?php
$a = 30;
$b = 10;
$c = $a % $b;
echo $c;
?>

Output will be remainder of 30 / 10 :
0

Example 2:

<?php
$a = 5;
$b = 2;
$c = $a % $b;
echo $c;
?>

Output will be remainder of 5 / 2) :
1

Example 3:

<?php
$a = 10;
$b = 4;
$c = $a % $b;
echo $c;
?>

Output will be the remainder of 10 / 4 :
2

Increment Operator : ++
Use: Its a short code for using against + 1
Example

<?php
$a = 30;
$a++;
echo $a;
?>

Output will be ( 30 + 1 ):
31

Decrement Operator : –
Use: Its a short code for using against – 1
Example

<?php
$a = 30;
$a--;
echo $a;
?>

Output will be ( 30 – 1 ):
29

Negation Operator : will return the opposite of number
Example

<?php
$a = 30;
$b = -$a;
echo $b;
?>

Output will be :
-30

echo is using for output one or more strings in PHP.

echo is basically a language construct and not a function. So we can use without parentheses with it.

We can run shell commands also using shell_exec in PHP echo syntax.But its only used in advanced PHP programming and we will discuss more about this in our later posts.

We can use the HTML formatting tags with PHP echo statement

See the following sample program with echo in PHP programs

<?php
echo "Good Morning";
?>

Its out put will be :
Good morning

<?php
echo "<b>Good Morning</b>";
?>

Its out put will be :
Good morning

<?php
$variable1="PHP";
$variable2="MySQL";
echo $variable1."&".$variable2;
?>

Its out put will be :
PHP&MySQL

<?php
$variable1="PHP";
$variable2="MySQL";
echo $variable1."<br />".$variable2;
?>

Its out put will be :
PHP
MySQL