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

Echoing the integer 0 (zero)

P: n/a
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:

$foo = 0 ;
echo 'foo is ' . $foo ;

yields:

foo is

not

foo is 0

So far I've been kludging it with $foo?$foo:'0', but I find it strange
to have to do that, not to mention annoying :-) Is it something I'm
doing, or is that a real deficiency in the language?

Thanks,
Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Margaret MacDonald, being the foo Margaret MacDonald is, wrote:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:

$foo = 0 ;
echo 'foo is ' . $foo ;

yields:

foo is

not

foo is 0

So far I've been kludging it with $foo?$foo:'0', but I find it strange
to have to do that, not to mention annoying :-) Is it something I'm
doing, or is that a real deficiency in the language?

Thanks,
Margaret


0 is the same thing as NULL. If you did:

$foo = '0'; //this makes foo a string
echo 'foo is ' . $foo;

It would display a zero;

--
Sharif T. Karim
....you don't know wrath yet...
Jul 17 '05 #2

P: n/a
Sharif T. Karim wrote:
Margaret MacDonald, being the foo Margaret MacDonald is, wrote:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:

$foo = 0 ;
echo 'foo is ' . $foo ;

yields:

foo is

not

foo is 0

So far I've been kludging it with $foo?$foo:'0', but I find it strange
to have to do that, not to mention annoying :-) Is it something I'm
doing, or is that a real deficiency in the language?

Thanks,
Margaret
0 is the same thing as NULL. If you did:


Are you sure? According to the documentation, NULL and zero are
different values. Unless I misread something, a variable only has a
NULL value either before it's initialised or if it's explicitly set to
NULL. Setting it to integer 0 should have the same effect (apart from
the actual value) as setting it to integer 1.

$foo = '0'; //this makes foo a string
echo 'foo is ' . $foo;

It would display a zero;


--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #3

P: n/a
On Fri, 09 Jul 2004 16:29:47 GMT, Margaret MacDonald
<sc**********@att.not.invalid> wrote:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:

$foo = 0 ;
echo 'foo is ' . $foo ;

yields:

foo is

not

foo is 0

So far I've been kludging it with $foo?$foo:'0', but I find it strange
to have to do that, not to mention annoying :-) Is it something I'm
doing, or is that a real deficiency in the language?


What version do you get this behaviour in? It appears wrong and is
inconsistent with the results I get, which are as expected:

andyh@server:~/public_html$ php -v
PHP 4.3.7RC1 (cli) (built: May 29 2004 20:17:57)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

andyh@server:~/public_html$ cat test.php
<?php
$foo = 0 ;
echo 'foo is ' . $foo ;
echo "\n";
?>

andyh@server:~/public_html$ php -q test.php
foo is 0

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

P: n/a
On Fri, 09 Jul 2004 18:40:39 GMT, "Sharif T. Karim" <sh****@nyc.rr.com> wrote:
0 is the same thing as NULL.


No, it isn't.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

P: n/a

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:rc********************************@4ax.com...
On Fri, 09 Jul 2004 16:29:47 GMT, Margaret MacDonald
<sc**********@att.not.invalid> wrote:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:

$foo = 0 ;
echo 'foo is ' . $foo ;

yields:

foo is

not

foo is 0

So far I've been kludging it with $foo?$foo:'0', but I find it strange
to have to do that, not to mention annoying :-) Is it something I'm
doing, or is that a real deficiency in the language?


What version do you get this behaviour in? It appears wrong and is
inconsistent with the results I get, which are as expected:

andyh@server:~/public_html$ php -v
PHP 4.3.7RC1 (cli) (built: May 29 2004 20:17:57)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

andyh@server:~/public_html$ cat test.php
<?php
$foo = 0 ;
echo 'foo is ' . $foo ;
echo "\n";
?>

andyh@server:~/public_html$ php -q test.php
foo is 0

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


It is wrong and unexpected, for sure.

OP, perhaps you should try

echo "foo is ".(int)$foo;

See if that helps out.
-- Matt
Jul 17 '05 #6

P: n/a
Andy Hassall wrote:
On Fri, 09 Jul 2004 16:29:47 GMT, Margaret MacDonald
<sc**********@att.not.invalid> wrote:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:

$foo = 0 ;
echo 'foo is ' . $foo ;

yields:

foo is

not

foo is 0

So far I've been kludging it with $foo?$foo:'0', but I find it strange
to have to do that, not to mention annoying :-) Is it something I'm
doing, or is that a real deficiency in the language?


What version do you get this behaviour in? It appears wrong and is
inconsistent with the results I get, which are as expected:

andyh@server:~/public_html$ php -v
PHP 4.3.7RC1 (cli) (built: May 29 2004 20:17:57)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

andyh@server:~/public_html$ cat test.php
<?php
$foo = 0 ;
echo 'foo is ' . $foo ;
echo "\n";
?>

andyh@server:~/public_html$ php -q test.php
foo is 0


I'm running 4.3.7, but it appeared in 4.3.0 before that. But I must
be bending something out of shape, because the problem appears in the
production code, but not (now) in test code.

How annoying. I'll have to see if I can figure out what I'm doing
that's confusing it.

Thanks for the response, Andy.
Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.