Help With Arrays
Moderator: General Moderators
-
ladieballer2004
- Forum Newbie
- Posts: 3
- Joined: Wed Jun 10, 2009 3:32 am
Help With Arrays
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
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
Code: Select all
if($var == null)
if($var == '')
if($var == "")
if(empty($var))- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
I'd only recommend the last one. The others imply different intent.
Re: Help With Arrays
I'd recommend the first 
'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.
'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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
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
* Goes to update code in light of new realisation. *
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
* Goes to update code in light of new realisation. *
Re: Help With Arrays
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.
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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
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:The list goes on and on. It's just madness to me.
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'Re: Help With Arrays
Are you sure about that?jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
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. 
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
Who gives a <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>?redmonkey wrote:Are you sure about that?jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
Re: Help With Arrays
Actually I was wrong.redmonkey wrote:Are you sure about that?jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
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);Code: Select all
if(trim($value) == null){} // will work
if(empty(trim($value))) // will not workI then check the config with:
Code: Select all
if((bool) $config['value']){//do something}
Last edited by jackpf on Mon Aug 17, 2009 7:43 pm, edited 1 time in total.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
Well, I like to be kind of useful. 
Re: Help With Arrays
Unclear as to why my question should merit such a response but, OK, duly noted.Ollie Saunders wrote:Who gives a smurf?redmonkey wrote:Are you sure about that?jackpf wrote:Plus, empty() is a function, null is a constant, so it'll be quicker.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Help With Arrays
Because that's never going to be a bottleneck to performance of an application, ever.
Re: Help With Arrays
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??
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??