Page 1 of 1

getting requested variables in another way

Posted: Thu Jun 09, 2005 12:39 am
by harrison
hi, this is my first post here.
I observed most of our codings regarding the ways to get $_POST variables.

Code: Select all

$lname = (isset($_POST['lname'])?$_POST['lname']:'');
$fname = (isset($_POST['fname'])?$_POST['fname']:'');
$address = (isset($_POST['address'])?$_POST['address']:'');
// etc.
I am wondering if i could do this:

Code: Select all

$varnames = array('lname','fname','address');
foreach($varnames as $var){
$($var) = (isset($_POST[$var])?$_POST[$var]:'');
}
... without messing with any rules or sacrificing memory resources?

Posted: Thu Jun 09, 2005 12:46 am
by Syranide
that is possible, or you just create a function which takes the two arguments, one value, and one default (which could default to '').

and if the value is NULL is returns the default instead.

thus making it like this

$lname = default( $_POST['lname'] );

NOTE: but, yes your solution works, however, I would still proceed with caution, as you shouldn't commonly use default values for user-submitted data, you should instead use "isset" and adapt according to what you get. as, getting fname, but not lname would certainly prove to be wrong and shouldn't proceed with registering him without his lname (e.g.).

Posted: Thu Jun 09, 2005 1:05 am
by harrison
thanks, sir.
i saw a version of that function

Code: Select all

$lname = either($_POST['lname'],DEFAULT_LNAME);
which chooses the default value if first argument is null or not set.