Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way for checking $_REQUEST variable

Tags:

php

request

$p = (isset($_REQUEST["p"])?$_REQUEST["p"]:"");

This is the common line I usually use in my php code. I always assume is there a better(small and faster) way to write the same ?

like image 394
GajendraSinghParihar Avatar asked Oct 17 '12 11:10

GajendraSinghParihar


People also ask

How can we use $_ GET $_ POST $_ request variable in PHP?

How to use it? Before you can use the the $_POST variable you have to have a form in html that has the method equal to POST. Then in the php, you can use the $_POST variable to get the data that you wanted. The $_POST syntax is ($_POST['name of the form field goes here']).

Why do we use $_ request variable?

The $_REQUEST variable is used to read the data from the submitted HTML form. Sample code: Here, the $_REQUEST variable is used to read the submitted form field with the name 'username'. If the form is submitted without any value, then it will print as “Name is empty”, otherwise it will print the submitted value.

What is $_ request variable?

PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag.


1 Answers

Create your own function :

function getIfSet(&$value, $default = null)
{
    return isset($value) ? $value : $default;
}

$p = getIfSet($_REQUEST['p']);

There's no other clean solution.

like image 161
Aelios Avatar answered Sep 30 '22 08:09

Aelios