Reply to Re: Help needed with is_null

Your name:

Reply:


Posted by Jerry Stuckle on 08/04/07 13:00

Big Moxy wrote:
> On Aug 3, 8:22 pm, Jerry Stuckle <jstuck...@attglobal.net> 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
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация