Help needed with is_null
am 04.08.2007 03:05:46 von Big Moxy
Would someone be so kind as to tell me what is wrong with this code?
(1) This code block indicates the session variable nomex is null.
if (is_null($_SESSION['nomex'])){
echo " is null.";
}
else {
echo " is not null.";
}
(2) However on the same page this code block always adds 100 to the
price.
if ($_SESSION['nomex'] != "^^no^^"){
if (!is_null($_SESSION['nomex'])){
// NOMEX Lining
$new_price = $new_price + 100;
}
}
echo "$".$new_price;
Thank you very much!!
Re: Help needed with is_null
am 04.08.2007 04:22:11 von Jerry Stuckle
Big Moxy wrote:
> Would someone be so kind as to tell me what is wrong with this code?
>
> (1) This code block indicates the session variable nomex is null.
> if (is_null($_SESSION['nomex'])){
> echo " is null.";
> }
> else {
> echo " is not null.";
> }
>
> (2) However on the same page this code block always adds 100 to the
> price.
> if ($_SESSION['nomex'] != "^^no^^"){
If it wasn't defined before, it is now.
> if (!is_null($_SESSION['nomex'])){
So this will never be true.
> // NOMEX Lining
> $new_price = $new_price + 100;
> }
> }
> echo "$".$new_price;
>
> Thank you very much!!
>
BTW - if you're checking to see if something is set, isset() is better.
But either way, check BEFORE you reference it.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Help needed with is_null
am 04.08.2007 05:00:28 von Big Moxy
On Aug 3, 8:22 pm, Jerry Stuckle wrote:
> Big Moxy wrote:
> > Would someone be so kind as to tell me what is wrong with this code?
>
> > (1) This code block indicates the session variable nomex is null.
> > if (is_null($_SESSION['nomex'])){
> > echo " is null.";
> > }
> > else {
> > echo " is not null.";
> > }
>
> > (2) However on the same page this code block always adds 100 to the
> > price.
> > if ($_SESSION['nomex'] != "^^no^^"){
>
> If it wasn't defined before, it is now.
>
> > if (!is_null($_SESSION['nomex'])){
>
> So this will never be true.
>
> > // NOMEX Lining
> > $new_price = $new_price + 100;
> > }
> > }
> > echo "$".$new_price;
>
> > Thank you very much!!
>
> BTW - if you're checking to see if something is set, isset() is better.
> But either way, check BEFORE you reference it.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
I think I understand your response however I am a PHP newbie so could
you please redo my code with isset()?
Thank you!
Re: Help needed with is_null
am 04.08.2007 06:30:32 von Matt Madrid
Jerry Stuckle wrote:
> Big Moxy wrote:
>> Would someone be so kind as to tell me what is wrong with this code?
>>
>> (1) This code block indicates the session variable nomex is null.
>> if (is_null($_SESSION['nomex'])){
>> echo " is null.";
>> }
>> else {
>> echo " is not null.";
>> }
>>
>> (2) However on the same page this code block always adds 100 to the
>> price.
>> if ($_SESSION['nomex'] != "^^no^^"){
>
> If it wasn't defined before, it is now.
Maybe, but if referencing an undefined variable sets it, it will be set to NULL.
if ($_SESSION['nothing']){}
var_dump($_SESSION['nothing']);
output: NULL
>
>> if (!is_null($_SESSION['nomex'])){
>
> So this will never be true.
What do you mean it will never be true. It will be true if that variable
had a non-null value to begin with.
>
> BTW - if you're checking to see if something is set, isset() is better.
> But either way, check BEFORE you reference it.
>
Either way in this case.
Re: Help needed with is_null
am 04.08.2007 06:39:38 von Matt Madrid
Big Moxy wrote:
> Would someone be so kind as to tell me what is wrong with this code?
Nothing that I can see...
>
> (1) This code block indicates the session variable nomex is null.
> if (is_null($_SESSION['nomex'])){
> echo " is null.";
> }
> else {
> echo " is not null.";
> }
>
> (2) However on the same page this code block always adds 100 to the
> price.
That can't be true. If the above code said it was NULL, then the
second IF block below can't be executing. Are you sure that there
isn't something in between these two blocks of code. Is this even
your "real" code, or just a quick rewrite.
> if ($_SESSION['nomex'] != "^^no^^"){
> if (!is_null($_SESSION['nomex'])){
> // NOMEX Lining
> $new_price = $new_price + 100;
> }
> }
> echo "$".$new_price;
>
I execute this code:
-------
$new_price = 49;
if (is_null($_SESSION['nomex'])){
echo " is null.";
}
else {
echo " is not null.";
}
if ($_SESSION['nomex'] != "^^no^^"){
if (!is_null($_SESSION['nomex'])){
// NOMEX Lining
$new_price = $new_price + 100;
}
}
echo "$".$new_price;
-------
Output is:
is null.$49
Re: Help needed with is_null
am 04.08.2007 14:14:16 von Toby A Inkster
Big Moxy wrote:
> if ($_SESSION['nomex'] != "^^no^^"){
> if (!is_null($_SESSION['nomex'])){
> // NOMEX Lining
> $new_price = $new_price + 100;
> }
> }
> echo "$".$new_price;
if (isset($_SESSION['nomex']) && $_SESSION['nomex']!='^^no^^')
$new_price += 100;
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 44 days, 15:52.]
Command Line Interfaces, Again
http://tobyinkster.co.uk/blog/2007/08/02/command-line-again/
Re: Help needed with is_null
am 04.08.2007 14:46:08 von Jerry Stuckle
Matt Madrid wrote:
> Jerry Stuckle wrote:
>> Big Moxy wrote:
>>> Would someone be so kind as to tell me what is wrong with this code?
>>>
>>> (1) This code block indicates the session variable nomex is null.
>>> if (is_null($_SESSION['nomex'])){
>>> echo " is null.";
>>> }
>>> else {
>>> echo " is not null.";
>>> }
>>>
>>> (2) However on the same page this code block always adds 100 to the
>>> price.
>>> if ($_SESSION['nomex'] != "^^no^^"){
>>
>> If it wasn't defined before, it is now.
>
> Maybe, but if referencing an undefined variable sets it, it will be set
> to NULL.
>
> if ($_SESSION['nothing']){}
> var_dump($_SESSION['nothing']);
>
> output: NULL
>
>>
>>> if (!is_null($_SESSION['nomex'])){
>>
>> So this will never be true.
>
> What do you mean it will never be true. It will be true if that variable
> had a non-null value to begin with.
You're correct - it will be true in this case.
>
>>
>> BTW - if you're checking to see if something is set, isset() is
>> better. But either way, check BEFORE you reference it.
>>
>
> Either way in this case.
Not at all. There is a definite difference between isset() and isnull().
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Help needed with is_null
am 04.08.2007 15:00:45 von Jerry Stuckle
Big Moxy wrote:
> On Aug 3, 8:22 pm, Jerry Stuckle wrote:
>> Big Moxy wrote:
>>> Would someone be so kind as to tell me what is wrong with this code?
>>> (1) This code block indicates the session variable nomex is null.
>>> if (is_null($_SESSION['nomex'])){
>>> echo " is null.";
>>> }
>>> else {
>>> echo " is not null.";
>>> }
>>> (2) However on the same page this code block always adds 100 to the
>>> price.
>>> if ($_SESSION['nomex'] != "^^no^^"){
>> If it wasn't defined before, it is now.
>>
>>> if (!is_null($_SESSION['nomex'])){
>> So this will never be true.
>>
>>> // NOMEX Lining
>>> $new_price = $new_price + 100;
>>> }
>>> }
>>> echo "$".$new_price;
>>> Thank you very much!!
>> BTW - if you're checking to see if something is set, isset() is better.
>> But either way, check BEFORE you reference it.
>>
>
> I think I understand your response however I am a PHP newbie so could
> you please redo my code with isset()?
>
> Thank you!
>
It's not hard - just use isset() before you do anything else with it:
Note the difference between isset() and is_null(). isset() is a language
construct, not a function call (despite its looks). isset() doesn't
change anything.
is_null() is a function call, and when you pass a variable which doesn't
exist, it is initialized and set to null.
Now - I shouldn't have been answering at midnight last night :-). But
the code you should use would be more like:
if (isset($_SESSION['nomex'])){
echo " is initialized.";
}
else {
echo " is not initialized.";
}
....
if (isset($_SESSION['nomex'])) {
if (!is_null($_SESSION['nomex']) {
if ($_SESSION['nomex'] != "^^no^^"){
// NOMEX Lining
$new_price = $new_price + 100;
}
}
}
echo "$".$new_price;
And ensure you're not setting $_SESSION['nomex'] in between your first
and second tests.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================