Help With Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ladieballer2004
Forum Newbie
Posts: 3
Joined: Wed Jun 10, 2009 3:32 am

Help With Arrays

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help With Arrays

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post by Ollie Saunders »

I'd only recommend the last one. The others imply different intent.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help With Arrays

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post 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. *
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help With Arrays

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: Help With Arrays

Post by redmonkey »

jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
Are you sure about that?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post 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. :-)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post 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>?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help With Arrays

Post 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.
Last edited by jackpf on Mon Aug 17, 2009 7:43 pm, edited 1 time in total.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post by Ollie Saunders »

Well, I like to be kind of useful. :-)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: Help With Arrays

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Help With Arrays

Post by Ollie Saunders »

Because that's never going to be a bottleneck to performance of an application, ever.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help With Arrays

Post 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??
Post Reply