$myText and $myZero

[b]code[/b]

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>if clause4</title>
</head>
<body>

<?php 
$myText="myText";
$myZero=0;

$yes="yes";
$no="no";

if($myZero==$myText)
echo $yes;
else
echo $no;
?>
</body>
</html>

[b]result[/b]

yes

Although the code in http://dot.kr/x-test/if-clause4.php should produce, I think, “no” because the value of $myZero is different from the value of $myText, it produces “yes.”
What’s wrong in my code or in my PHP?

because you are comparing a string to an integer.
from the manual:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

Source: http://php.net/manual/en/language.operators.comparison.php

What is the difference between “===” and “==?”
I like to know whether it’s just a typo or it has special meaning?

RTFM (Read The Fine Manual)

the answer to your question is in the link spikeZ posted

if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
from http://php.net/manual/en/language.operators.comparison.php

What types are there in PHP?
A number is different from text in type?

RTFM (Read The Fine Manual)

you might find it helpful to bookmark the link below in your browser

I know what you meant.

I have reading problems(manual) caused by some reasons.
(One of the reasons is English.)

you can choose to view the manual in a wide range of different languages.

Data types… Oh boy.

PHP is a “loosely typed” language. That is, it doesn’t force you to tell the computer what a variable is when you declare it. Many scripting languages are this way - JavaScript for example, and even python if memory serves correctly. The problem though is sometimes it is important to know not only the value of a variable, but what the variable is.

A variable’s datatype is what the variable is. Datatypes are pretty consistent between programming languages, at a minimum there will be integers, booleans, strings, floats and nulls. Most languages have arrays and objects as well.

When you ask PHP to compare two variables it has get them both to the same datatype to do the comparison. This happens silently in the background. Most of the time things will work out as you expect, but not always.

A classic example comes out of [fphp]strpos[/fphp] This function returns the index position of a string in another string. And like most computer indexes it starts with 0, so if the string is at the start strpos returns 0. If strpos can’t find the string you’re looking for it returns false.

Here’s the trap. 0, ‘0’, false, null, all loosely evaluate to false. That is, 0 == false.

Live code…


$a = 'pot';
$b = 'potato';

if (strpos($b, $a) == 0) {
  echo 'This will print.'; // Because 0 == 0 is true.
}

$a = hot;

if (strpos($b, $a) == 0) {
  echo 'This will print'; // Because, false == 0 is also true.  Unintuitive yes, but that's how datatypes can be.
}

if (strpos($b, $a) === 0 {
  echo 'This will not print'; // Because, false === 0 is false. The third = is strict comparison.
}

if (strpos($b, $a) === false) {
  echo 'This will print'; // Because false === false.
}

$a = 'pot'

if (strpos($b, $a) === false) {
  echo 'This will not print'; // Because now strpos returns 0 - 'pot' is at the start of 'potato'. The eval will be 0 === false, which isn't true.
}

This is where strict comparison comes in - for those times when you need to know not only what the variable’s value is, but what type of variable is it. For a strict comparison to be true both value and type must match.

To recap…


if (0 == false) {
  echo 'true'; // Echoes true - because this is true.
}

if ( 0 === false ) {
  echo 'true'; // Nothing echoed - this line will be skipped because the condition is false.
}

if ( false === false ) {
  echo 'true'; // This however is true.
}

if (true == 'a string') {
  echo 'true'; // Strings will evaluate true unless they parse to 0 or are empty.
}

if (false == '' ) {
  echo 'true'; // This line echoes, an empty string evals false.
}

if ( false == '0' ) {
  echo 'true'; // But so will this, so be careful.
}

 if ( false == '000' ) {
  echo 'true'; // Also parses to 0.
}

If this seems intimidating, well it can be. PHP tries pretty hard to keep this from coming up but sometimes it’s unavoidable. In strongly typed languages such as C or Java you must declare the type of variable. That is a blessing and a curse since you don’t hit corner cases like this, yet doing things in the first place is more difficult.

Off Topic:

Kalon, being rude to others hardly encourages anyone to be nice to you. If you don’t want to answer a new user’s question that’s fine, but posting RTFM is hardly constructive and ‘fine’ was not the f word you were looking for, everyone knows that.

Off Topic:

other words I’ve seen used are “Flipping”, “Flaming”, “Fantastic”, “Famous”

also, the rtfm link I posted directed the OP to the appropriate page in the manual to answer his question

But I suppose RTFM could also mean “Read This For More”.

So that I don’t offend people with thin skins in the future, I’ll use the 2nd acronym from now on :slight_smile:

Thank you very much, Michael Morris.
Very thank to Kalon for everything you give me.

you’re welcome :slight_smile:

no offence was intended and I apologise if you were offended.

I know you intended to lead me to the right track which is getting more structured knowledge.

I apologise for asking too small and many things without googling or reading manual.