Connecting Tech Pros Worldwide Forums | Help | Site Map

The same operation on a number of variables.

Paweł
Guest
 
Posts: n/a
#1: Jul 17 '05
I have a big number of variables, mostly data from the form.
I want to convert all empty variables into "NULL".
Obviously for each variable I could write:
if (empty($a)) $a=="NULL";
if (empty($b)) $b=="NULL";...and so on....
But let's say I have like a hundred of them.
Is there any way to write it in just a few rows ???

Thanx

Jan Pieter Kunst
Guest
 
Posts: n/a
#2: Jul 17 '05

re: The same operation on a number of variables.


In article <1n9rwomx8jqem$.dlg@www.okozaoko.pl>, Pawe? <pmg3@op.pl>
wrote:
[color=blue]
> I have a big number of variables, mostly data from the form.
> I want to convert all empty variables into "NULL".
> Obviously for each variable I could write:
> if (empty($a)) $a=="NULL";
> if (empty($b)) $b=="NULL";...and so on....
> But let's say I have like a hundred of them.
> Is there any way to write it in just a few rows ???
>
> Thanx[/color]

If they come from a form, they are part of the $_POST (or $_GET) array,
so you could loop over that.

JP

--
Sorry, <devnull@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Pedro Graca
Guest
 
Posts: n/a
#3: Jul 17 '05

re: The same operation on a number of variables.


Paweł wrote:[color=blue]
> I have a big number of variables, mostly data from the form.
> I want to convert all empty variables into "NULL".
> Obviously for each variable I could write:
> if (empty($a)) $a=="NULL";
> if (empty($b)) $b=="NULL";...and so on....
> But let's say I have like a hundred of them.
> Is there any way to write it in just a few rows ???[/color]

<?php
$variable_names = array('a', 'b', 'c1', 'c2'/*, ...*/);
foreach ($variable_names as $var)
if (empty($$var)) $$var = 'NULL';
?>

Read all about "variable variables" here:
http://www.php.net/manual/en/languag...s.variable.php
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Closed Thread