Welcome Guest, Not a member yet? Register   Sign In
NULL and 0
#1

[eluser]dimaomni[/eluser]
Hello everybody!

I have some input_form().From they I receive array which can include key with digits and empty values.

This values I use for INSERT query, and when value has NULL - this involve sql error.
So I start to use array_filter(), and everything had been good before I needed use "0".

I've tried use this condition, however it like tautology and don't work ).

Code:
if ($mark>=0 && $mark<=12 && $mark != NULL)

What do you tell ?
#2

[eluser]Tominator[/eluser]
Hi!

Try this:
Code:
if((!empty($mark) || $mark == 0) && $mark <= 12 && !($mark < 0))

Tom.
#3

[eluser]Krzemo[/eluser]
empty will return true both for 0 and null

two possibilites:

1. if value is null change it to something acceptable
Code:
foreach($arr as $val)
{
    (! isset($val)) ? 0 : $val;
}
2. change table column to accept nulls

regs
#4

[eluser]EmmaMadeleine[/eluser]
thnxx Tominator
#5

[eluser]Tominator[/eluser]
More effective:
Code:
if((!empty($mark) || $mark == '0') && $mark <= 12 && !($mark < 0))

Cshamoh: Empty will not return true for 0 or null Smile
#6

[eluser]Krzemo[/eluser]
Taken from PHP manual:
Quote:The following things are considered to be empty:

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

So IMHO empty will return true both when 0 and NULL
#7

[eluser]Tominator[/eluser]
Oh I am sorry Big Grin I thought about !empty Big GrinD

Yes, 0 and NULL will return true (it is empty), but I am using !empty and my script was tested Smile

Sorry to you Cshamoh, I was wrong ...

Tom.
#8

[eluser]dimaomni[/eluser]
I've tried test with simpler code

Code:
&lt;?php
$a = array("1"=>'',"2"=>0,"3"=>4,"4"=>'',"5"=>NULL);

foreach ($a as $b)    {
if (!$b)
{
      echo "b ".$b."<br />";
}
}
?&gt;
result
b

b 0

b

b
----------------------
Code:
$a = array("1"=>'',"2"=>0,"3"=>4,"4"=>'',"5"=>NULL);

foreach ($a as $b)    {
if(!empty($b))
{
      
} else echo "b ".$b."<br />";
}
result
b

b 0

b

b
-------------------
Code:
&lt;?php
$a = array("1"=>'',"2"=>0,"3"=>4,"4"=>'',"5"=>NULL);

foreach ($a as $b)    {
if($b === 0)
{
    echo "b ".$b."<br />";  
}
}
?&gt;

result
b 0
-----------------
Code:
&lt;?php
$a = array("1"=>'',"2"=>0,"3"=>4,"4"=>'',"5"=>NULL);

foreach ($a as $b)    {
if($b === 0 or $b>1 && $b<12)
{
    echo "b ".$b."<br />";  
}
}
?&gt;

result

b 0

b 4

----------
lsat code works but in project still not work, I've asked a teacher and he gave solution:
Code:
if (preg_match("!^([0-9]|1[1-2])$!", $b)
)..
so it works everywhere.
#9

[eluser]Tominator[/eluser]
Have you tried my condition? Because it's faster than regexp (and works everywhere too) Smile
#10

[eluser]dimaomni[/eluser]
[quote author="Tominator" date="1272246124"]Have you tried my condition? Because it's faster than regexp (and works everywhere too) Smile[/quote]

At first I've tried in work example and it didn't. Just now I tried in simple code and it work.

Miracles aren't. Miracle-workers are. :-)

Need to look carefully to code.
I think topic have to be close.

Very thanks for all!!!




Theme © iAndrew 2016 - Forum software by © MyBB