Unit - 4
Unit - 4
PHP BASICS
What is PHP
PHP is an open-source, interpreted, and object-oriented scripting language
that can be executed at the server-side. PHP is well suited for web
development. Therefore, it is used to develop web applications (an application
that executes on the server and generates the dynamic page.).
PHP Features
PHP is very popular language because of its simplicity and open source. There
are some important features of PHP given below:
Performance:
PHP script is executed much faster than those scripts which are written in
other languages such as JSP and ASP. PHP uses its own memory, so the server
workload and loading time is automatically reduced, which results in faster
processing speed and better performance.
Open Source:
PHP source code and software are freely available on the web. You can
develop all the versions of PHP according to your requirement without paying
any cost. All its components are free to download and use.
Embedded:
PHP code can be easily embedded within HTML tags and script.
Platform Independent:
PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP
application developed in one OS can be easily executed in other OS also.
Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
Error Reporting -
PHP allows us to use a variable without declaring its datatype. It will be taken
automatically at the time of execution based on the type of data it contains on
its value.
PHP is compatible with almost all local servers used today like Apache,
Netscape, Microsoft IIS, etc.
Security:
Control:
Prerequisite
Before learning PHP, you must have the basic knowledge of HTML,
CSS, and JavaScript. So, learn these technologies for better implementation
of PHP.
CSS - CSS helps to make the webpage content more effective and attractive.
All PHP code goes between the php tag. It starts with <?php and ends with ?
>. The syntax of PHP tag is given below:
1. <?php
2. //your code here
3. ?>
Let's see a simple PHP example where we are writing some text using PHP
echo command.
1. <!DOCTYPE>
2. <html>
3. <body>
4. <?php
5. echo "<h2>Hello First PHP</h2>";
6. ?>
7. </body>
8. </html>
Output:
PHP echo statement can be used to print the string, multi-line strings,
escaping characters, variable, array, etc. Some important points that you must
know about the echo statement are:
1. <?php
2. echo "Hello by PHP echo";
3. ?>
Output:
1. <?php
2. $msg="Hello JavaTpoint PHP";
3. echo "Message is: $msg";
4. ?>
Output:
PHP Print
Like PHP echo, PHP print is a language construct, so you don't need to use
parenthesis with the argument list. Print statement can be used with or
without parentheses: print and print(). Unlike echo, it always returns 1.
PHP print statement can be used to print the string, multi-line strings,
escaping characters, variable, array, etc. Some important points that you must
know about the echo statement are:
1. <?php
2. print "Hello by PHP print ";
3. print ("Hello by PHP print()");
4. ?>
Output:
PHP Variables
In PHP, a variable is declared using a $ sign followed by the variable name. Here,
some important points to know about variables:
1. $variablename=value;
o A variable must start with a dollar ($) sign, followed by the variable
name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9,
_).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
o One thing to be kept in mind that the variable name cannot start with a
number or special symbols.
o PHP variables are case-sensitive, so $name and $NAME both are
treated as different variable.
1. <?php
2. $str="hello string";
3. $x=200;
4. $y=44.6;
5. echo "string is: $str <br/>";
6. echo "integer is: $x <br/>";
7. echo "float is: $y <br/>";
8. ?>
Output:
1. <?php
2. $x=5;
3. $y=6;
4. $z=$x+$y;
5. echo $z;
6. ?>
Output:
11
1. Local variable
2. Global variable
3. Static variable
Local variable
The variables that are declared within a function are called local variables for
that function. These local variables have their scope only in that particular
function in which they are declared. This means that these variables cannot be
accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely
different from the variable declared inside the function. Let's understand the
local variables with the help of an example:
File: local_variable1.php
1. <?php
2. function local_var()
3. {
4. $num = 45; //local variable
5. echo "Local variable declared inside the function is: ". $num;
6. }
7. local_var();
8. ?>
Output:
File: local_variable2.php
1. <?php
2. function mytest()
3. {
4. $lang = "PHP";
5. echo "Web development language: " .$lang;
6. }
7. mytest();
8. //using $lang (local variable) outside the function will generate an error
9. echo $lang;
10. ?>
Output:
Global variable
The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program. To access the
global variable within a function, use the GLOBAL keyword before the variable.
However, these variables can be directly accessed or used outside the function
without any keyword. Therefore there is no need to use any keyword to access
a global variable outside the function.
Example:
1. <?php
2. $name = "Sanaya Sharma"; //Global Variable
3. function global_var()
4. {
5. global $name;
6. echo "Variable inside the function: ". $name;
7. echo "</br>";
8. }
9. global_var();
10. echo "Variable outside the function: ". $name;
11. ?>
Output:
Note: Without using the global keyword, if you try to access a global variable
inside the function, it will generate an error that the variable is undefined.
Example:
1. <?php
2. $name = "Sanaya Sharma"; //global variable
3. function global_var()
4. {
5. echo "Variable inside the function: ". $name;
6. echo "</br>";
7. }
8. global_var();
9. ?>
Output:
If two variables, local and global, have the same name, then the local variable
has higher priority than the global variable inside the function.
Example:
1. <?php
2. $x = 5;
3. function mytest()
4. {
5. $x = 7;
6. echo "value of x: " .$x;
7. }
8. mytest();
9. ?>
Output:
Value of x: 7
Note: local variable has higher priority than the global variable.
Static variable
It is a feature of PHP to delete the variable, once it completes its execution
and memory is freed. Sometimes we need to store a variable even after
completion of function execution. Therefore, another important feature of
variable scoping is static variable. We use the static keyword before the
variable to define a variable, and this variable is called as static variable.
Static variables exist only in a local function, but it does not free its memory
after the program execution leaves the scope. Understand it with the help of
an example:
Example:
File: static_variable.php
1. <?php
2. function static_var()
3. {
4. static $num1 = 3; //static variable
5. $num2 = 6; //Non-static variable
6. //increment in non-static variable
7. $num1++;
8. //increment in static variable
9. $num2++;
10. echo "Static: " .$num1 ."</br>";
11. echo "Non-static: " .$num2 ."</br>";
12. }
13.
14. //first function call
15. static_var();
16.
17. //second function call
18. static_var();
19. ?>
Output:
Static: 4
Non-static: 7
Static: 5
Non-static: 7
You have to notice that $num1 regularly increments after each function call,
whereas $num2 does not. This is why because $num1 is not a static variable,
so it freed its memory after the execution of each function call.
The $$var (double dollar) is a reference variable that stores the value of the
$variable inside it.
Example 1
1. <?php
2. $x = "abc";
3. $$x = 200;
4. echo $x."<br/>";
5. echo $$x."<br/>";
6. echo $abc;
7. ?>
Output:
In the above example, we have assigned a value to the variable x as abc. Value
of reference variable $$x is assigned as 200.
PHP Constants
PHP constants are name or identifier that can't be changed during the
execution of the script except for magic constants, which are not really
constants. PHP constants can be defined by 2 ways:
Constants are similar to the variable except once they defined, they can never
be undefined or changed. They remain constant across the entire program.
PHP constants follow the same PHP variable rules. For example, it can be
started with a letter or underscore only.
File: constant1.php
1. <?php
2. define("MESSAGE","Hello JavaTpoint PHP");
3. echo MESSAGE;
4. ?>
Output:
File: constant2.php
1. <?php
2. define("MESSAGE","Hello JavaTpoint PHP",true);//not case sensitive
3. echo MESSAGE, "</br>";
4. echo message;
5. ?>
Output:
File: constant3.php
1. <?php
2. define("MESSAGE","Hello JavaTpoint PHP",false);//case sensitive
3. echo MESSAGE;
4. echo message;
5. ?>
Output:
File: constant4.php
1. <?php
2. const MESSAGE="Hello const by JavaTpoint PHP";
3. echo MESSAGE;
4. ?>
Output:
Constant vs Variables
Constant Variables
Once the constant is defined, it can never be A variable can be undefined as well as
redefined. redefined easily.
A constant can only be defined using define() A variable can be defined by simple
function. It cannot be defined by any simple assignment (=) operator.
assignment.
There is no need to use the dollar ($) sign To declare a variable, always use the dollar
before constant during the assignment. ($) sign before the variable.
Constants do not follow any variable scoping Variables can be declared anywhere in the
rules, and they can be defined and accessed program, but they follow variable scoping
anywhere. rules.
Constants are the variables whose values can't The value of the variable can be changed.
be changed throughout the program.
It holds only single value. There are 4 scalar data types in PHP.
1. boolean
2. integer
3. float
4. string
It can hold multiple values. There are 2 compound data types in PHP.
1. array
2. object
1. resource
2. NULL
PHP Boolean
Booleans are the simplest data type works like switch. It holds only two
values: TRUE (1) or FALSE (0). It is often used with conditional statements. If
the condition is correct, it returns TRUE otherwise FALSE.
Example:
1. <?php
2. if (TRUE)
3. echo "This condition is TRUE.";
4. if (FALSE)
5. echo "This condition is FALSE.";
6. ?>
Output:
PHP Integer
Integer means numeric data with a negative or positive sign. It holds only
whole numbers, i.e., numbers without fractional part or decimal points.
Example:
1. <?php
2. $dec1 = 34;
3. $oct1 = 0243;
4. $hexa1 = 0x45;
5. echo "Decimal number: " .$dec1. "</br>";
6. echo "Octal number: " .$oct1. "</br>";
7. echo "HexaDecimal number: " .$hexa1. "</br>";
8. ?>
Output:
Decimal number: 34
Octal number: 163
HexaDecimal number: 69
PHP Float
Example:
1. <?php
2. $n1 = 19.34;
3. $n2 = 54.472;
4. $sum = $n1 + $n2;
5. echo "Addition of floating numbers: " .$sum;
6. ?>
Output:
PHP String
Example:
1. <?php
2. $company = "Javatpoint";
3. //both single and double quote statements will treat different
4. echo "Hello $company";
5. echo "</br>";
6. echo 'Hello $company';
7. ?>
Output:
Hello Javatpoint
Hello $company
PHP Array
An array is a compound data type. It can store multiple values of same data
type in a single variable.
Example:
1. <?php
2. $bikes = array ("Royal Enfield", "Yamaha", "KTM");
3. var_dump($bikes); //the var_dump() function returns the datatype and valu
es
4. echo "</br>";
5. echo "Array Element1: $bikes[0] </br>";
6. echo "Array Element2: $bikes[1] </br>";
7. echo "Array Element3: $bikes[2] </br>";
8. ?>
Output:
You will learn more about array in later chapters of this tutorial.
PHP object
Objects are the instances of user-defined classes that can store both values
and functions. They must be explicitly declared.
Example:
1. <?php
2. class bike {
3. function model() {
4. $model_name = "Royal Enfield";
5. echo "Bike Model: " .$model_name;
6. }
7. }
8. $obj = new bike();
9. $obj -> model();
10. ?>
Output:
PHP Resource
Resources are not the exact data type in PHP. Basically, these are used to store
some function calls or references to external PHP resources. For example - a
database call. It is an external resource.
PHP Null
Null is a special data type that has only one value: NULL. There is a
convention of writing it in capital letters as it is case sensitive.
The special type of data type NULL defined a variable with no value.
Example:
1. <?php
2. $nl = NULL;
3. echo $nl; //it will not give any output
4. ?>
Output:
PHP Operators
PHP Operator is a symbol i.e used to perform operations on operands. In
simple words, operators are used to perform operations on variables or
values. For example:
In the above example, + is the binary + operator, 10 and 20 are operands and
$num is variable.
o Arithmetic Operators
o Assignment Operators
o Bitwise Operators
o Comparison Operators
o Incrementing/Decrementing Operators
o Logical Operators
o String Operators
o Array Operators
o Type Operators
o Execution Operators
o Error Control Operators
Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic
operations such as addition, subtraction, etc. with numeric values.
Assignment Operators
The assignment operators are used to assign value to different variables. The
basic assignment operator is "=".
& And $a & $b Bits that are 1 in both $a and $b are set to 1,
otherwise 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are set
to 1
<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number
of places
Comparison Operators
Comparison operators allow comparing two values, such as number or string.
Below the list of comparison operators are given:
=== Identical $a === Return TRUE if $a is equal to $b, and they are
$b of same data type
!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and they
are not of same data type
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease
the value of a variable.
Logical Operators
The logical operators are used to perform bit-level operations on operands.
These operators allow the evaluation and manipulation of specific bits within
the integer.
xor Xor $a xor $b Return TRUE if either $ or $b is true but not both
String Operators
The string operators are used to perform the operation on strings. There are
two string operators in PHP, which are given below:
Array Operators
The array operators are used in case of array. Basically, these operators are
used to compare the values of arrays.
=== Identity $a === Return TRUE if $a and $b have same key/value pair of
$b same type in same order
Type Operators
The type operator instanceof is used to determine whether an object, its
parent and its derived class are the same type or not. Basically, this operator
determines which certain class the object belongs to. It is used in object-
oriented programming.
1. <?php
2. //class declaration
3. class Developer
4. {}
5. class Programmer
6. {}
7. //creating an object of type Developer
8. $charu = new Developer();
9.
10. //testing the type of object
11. if( $charu instanceof Developer)
12. {
13. echo "Charu is a developer.";
14. }
15. else
16. {
17. echo "Charu is a programmer.";
18. }
19. echo "</br>";
20. var_dump($charu instanceof Developer); //It will return true.
21. var_dump($charu instanceof Programmer); //It will return false.
22. ?>
Output:
Charu is a developer.
bool(true) bool(false)
Execution Operators
PHP has an execution operator backticks (``). PHP executes the content of
backticks as a shell command. Execution operator and shell_exec() give the
same result.
`` backticks echo `dir`; Execute the shell command and return the result.
Here, it will show the directories available in current
folder.
[ array() left
** arithmetic right
| bitwise OR left
|| logical OR left
?: ternary left
or logical left
PHP supports single line and multi line comments. These comments are
similar to C/C++ and Perl style (Unix shell style) comments.
1. <?php
2. // this is C++ style single line comment
3. # this is Unix Shell style single line comment
4. echo "Welcome to PHP single line comments";
5. ?>
Output:
1. <?php
2. /*
3. Anything placed
4. within comment
5. will not be displayed
6. on the browser;
7. */
8. echo "Welcome to PHP multi line comment";
9. ?>
Output:
PHP Functions
PHP function is a piece of code that can be reused many times. It can take
input as argument list and return value. There are thousands of built-in
functions in PHP.
Less Code: It saves a lot of code because you don't need to write the logic
many times. By the use of function, you can write the logic only once and
reuse it.
Syntax
1. function functionname(){
2. //code to be executed
3. }
Note: Function name must be start with letter and underscore only like other
labels in PHP. It can't be start with numbers or special symbols.
1. <?php
2. function sayHello(){
3. echo "Hello PHP Function";
4. }
5. sayHello();//calling function
6. ?>
Output:
File: functionarg.php
1. <?php
2. function sayHello($name){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Sonoo");
6. sayHello("Vimal");
7. sayHello("John");
8. ?>
Output:
Hello Sonoo
Hello Vimal
Hello John
File: functionarg2.php
1. <?php
2. function sayHello($name,$age){
3. echo "Hello $name, you are $age years old<br/>";
4. }
5. sayHello("Sonoo",27);
6. sayHello("Vimal",29);
7. sayHello("John",23);
8. ?>
Output:
File: functionref.php
1. <?php
2. function adder(&$str2)
3. {
4. $str2 .= 'Call By Reference';
5. }
6. $str = 'Hello ';
7. adder($str);
8. echo $str;
9. ?>
Output:
File: functiondefaultarg.php
1. <?php
2. function sayHello($name="Sonoo"){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Rajesh");
6. sayHello();//passing no value
7. sayHello("John");
8. ?>
Output:
Hello Rajesh
Hello Sonoo
Hello John
File: functiondefaultarg.php
1. <?php
2. function cube($n){
3. return $n*$n*$n;
4. }
5. echo "Cube of 3 is: ".cube(3);
6. ?>
Output:
Cube of 3 is: 27
PHP Arrays
PHP array is an ordered map (contains value on the basis of key). It is used to
hold multiple values of similar type in a single variable.
Easy to traverse: By the help of single loop, we can traverse all the elements
of an array.
1. Indexed Array
2. Associative Array
3. Multidimensional Array
PHP Indexed Array
PHP index is represented by number which starts from 0. We can store
number, string and object in the PHP array. All PHP array elements are
assigned to an index number by default.
1st way:
1. $season=array("summer","winter","spring","autumn");
2nd way:
1. $season[0]="summer";
2. $season[1]="winter";
3. $season[2]="spring";
4. $season[3]="autumn";
Example
File: array1.php
1. <?php
2. $season=array("summer","winter","spring","autumn");
3. echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
4. ?>
Output:
1. <?php
2. $season[0]="summer";
3. $season[1]="winter";
4. $season[2]="spring";
5. $season[3]="autumn";
6. echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
7. ?>
Output:
1st way:
1. $salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
2nd way:
1. $salary["Sonoo"]="350000";
2. $salary["John"]="450000";
3. $salary["Kartik"]="200000";
Example
File: arrayassociative1.php
1. <?php
2. $salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
3. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
4. echo "John salary: ".$salary["John"]."<br/>";
5. echo "Kartik salary: ".$salary["Kartik"]."<br/>";
6. ?>
Output:
Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
File: arrayassociative2.php
1. <?php
2. $salary["Sonoo"]="350000";
3. $salary["John"]="450000";
4. $salary["Kartik"]="200000";
5. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
6. echo "John salary: ".$salary["John"]."<br/>";
7. echo "Kartik salary: ".$salary["Kartik"]."<br/>";
8. ?>
Output:
Definition
1. $emp = array
2. (
3. array(1,"sonoo",400000),
4. array(2,"john",500000),
5. array(3,"rahul",300000)
6. );
1 sonoo 400000
2 john 500000
3 rahul 300000
File: multiarray.php
1. <?php
2. $emp = array
3. (
4. array(1,"sonoo",400000),
5. array(2,"john",500000),
6. array(3,"rahul",300000)
7. );
8.
9. for ($row = 0; $row < 3; $row++) {
10. for ($col = 0; $col < 3; $col++) {
11. echo $emp[$row][$col]." ";
12. }
13. echo "<br/>";
14. }
15. ?>
Output:
1 sonoo 400000
2 john 500000
3 rahul 300000
PHP String
PHP string is a sequence of characters i.e., used to store and manipulate text.
PHP supports only 256-character set and so that it does not offer native
Unicode support. There are 4 ways to specify a string literal in PHP.
1. single quoted
2. double quoted
3. heredoc syntax
4. newdoc syntax (since PHP 5.3)
Single Quoted
We can create a string in PHP by enclosing the text in a single-quote. It is the
easiest way to specify string in PHP.
For specifying a literal single quote, escape it with a backslash (\) and to
specify a literal backslash (\) use double backslash (\\). All the other instances
with backslash such as \r or \n, will be output same as they specified instead
of having any special meaning.
For Example
Following some examples are given to understand the single quoted PHP
String in a better way:
Example 1
1. <?php
2. $str='Hello text within single quote';
3. echo $str;
4. ?>
Output:
We can store multiple line text, special characters, and escape sequences in a
single-quoted PHP string.
Example 2
1. <?php
2. $str1='Hello text
3. multiple line
4. text within single quoted string';
5. $str2='Using double "quote" directly inside single quoted string';
6. $str3='Using escape sequences \n in single quoted string';
7. echo "$str1 <br/> $str2 <br/> $str3";
8. ?>
Output:
Example 3
1. <?php
2. $num1=10;
3. $str1='trying variable $num1';
4. $str2='trying backslash n and backslash t inside single quoted string \n \t';
5. $str3='Using single quote \'my quote\' and \\backslash';
6. echo "$str1 <br/> $str2 <br/> $str3";
7. ?>
Output:
Note: In single quoted PHP strings, most escape sequences and variables will
not be interpreted. But, we can use single quote through \' and backslash
through \\ inside single quoted PHP strings.
Double Quoted
In PHP, we can specify string through enclosing text within double quote also.
But escape sequences and variables will be interpreted using double quote
PHP strings.
Example 1
1. <?php
2. $str="Hello text within double quote";
3. echo $str;
4. ?>
Output:
Now, you can't use double quote directly inside double quoted string.
Example 2
1. <?php
2. $str1="Using double "quote" directly inside double quoted string";
3. echo $str1;
4. ?>
Output:
Example 3
1. <?php
2. $str1="Hello text
3. multiple line
4. text within double quoted string";
5. $str2="Using double \"quote\" with backslash inside double quoted string";
6. $str3="Using escape sequences \n in double quoted string";
7. echo "$str1 <br/> $str2 <br/> $str3";
8. ?>
Output:
Example 4
1. <?php
2. $num1=10;
3. echo "Number is: $num1";
4. ?>
Output:
Number is: 10
Heredoc
Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an
identifier is provided after this heredoc <<< operator, and immediately a new
line is started to write any text. To close the quotation, the string follows itself
and then again that same identifier is provided. That closing identifier must
begin from the new line without any whitespace or tab.
Naming Rules
The identifier should follow the naming rule that it must contain only
alphanumeric characters and underscores, and must start with an underscore
or a non-digit character.
For Example
Valid Example
1. <?php
2. $str = <<<Demo
3. It is a valid example
4. Demo; //Valid code as whitespace or tab is not valid before closing identifier
5. echo $str;
6. ?>
Output:
It is a valid example
Invalid Example
We cannot use any whitespace or tab before and after the identifier and
semicolon, which means identifier must not be indented. The identifier must
begin from the new line.
1. <?php
2. $str = <<<Demo
3. It is Invalid example
4. Demo; //Invalid code as whitespace or tab is not valid before closing ide
ntifier
5. echo $str;
6. ?>
Output:
Newdoc
Newdoc is similar to the heredoc, but in newdoc parsing is not done. It is also
identified with three less than symbols <<< followed by an identifier. But here
identifier is enclosed in single-quote, e.g. <<<'EXP'. Newdoc follows the
same rule as heredocs.
1. <?php
2. $str = <<<'DEMO'
3. Welcome to javaTpoint.
4. Learn with newdoc example.
5. DEMO;
6. echo $str;
7. echo '</br>';
8.
9. echo <<< 'Demo' // Here we are not storing string content in variable str.
10. Welcome to javaTpoint.
11. Learn with newdoc example.
12. Demo;
13. ?>
Output:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
PHP Form Handling
The PHP superglobals $_GET and $_POST are used to collect form-
data.
<html>
<body>
</body>
</html>
Run Example »
When the user fills out the form above and clicks the submit button, the
form data is sent for processing to a PHP file named "welcome.php". The
form data is sent with the HTTP POST method.
To display the submitted data you could simply echo all the variables. The
"welcome.php" looks like this:
<html>
<body>
</body>
</html>
The same result could also be achieved using the HTTP GET method:
Example
<html>
<body>
</body>
</html>
Run Example »
<html>
<body>
</body>
</html>
The code above is quite simple. However, the most important thing is
missing. You need to validate form data to protect your script from
malicious code.
This page does not contain any form validation, it just shows how you can send
and retrieve form data.
However, the next pages will show how to process PHP forms with security in
mind! Proper validation of form data is important to protect your form from
hackers and spammers!
Both GET and POST are treated as $_GET and $_POST. These are
superglobals, which means that they are always accessible, regardless of
scope - and you can access them from any function, class or file without
having to do anything special.
$_GET is an array of variables passed to the current script via the URL
parameters.
$_POST is an array of variables passed to the current script via the HTTP
POST method.
However, because the variables are not displayed in the URL, it is not
possible to bookmark the page.
PHP Session
PHP session is used to store and pass information from one page to another
temporarily (until user close the website).
PHP session creates unique user id for each browser to recognize the user and
avoid conflict between multiple browsers.
PHP session_start() function
PHP session_start() function is used to start the session. It starts a new or
resumes existing session. It returns existing session if session is created
already. If session is not available, it creates and returns new session.
Syntax
Example
1. session_start();
PHP $_SESSION
PHP $_SESSION is an associative array that contains all session variables. It is
used to set and get session variable values.
1. $_SESSION["user"] = "Sachin";
1. echo $_SESSION["user"];
1. <?php
2. session_start();
3. ?>
4. <html>
5. <body>
6. <?php
7. $_SESSION["user"] = "Sachin";
8. echo "Session information are set successfully.<br/>";
9. ?>
10. <a href="session2.php">Visit next page</a>
11. </body>
12. </html>
File: session2.php