Accessing Form Variables in PHP – GET & POST Guide

What Are Form Variables in PHP?

Accessing form variables are an important topic in PHP web applications.this PHP is helps to validate the data given by the user.this PHP application consists of checkboxes, radio buttons, text fields, etc. given by the user with interacting with application fields.

Why Do We Use Form Variables in PHP?

In PHP we can access form two superglobal variables.one is $_POST and another is $_GET.these two variables are used for taking inputs from user in the form and validate the data.

Difference Between GET and POST Methods

$_GET is used to It sends data through the URL..it displays on url of the browser.GET is less secure because data appears in the URL. because some of the content is visible on the browser.In Get we can send only limited information.we cannot send any large data.$_POST is used the get the content from the form.it is not visible on url like values, variable names.it is more secure than get.Server settings decide the limit. to the user.

PHP Form Example Using POST Method

<form method=”post” action =”hello.php“>

Method Attribute in PHP Forms

Method explanation:

InPHP method attribute is used to check it send data from $_post method or $_get method.

Action:action is the attribute in PHP .it is used to for where the data submits and from where data is sending.

Name:

Name field is important in PHP .it is used to submit data in form.

Name explanation

$user=$_POST[‘user’];

Target explanation

Target attribute is crucial in PHP.it is used to display the data after submitting the form.

<form method=”post” action =”hello.php”>

<Input type=”text” name=”user”>

<input type =”submit” value=”submit”>

</Form>

Example:

<form method=”get” action=”hello.php”>

Age:<input type=”text” name=”age”>

<input type=”text” value=”submit”>

</form>

hello.php

<?php

$age=$_GET[‘age’];

echo “age : “.$age;

?>

Output:

age : 25

Variable Handling Functions in PHP

Variable handling functions are predefined functions.we cannot install any function in php.there are function in php we can access it easily.

  • 1.isset()
  • 2.empty()
  • 3.unset()
  • 4.gettype()
  • 5.settype()
  • 6.is_int()
  • 7.is_float()
  • 8.is_string()
  • 9.is_bool()
  • 10.is_null()
  • 11.is_array()
  • 12.is_object()
  • 13.is_numeric()
  • 14.intval()
  • 15.floatval()
  • 16 strval()
  • 17.var_dump()
  • 18.print_r()

1.isset():

It is used to check wheather variable is assigned or not.

If it is assigned it prints true.otherwise it prints false.

Example:

<?php

$name=”hi”;

if(isset($name))

{

echo “true”;

}

?>

Output :

True

2.empty():

empty() function is used to check variable is empty or not.empty means null..,nothing value is assigned.

Example:

<?php

$name=””;

if(empty($name))

{

echo “true”;

}

?>

Output :

true

3.gettype()

It returns the datatype of a variable.

It return the data type of that variable.

Example:

<?php

$name=”hi”;

echo gettype($name);

?>

Output:

String

4.is_int()

It is used to check whether given variable is integer or not.

Example:

<?php

$a=10;

if(is_int($a))

{

echo “true”;

}

?>

Output:

True

5.is_float()

it is used to check whether given variable is float or not

Example:

<?php

$a=12.5;

if(is_float($a))

{

echo “yes”;

}

?>

Output :

Yes

6.is_string()

Is_string() is used to check whether given variable is string or not.

Example:

<?php

$a=”hello”;

if(is_string($a))

{

echo “yes”;

}

?>

Output:

Yes

7.var_dump()

var_dump() is one the important function in php.it is used to give variable value.

It also displays the data type of that variable.

Example:

<?php

$a=100;

var_dump($a);

?>

Output:

int(100)

8.unset()

Unset is used to delete the variable value.

Isset is used to check the value is assigned or not.

Example:

<?php

$a=100;

unset($a);

echo isset($a);

?>

Output:

No output

9.is_bool()

It is used to check whether given variable Boolean or not.

Example:

<?php

$b=true;

if(is_bool($b))

{

echo”true”;

}

?>

Output :

True

10.print_r()

It is used to print array element one after another easily.

Example:

<?php

$c=array(“cat”,”dog”);

print_r($c);

?>

Output:

[0]Cat

[1]Dog

11.settype():

settype() function is used to change datatype of a variable.

<?php

$a=”10″;

settype($a ,”integer”);

echo gettype($a);

?>

Output:

integer

12.is_null():

is_null() function is used to declare some variable value as null.it checks whether variable is null or not

Example:

<?php

$m=NULL;

if(is_null($m))

{

echo “yes”;

}

else

{

echo “no”;

}

?>

Output:

yes.

13.is_array()

is_array() function is used to check whether given variable is array or not.if it is array it prints one statement.otherwise nothing to print.

<?php

$a= array(4,5,6);

if(is_array($a))

echo “Array”;

?>

Output:

Array

14.is_object()

is_object() function is used to check whether given variable is object or not .if it is object it prints statement.

Example:

<?php

class Student

{

}

$k = new class();

if (is_object($k))

echo “It is an object”;

else

echo “It is not an object”;

?>

Output:

It is an object.

15.is_numeric()

is_numeric() function is used to check whether given variable is numeric or not.if it is true it prints one statement.othwise it prints another statement.

<?php

$xg= “143”;

if (is_numeric($g))

echo “It is numeric number”;

else

echo “It is not numeric number”;

?>

output:

It is numeric number.

16.intval()

intval() function is used to give integer value from variable.

<?php

$s=14.75;

echo intval($s);

?>

Output:

14

17.floatval()

floatval() function is used to give float value from the variable.

Example:

<?php

$s=14;

echo intval($s);

?>

Output:

14.00

18.strval()

Strval() function is used to give string value from a variable

Example:

<?php

$s=14;

echo strval($s);

echo gettype($s);

?>

Output:

14

String.

Frequently Asked Questions (FAQs)

1. What are form variables in PHP?

Answer: $GET and $POST

2 What is the difference between GET and POST in PHP?

Answer: GET is used for non sensitive data .post is used for sensitive and secure data

3.How do you access form data in PHP?

Answer: In php we have two method GET and POSt to access form data from the server.it is also called retrieving data from the post using methods.

4.Why is the name attribute important in PHP forms?

Answer: Name attribute is used to show the name of the field in form.

5.Which method is more secure: GET or POST?

Answer: POST is more secure than GET.

6.How do you validate form data in PHP?

Answer: In php there so many variable handling functions.so,that we are checking the data is correct,entered,secure or not .

7.What are PHP superglobal variables?

Answer: GET,POST, REQUEST, SERVER.

8.Can I use GET and POST in the same PHP application?

Answer: yes we can use both methods in one application.

 

Leave a Comment