Page 1 of 2

Help With Arrays

Posted: Mon Aug 17, 2009 12:14 pm
by ladieballer2004
Ok so here it goes: I have an array that is attached to a web form. It's function is to ask the user what she has been involved in. There are 3 rows with 4 columns each. The user is required to fill out at least one row but not all three.
Here is the problem. When I test it out all three rows are entered into the database wether they hold information or not. How do I get the parser to recognize when a row is blank and ignore it before the info is inserted into the database?

Here is the PHP Code for the Array
forforeach( (array) $_POST['Activity'] as $row => $Act)
{
if
(
($Activity = trim($Act)) &&
($Position = trim($_POST['Position'][$row])) &&
($StartDate = trim($_POST['StartDate'][$row])) &&
($EndDate = trim($_POST['EndDate'][$row]))
)

}


$involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate)
VALUES ('$Activity','$Position','$StartDate','$EndDate')";


Thanks in advance

Re: Help With Arrays

Posted: Mon Aug 17, 2009 12:24 pm
by jackpf

Code: Select all

if($var == null)
if($var == '')
if($var == "")
if(empty($var))
Any of them will check if a variable is empty.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 4:09 pm
by Ollie Saunders
I'd only recommend the last one. The others imply different intent.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 6:12 pm
by jackpf
I'd recommend the first :P

'0' or (string) 0 counts as empty. Try posting '0' in this forum. phpBB uses empty(). It'll treat it as if the post is empty...when it's obviously not. I only use empty() on integers.

Plus, empty() is a function, null is a constant, so it'll be quicker.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 6:38 pm
by Ollie Saunders
I thought the author was looking to find out if an array was empty. Seeing as that's not the case I retract my last statement, although I'm sort of reinstating it slightly differently with what I'm about to say:

My preference is for === comparisons over == and an empty() call for general PHP's definition of emptiness. If you specifically want to know if you have an empty array I'd recommend $array === array() now that I think of it. jackpf, guess why I prefer this :-D

* Goes to update code in light of new realisation. *

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:04 pm
by jackpf
Well, empty() and null also represent an empty array...

However, if you use === with null, unless you've actually unset() it, or declared it null, it'll return false. So, an empty string will not === null, but it will == null. Because null is a data type...so it's obviously not a string.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:29 pm
by Ollie Saunders
That's all correct.

But the point is that $a == null will return true if $a is null, false, array(), 0, or empty string. To me this is very confusing and unpredictable. I don't want to have to ask myself "Hm, do I want this condition to pass if it's null, false, array(), 0, or empty string", I usually only have one value in mind and I want my code to be doing exactly what I think it should be. Also == creates the bizarre situations where these are true:

Code: Select all

"" == null
"" == 0
"" == false
false == 0
false == array()
0 == '0'
The list goes on and on. It's just madness to me.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:32 pm
by redmonkey
jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
Are you sure about that?

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:33 pm
by Ollie Saunders
One I particularly like (read: dislike) is that ('0' == false) is true but ('0' == null) is not. You have to make a special effort to remember that beauty! Of course you can argue this is useful but when you go to another language that say null and false are false and everything else is always true and you see that is never a problem, you'll change your mind too. :-)

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:33 pm
by Ollie Saunders
redmonkey wrote:
jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
Are you sure about that?
Who gives a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>?

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:40 pm
by jackpf
redmonkey wrote:
jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
Are you sure about that?
Actually I was wrong.

Check this out:

Code: Select all

<?php
$starttime = array_sum(explode(' ', microtime()));
 
$var = 'something';
 
for($i = 0; $i < 10000000; $i++)
{
    if($var == null){}
}
 
echo sprintf('Null took %s seconds...', array_sum(explode(' ', microtime())) - $starttime);
 
$starttime = array_sum(explode(' ', microtime()));
 
$var = 'something';
 
for($i = 0; $i < 10000000; $i++)
{
    if(empty($var)){}
}
 
echo '<br />'.sprintf('Empty() took %s seconds...', array_sum(explode(' ', microtime())) - $starttime);
On my pc NULL was about 0.06 seconds slower....how weird. Anyway, <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> that. I still prefer NULL, because you can do more stuff with it, like:

Code: Select all

if(trim($value) == null){} // will work
if(empty(trim($value))) // will not work
And @Ollie Saunders, that's kind of useful...because for my site's config, I store values as 0 for off, 1 for on (binary, basically).

I then check the config with:

Code: Select all

if((bool) $config['value']){//do something}
Config values are stored as strings...but without '0' being false, that wouldn't work.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 7:43 pm
by Ollie Saunders
Well, I like to be kind of useful. :-)

Re: Help With Arrays

Posted: Mon Aug 17, 2009 8:05 pm
by redmonkey
Ollie Saunders wrote:
redmonkey wrote:
jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
Are you sure about that?
Who gives a smurf?
Unclear as to why my question should merit such a response but, OK, duly noted.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 8:15 pm
by Ollie Saunders
Because that's never going to be a bottleneck to performance of an application, ever.

Re: Help With Arrays

Posted: Mon Aug 17, 2009 8:29 pm
by jackpf
I completely agree...but you'd be surprised how crazy people get over performance!

Like, people who use ++$i instead of $i++ to increment an int, because it's apparently faster. But what the hell?!?! If it is actually any faster, it'd be like what, 0.0000000000000000001 seconds difference or something??