By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Help needed with is_null

P: n/a
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!!

Aug 4 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
js*******@attglobal.net
==================
Aug 4 '07 #2

P: n/a
On Aug 3, 8:22 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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!

Aug 4 '07 #3

P: n/a
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.
Aug 4 '07 #4

P: n/a
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

Aug 4 '07 #5

P: n/a
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.
js*******@attglobal.net
==================
Aug 4 '07 #6

P: n/a
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/0...nd-line-again/
Aug 4 '07 #7

P: n/a
Big Moxy wrote:
On Aug 3, 8:22 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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.
js*******@attglobal.net
==================
Aug 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.