470,862 Members | 1,769 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

The same operation on a number of variables.

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
Jul 17 '05 #1
2 1344
In article <1n****************@www.okozaoko.pl>, Pawe? <pm**@op.pl>
wrote:
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


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

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2
Paweł wrote:
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 ???


<?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 :
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Magix | last post: by
12 posts views Thread by Chadwick Boggs | last post: by
4 posts views Thread by John Dann | last post: by
16 posts views Thread by foolsmart2005 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.