473,499 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

0 vs false

So, this was driving me insane today, and I'm sure there's an easy
answer that I'm just missing.

Say I have an expression where 0 is acceptable as a TRUE response. But
how, in PHP, do I say that 0 = true since it treats 0 = FALSE = "".
For example, if I look for "foo" in a string using strpos:

if (strpos("foo bar", "foo")) {
echo "foo found at the beginning of the phrase.";
}

This should evaluate to TRUE, but it doesn't since it finds "food" at
position 0, which then evaluates to FALSE.

I tried casting, isset, is_null, and a few other siller options. I
eventually worked around it by using substr_count, but mostly I'm just
curious about the general answer to the question.

Thanks.

Apr 24 '06 #1
4 5679
es******@gmail.com wrote:
So, this was driving me insane today, and I'm sure there's an easy
answer that I'm just missing.

Say I have an expression where 0 is acceptable as a TRUE response. But
how, in PHP, do I say that 0 = true since it treats 0 = FALSE = "".
For example, if I look for "foo" in a string using strpos:

if (strpos("foo bar", "foo")) {
echo "foo found at the beginning of the phrase.";
}

This should evaluate to TRUE, but it doesn't since it finds "food" at
position 0, which then evaluates to FALSE.

I tried casting, isset, is_null, and a few other siller options. I
eventually worked around it by using substr_count, but mostly I'm just
curious about the general answer to the question.

Thanks.


if (strpos("foo bar", "foo") !== false) {
echo "foo found";
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 24 '06 #2
Message-ID: <11**********************@u72g2000cwu.googlegroups .com> from
es******@gmail.com contained the following:

Say I have an expression where 0 is acceptable as a TRUE response. But
how, in PHP, do I say that 0 = true since it treats 0 = FALSE = "".


You'll be embarrassed to learn that there is a section on is in the
manual. ;-)

http://uk.php.net/strpos

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Apr 24 '06 #3
Argh, a RTFM would have saved me an hour. Thanks, though! That, of
course, solved my problem right away.

Apr 24 '06 #4
Argh, a RTFM would have saved me an hour. Thanks, though! That, of
course, solved my problem right away.

Apr 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1737
by: Sylvain Thenault | last post by:
Hi there ! Can someone explain me the following behaviour ? >>> l = >>> 0 in (l is False) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iterable argument...
3
2533
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
35
3339
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
15
2297
by: F. Da Costa | last post by:
Hi all, Following two sniperts of code I'm using and getting very interesting results from. ..html <tr id="1" class="segment" open="false"> This is the segment under 'investigation' ..js
14
2436
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
17
2531
by: Tatu Portin | last post by:
Is FALSE == 0? In other words, does 'function ()' execute on all platforms: if ( (1 == 2) == 0 ) function (); if ( (7!) == 0 ) function (); I would like if you could point to some...
10
3258
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
30
3097
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
6
5557
by: Java script Dude | last post by:
Small glitch (IMHO) in JavaScript is related to isNaN() boolean function. If passed a null, it returns true which is incorrect. This one cause me quite a bit of grief until I detected it and was...
14
3310
by: Prateek | last post by:
I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.