PHP GET and POST

Difference between GET and POST PHP

In PHP there are two ways for sending information to server.

These are GET and POST $_GET and $_POST both are super global variable.

The variable which is always accessible is called as super global variable.

Difference

$_GET :

  1. $_GET is super global variable.
  2. Data send in GET is visible in URL.
  3. Not preferable for send sensitive data.
  4. Limitation of send data (About 2000 character)
  5. In GET you send information to server in two way :
    1. Through URL
    2. Through from

Through URL Example

<!DOCTYPE html>
<html>
<body>
<a href=“http://www.example.com/action.php?name=john&age=24">Send</a>
</body>
</html>

Through From Example

<!DOCTYPE html>
<html>
<body>
<form action="action_page.php" method="get">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

In action_page.php we gather the information using $_GET super global variable.

$_POST

  1. $_GET is super global variable.
  2. Data send in POST is not visible in URL.
  3. Preferable for send sensitive data.
  4. No limitation for send data.
  5. In POST you send information to server in one way that is form.

Example :

<!DOCTYPE html>
<html>
<body>
<form action="action_page.php" method="post">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

PHP for Loop and for- each loop

For loop is an control Statement

Syntax

for (init counter; test counter; increment counter) {
code to be executed;
}

In for loop there are 3 section

  • init counter
  • test counter
  • increment counter

The init counter is Used for Intialize Because it is Executed only once.

The test counter Check the Condition.Here we Write Expression which can be Executed Multiple Times.

The increment counter is Used for Increasing or Decreasing the value of a variable or we can Write any Expression.It is also Executed Several Times.

Example For Loop :

<?php
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}
?>

Second Exmaple :

<?php
for ($x = 0; $x <= 100; $x+=10) {
    echo "The number is: $x <br>";
}
?>

PHP for each loop

for each loop is a special type of control statement which is used for array

it access all the elements in an array 

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}
?>

Second Example :

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
  echo "$x = $val<br>";
}
?>

PHP do while loops

The do…while loop will always execute the block of code once In PHP do..while loop is similar to while loop but one difference between them.The do…while loop will always execute the block of code once. If the condition is true then code of block executed repeatedly, until the condition becomes false.

But in while loops execute a block of code while the specified condition is true.

Syntax

do {
    code to be executed;
} while (condition is true);

Example :

<?php
$a=1;
do
{
echo $a;
$a++;
}
while($a<=10)
?>

PHP while loops

In PHP while loops is used to execute a block of a code while the specified condition is true

Some time we want the same block of code to run over and over again

Example: Print 1 to 20 number.

Example: Fetch multiple students data In that case we can use while loop.

Syntax

while (condition is true) {
    code to be executed;
}
Example : (Print 1 to 20)
<?php
$a=1;
while ($a<=20)
{
echo $a;
$a++;
}
?>
Example : (Print all even and odd number between 0 to 20)
<?php
    $i=0;
     while($i <= 20){
         if($i % 2 == 0){
             echo $i." - Even,  ";
         }else{
              echo $i." - Odd,  ";
         }
         $i++;
     }
 ?>

PHP Switch Statement

The switch case statement is an alternative to the if-elseif-else statement. It is the same thing as if-elseif-else statement.

How its Works

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each Case.
  • If there is a match, the associated block of code is executed other wise the default block is executed.

Syntax

switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

Example :

<?php
$favsport = "cricket";
switch ($favsport) {
case "hockey":
echo "Your favorite sport is Hocky!";
break;
case "cricket":
echo "Your favorite sport is Cricket!";
break;
case "football":
echo "Your favorite sport is Football!";
break;
case "badminton":
echo "Your favorite sport is Badminton!";
break;
default:
echo "Your favorite sport is neither Hockey, Tennis, nor Golf!";
}
?>

Second Example :

<?php
$favsport = "cricket";
switch ($favsport) {
case "hockey":
echo "Your favorite sport is Hocky!";
break;
case "tennis":
echo "Your favorite sport is Cricket!";
break;
case "football":
echo "Your favorite sport is Football!";
break;
case "badminton":
echo "Your favorite sport is Badminton!";
break;
default:
echo "Your favorite sport is neither Hockey, Cricket, Football nor Golf!";
}
?>

Third Example :

<?php
$d = date("D");
switch ($d){
case "Mon":
echo "Today is Monday";
break;
case "Tue":
echo "Today is Tuesday";
break;
case "Wed":
echo "Today is Wednesday";
break;
case "Thu":
echo "Today is Thursday";
break;
case "Fri":
echo "Today is Friday";
break;
case "Sat":
echo "Today is Saturday";
break;
case "Sun":
echo "Today is Sunday";
break;
default:
echo "Wonder which day is this ?";
}
?>

PHP if…else…elseif Statements

PHP – The if Statement

If statement is a control statement. The body of the if statement is executed when condition is true or non zero. In PHP we can pass either Boolean value or any other value

Syntax

if (condition) {
code to be executed if condition is true;
}
<?php
$x=10;
$y=10;
if($x==$y)
{
echo"successful";
}
?>

PHP – The if…else Statement

It is also a control statement. It is very useful in programming. If condition is not true the else part executed. Else has no condition.

Syntax

if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
<?php
$x=10;
$y=10;
if($x==$y)
{
echo"successful";
}
else
{
echo"failure";
}
?>

PHP – The if…else….elseif Statement

Syntax

if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
<?php
$a=10;
$b=10;
$c=10;
if($a > $b and $a > $c)
{
echo"a is largest.";
}
elseif($b > $a and $b > $c)
{
echo"b is largest";
}
elseif($c > $a and $c > $b)
{
echo" c is largest";
}
else
{
echo"a=b=c";
}
?>

PHP Operators

