true, why?" />
T
T
trickii2011-11-21 15:46:26
PHP
trickii, 2011-11-21 15:46:26

("PHP" == 0) => true, why?

Hello,
the question about the operation itself was already in q&a: Types in PHP. Template break . The answer comes down to a simple one - "PATAMUSHTA".
In short:
var_dump(&quot;php&quot; == 0); //true<br/>
My question is, why? In any case, this non-standard behavior was implemented with a purpose. Does anyone have real examples of using this feature or meeting with a rake because of it?

Answer the question

In order to leave comments, you need to log in

9 answer(s)
A
Alexey Sidorov, 2011-11-21
@Gortauer87

I wonder why a non-empty string is cast to zero when it should at least be cast to one?

B
Boris, 2011-11-21
@zIs

let's point by point.
1. There is no strong typing in PHP. It is a fact.
2. It follows from item 1 that types will be cast automatically in some cases.
3. The string is converted to a number exactly like this: (int) "PHP"; // 0
(it seems logical to me)
4. To compare two different types, you need to cast one to the other. It's also stupid to argue with this.
5. Here, perhaps, a non-obvious point - in PHP for this, the string is converted to a number. This is described in the documentation.

V
Vladimir Chernyshev, 2011-11-21
@VolCh

Practical application:
An input field that implies a numeric value, such as the transfer amount. The user enters the string "thousand bucks". There is no normal validation (why is a separate question). Transfer goes only if the amount is greater than zero. The string is converted to zero and the translation will not go away. If it had been converted to 1 (true), then the transfer would have left with an amount not obvious to the user, a minimum of a bug feature. And even better -1 (true true :) ) and no validation at all - the translation would turn out to be negative - a critical bug.
PHP, IMHO, was designed to reduce the number of such critical bugs.

E
edogs, 2011-11-21
@edogs

The answer there was not "patamushta", but "because the string type is converted to numeric".
And then the logic becomes more understandable (although its reasonableness is still doubtful).
Do not forget that php language is initially simple for building simple pages. From there, there are all sorts of things like global (so as not to complicate your life $_GET) and autoslashes (so as not to complicate your life addaslashes ($_GET) when inserting into the database).
Practice? For example:
var_dump("1 rub"==1);//true

E
edogs, 2011-11-21
@edogs

And, concerning a rake still there was a question.
Look for one of the vulnerability descriptions in phpbb, quite critical. There, one of the checks was of the form if($a==$b), while $b was taken from the serialized array stored in the user's cookies. Accordingly, the script expected a string variable and a comparison like "login"=="admin", and then suddenly a number came, and the comparison turned into "login"==0, after which an epic fail occurred. Well, it's not a very accurate story, it just boiled down to that.

G
gro, 2011-11-21
@gro

The correct answer was repeatedly given here - “the string is reduced to a number”, but why this is done for many, even with correct answers, is not obvious.
In general, everything is simple - $_GET and $_POST are always strings, although some variables are assumed to be numeric, often even numeric fields come from the database in string form, well, one of the main tasks of PHP is text processing (including XML), where again You still have to work with numbers as strings.
Therefore, strings are often considered as numbers.

S
shagguboy, 2011-11-21
@shagguboy

> My question is, why?
PHP was conceived as a loosely typed language. very non-strict. so it's just a language bug.
>Does anyone have real examples of using this feature
, I constantly write something like
if($str){

}
else{

}

S
Strate, 2011-11-23
@Strate

var_dump("php" == 0); // true
var_dump((int)"php" == 0); //true
var_dump("php" == (string)0); //false

T
trickii, 2011-11-25
@trickii

Here, by the way, is an example of a rake, just found yesterday:
From the service we get the rate as a number or "--" in case of absence. Next was the following code: Which was executed incorrectly, i.e. "--" became -0. It is clear that "--" is not the topic here, but these are the realities, the service is not ours. What is characteristic (integer) gives just 0.
$price = count($price) > 0 ? (float) str_replace(',', '.', $price[0]) : 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question