473,789 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some strings are not true ?

This is true: "2-3"==1

But "1-2"==1 is not true

Why ?

Dec 31 '07 #1
5 1065
gr*****@reenie. org wrote:
This is true: "2-3"==1

But "1-2"==1 is not true

Why ?
RTFM on type juggling. You may not be doing a comparison between integers or
between strings. Force type casting where appropiate.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

MSN:i_********* *************** *@hotmail.com
Jabber:iv****** ***@jabber.org ; iv*********@kde talk.net
Dec 31 '07 #2
On Dec 31, 1:24 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
grou...@reenie. org wrote:
This is true: "2-3"==1
But "1-2"==1 is not true
Why ?

RTFM on type juggling. You may not be doing a comparison between integers or
between strings. Force type casting where appropiate.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

MSN:i_eat_s_p_a _m_for_breakf.. .@hotmail.com
Jabber:ivansanc ...@jabber.org ; ivansanc...@kde talk.net
Yes, I read that already. I know how to make it consistent with type
casting, but that is not my question.

In both cases, it is a comparison between a string and an integer.

The Manual says:
If the string starts with valid numeric data, this will be the value
used. Otherwise, the value will be 0 (zero).

Bot strings start with valid numeric data, so I still want to know why
one is true and the other is not.
Dec 31 '07 #3
gr*****@reenie. org wrote:
This is true: "2-3"==1

But "1-2"==1 is not true

Why ?
<?php
var_dump("2-3"==1);
var_dump("1-2"==1);
?>
Output:
bool(false)
bool(true)

Why? "2-3" evaluates to 2, "1-2" evaluates to 1. 2!=1 therefore the
first statement is false, the second one is true because 1==1

--
Posted via a free Usenet account from http://www.teranews.com

Dec 31 '07 #4
On Dec 31, 2:40 pm, Justin Koivisto <justin.koivi.. .@gmail.comwrot e:
grou...@reenie. org wrote:
This is true: "2-3"==1
But "1-2"==1 is not true
Why ?

<?php
var_dump("2-3"==1);
var_dump("1-2"==1);
?>
Output:
bool(false)
bool(true)

Why? "2-3" evaluates to 2, "1-2" evaluates to 1. 2!=1 therefore the
first statement is false, the second one is true because 1==1

--
Posted via a free Usenet account fromhttp://www.teranews.co m
OK, thanks, it makes sense now.
Dec 31 '07 #5
gr*****@reenie. org wrote:
>
This is true: "2-3"==1

But "1-2"==1 is not true

Why ?
I get exactly the opposite results with the antique PHP 4.1.2 on my Linux
system.

What's happening here is that PHP tries to convert the string to an integer
for the comparison, and that conversion stops at the first non-digit (the
"-").

So, converting "2-3" produces the integer 2, and converting "1-2" produces
the integer 1.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 31 '07 #6

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

Similar topics

7
1199
by: Stephan Diehl | last post by:
A while ago, I've posted a recipie about finding a common prefix to a list of strings. While the recipie itself is quite bad (I have to admit) and I didn't know at that time that this problem was solved already in the os.path module. The interesting part can be found in the commentaries, as this turned out to be a quest for the most efficient algorithm to solve this particular problem. All of this can be found at...
7
1642
by: Mike Thompson | last post by:
The interning of strings has me puzzled. Its seems to happen sometimes, but not others. I can't decern the pattern and I can't seem to find documentation regarding it. I can find documentation of a builtin called 'intern' but its use seems frowned upon these days. For example, using py2.3.3, I find that string interning does seem to happen sometimes ...
42
1656
by: Rigga | last post by:
Hi all, I am wondering why string's are not true objects?.... Let me explain... If i write the code Dim x1 as String = "veg" Dim x2 as String = "veg" If x1 = x2 then
3
1593
by: Mike | last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or comments on how to make this work I would be grateful! I have been working on this for the past 4 days and nothing I do seems to get me any closer to the solution. Below is a program that I am working on for a class project. The original code was provided for us which is what I have below. What we have to do is make the app run so that it allows the user to add...
4
2788
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
9
4064
by: sci | last post by:
I believe both ways to create an array of strings are correct. Is there any difference between these two? 1. char *MyString = {"First string", "Second string", ..."Tenth string"}; 2. char MyString = {"First string", "Second string", ..."Tenth string"};
193
9651
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
44
4197
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most recent call last): File "<interactive input>", line 1, in ?
4
7670
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that are NOT the word apple OR banana? Then what would be needed to match only strings that do not CONTAIN the word "apple" or "banana" or "cherry"?
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5404
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.