|
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
==================
Navigation:
[Reply to this message]
|