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
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
Edited by dodgeitorelse3
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.