473,287 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Some strings are not true ?

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

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

Why ?

Dec 31 '07 #1
5 1050
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*********@kdetalk.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...@kdetalk.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.comwrote:
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.com
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
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...
7
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...
42
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
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...
4
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
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...
193
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...
44
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...
4
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.