Operator in PHP is a symbol that is used to perform PHP operations.For example: + , – , * , / etc

There are many types of operators in PHP

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators

Arithmetic operators

OperatorDescriptionExampleResult
+Addition$x + $ySum of $x and $y
Subtraction$x – $yDifference of $x and $y.
*Multiplication$x * $yProduct of $x and $y.
/Division$x / $yQuotient of $x and $y
%Modulus$x % $yRemainder of $x divided by $y

Assignment operators

OperatorDescriptionExampleIs The Same As
=Assign$x = $y$x = $y
+=Add and assign$x += $y$x = $x + $y
-=Subtract and assign$x -= $y$x = $x – $y
*=Multiply and assign$x *= $y$x = $x * $y
/=Divide and assign quotient$x /= $y$x = $x / $y
%=Divide and assign modulus$x %= $y$x = $x % $y

Comparison operators

OperatorNameExampleResult
==Equal$x == $yTrue if $x is equal to $y
===Identical$x === $yTrue if $x is equal to $y, and they are of the same type
!=Not equal$x != $y
True if $x is not equal to $y
<>Not equal$x <> $yTrue if $x is not equal to $y
!==Not identical$x !== $yTrue if $x is not equal to $y, or they are not of the same type
<Less than$x < $yTrue if $x is less than $y
>Greater than$x > $yTrue if $x is greater than $y
>=Greater than or equal to$x >= $yTrue if $x is greater than or equal to $y
<=Less than or equal to$x <= $yTrue if $x is less than or equal to $y

Increment/Decrement operators

OperatorNameEffect
++$xPre-incrementIncrements $x by one, then returns $x
$x++Post-incrementReturns $x, then increments $x by one
–$xPre-decrementDecrements $x by one, then returns $x
$x–Post-decrementReturns $x, then decrements $x by one

Logical operators

OperatorNameExampleResult
andAnd$x and $yTrue if both $x and $y are true
orOr$x or $yTrue if either $x or $y is true
xorXor$x xor $yTrue if either $x or $y is true, but not both
&&And$x && $yTrue if both $x and $y are true
||Or$x || $yTrue if either $$x or $y is true
!Not!$xTrue if $x is not true

String operators

OperatorDescriptionExampleResult
.Concatenation$str1 . $str2Concatenation of $str1 and $str2
.=Concatenation assignment$str1 .= $str2Appends the $str2 to the $str1

Array Operator

OperatorNameExampleResult
+Union$x + $y
Union of $x and $y
==Equality$x == $yTrue if $x and $y have the same key/value pairs
===Identity$x === $yTrue if $x and $y have the same key/value pairs in the same order and of the same types
!=Inequality$x != $yTrue if $x is not equal to $y
<>Inequality$x <> $yTrue if $x is not equal to $y
!==Non-identity$x !== $yTrue if $x is not identical to $y

PHP String

A string is an having letters, numbers, special characters and arithmetic values or combination of all

Example: Hello World !

Example: Hello World ! 123

Above both example are string

There is two way to assign a string to a variable

<?php
$string=‘Hello World !’;
$string_two=“Hello World !”;
?>

You can use both single quotation or double quotation.But there is a small difference between single quotation and double quotation.

single quotation

  1. Single quoted strings are the easiest way to specify string.
  2. It is faster than double quotation.

double quotation

  1. Double quotes force PHP to specify the string.
  2. It is less faster than single quotation.

So now in our mind a big confusion come that where to use single quotation and where to use double quotation.

Best example to understanding

<?php
$x=‘Hello';
echo '$x World’;
?>
Output: $x World
<?php
$x=‘Hello';
echo "$x World";
?>
Output: Hello World

PHP Data type

We can assign different type of data to a variable. Data type means the various type of data that we assign to a variable

PHP support various type of data type and all are also different from each other

list of data type :

  1. String
  2. Integer
  3. Float (floating point numbers – also called double)
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resource

PHP String

A string can have letters, numbers, special characters and arithmetic values or combination of all

Example

Note : You can use both single quotation or double quotation
<!DOCTYPE html>
<html>
<body>
<?php 
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>"; 
echo $y;
?>
</body>
</html>

PHP Integer

All the number without decimal point is known as integer.

Example: 100, 2000 etc

<!DOCTYPE html>
<html>
<body>
<?php  
$x = 100;
var_dump($x);
?>  
</body>
</html>

PHP Float or Double

The Float or Double data type are number with decimal point.

Example: 29.6, 30.03 etc

<!DOCTYPE html>
<html>
<body>
<?php  
$x = 29.6;
var_dump($x);
?>  
</body> </html>

PHP Boolean

PHP Boolean having only two possible values either true or false.

PHP Array

PHP array is used to store multiple value in one single variable

<!DOCTYPE html>
<html>
<body>
<?php  
$name = array("Tanvi","Manas","Manshi");
var_dump($name);
?>  
</body>
</html>

PHP null

A variable of type null is a variable without any data

<!DOCTYPE html>
<html>
<body>
<?php
$x= NULL;
var_dump($x);
?>
</body>
</html>

PHP Superglobal variable

In PHP there are various Superglobals variable are available

  1. $GLOBALS
  2. $_SERVER
  3. $_REQUEST
  4. $_POST
  5. $_GET
  6. $_FILES
  7. $_ENV
  8. $_COOKIE
  9. $_SESSION

$_REQUEST

In PHP $_REQUEST are used to collect form data.

$_SERVER

$_SERVER is an super globalvariable containing information such as headers, paths, and script locations.

$_GET

In PHP $_GET are used to collect form data.

In GET you send information to server in two way :

  • Through URL
  • Through from

$_POST

In PHP $_POST are used to collecting form data.

In POST you send information to server in one way