Jump to content

check if two strings match not working


coenster

Recommended Posts

I have the following code, my bad if it is not in correct format... still learning.

<?php

$string = file_get_contents('/path/to/file');
$found = 'found';
       if($string == $found){
               echo "string match";
   }
       else {
               echo "string does not match found";
            }
echo "<br>";

echo "value of string: $string";
echo "<br>";
echo "value of found: $found";
?>

The output of that says: "string does not match found". and the value for $string and $found is the same. If the value of $string and $found are not the same it still says:"string does not match found". Am I missing something or am I going nuts... Please let me know what is wrong with my code.Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/295597-check-if-two-strings-match-not-working/
Share on other sites

check your path to the file.

 

I used your code with my path and got

<?php


$string = file_get_contents('found.txt');

$found = 'found';

        if($string == $found){

                echo "string match";

    }

        else {

                echo "string does not match found";

             }

echo "<br>";


echo "value of string: $string";

echo "<br>";

echo "value of found: $found";

?>

and displays



string match
value of string: found
value of found: found

Why do you "think" they match? Are there any white-space characters (e.g. spaces, tabs, etc.) in the file you are reading? You need to verify EXACTLY what those variables contain - echoing to the page isn't going to work

 

Run this and you will see why PHP doesn't consider those values the same

 

$string = file_get_contents('/path/to/file');
$found = 'found';
 
if($string == $found)
{
    echo "string match";
}
else
{
    echo "string does not match found";
}
 
echo "<br><br>String var: ";
var_dump($string);
 
echo "<br><br>Found var: ";
var_dump($found);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.