Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,673 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,167 people online right now. Registration is fast and FREE... Join Now!




Comparing a string from file and a variable

 
Reply to this topicStart new topic

Comparing a string from file and a variable

mudisoft
post 21 Jul, 2007 - 04:29 AM
Post #1


New D.I.C Head

*
Joined: 9 Jul, 2007
Posts: 11


My Contributions


Hello, I'm having a hard time comparing a string from a file line and a string in a variable...
I can print the line with the following code but can't compare what I've printed with a predefined variable.
Here's the display code:
CODE
<?php
$handle = @fopen("afile.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
        echo $buffer;
    }
    fclose($handle);
}
?>


Example of what I want to do:
I have a file with a string in it
If in the first (and only) line in the file says "1", the script prints "It's true"
Else, or if the file says "0", the script prints "It's false"

Thank you
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 21 Jul, 2007 - 09:50 PM
Post #2


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


What ever you seem to be doing in your script I am sure there is an easier way to do it. What are you doing with this script?
User is offlineProfile CardPM

Go to the top of the page

mudisoft
post 21 Jul, 2007 - 11:12 PM
Post #3


New D.I.C Head

*
Joined: 9 Jul, 2007
Posts: 11


My Contributions


This script displays the first line of a text file. When I try to compare the string it fails to load.
User is offlineProfile CardPM

Go to the top of the page

duffsstuff
post 22 Jul, 2007 - 06:55 AM
Post #4


D.I.C Head

**
Joined: 10 Sep, 2006
Posts: 67


My Contributions


You could try requiring the file like this:

What I did was use the file_get_contents() witch seems easier for what you want to do. I am assuming you know how to put in your own logic into the if else statment so I didn't bother with that. Hope this helps.
CODE

<?php

// File
$file = 'your_file.txt';

// get the info inside the file
$file_content = file_get_contents('$file');

// logic
if ( $file_content ) {
print "It's true";
} else {
print "It's False";
}


This post has been edited by duffsstuff: 22 Jul, 2007 - 06:56 AM
User is offlineProfile CardPM

Go to the top of the page

mudisoft
post 22 Jul, 2007 - 10:27 AM
Post #5


New D.I.C Head

*
Joined: 9 Jul, 2007
Posts: 11


My Contributions


Thank you for the idea!
I print the string successfully, but again cannot compare it...
CODE

<?php
$file = "input.txt";

// I've removed the quotation marks here in order for the script to work
$file_content = file_get_contents($file);

print $file_content;

?>


I dunno how to write a proper "if" statement for the comparison of the strings. I put only "1" in the text file and make the php to ask if it's "1" or not, but it always says "False" as it isn't.

CODE

//I've tried with quotation marks here, doesn't help
if ($file_content == 1) {
print "True";
    } else {
print "False";
}


I now started thinking again and started to wonder if it has something to do with the encoding of the input file or something... Can this be the problem somehow...?
User is offlineProfile CardPM

Go to the top of the page

Styx
post 24 Jul, 2007 - 10:14 AM
Post #6


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


That should work just fine.

test.php
CODE
<?php
$file = "input.txt";
$file_content = file_get_contents($file);

print $file_content;

print ($file_content == 1) ? 'True' : 'False';
?>

input.txt
CODE
1


Displays 1True

Check that input.txt exists in the current directory and that it is readable.
User is offlineProfile CardPM

Go to the top of the page

mudisoft
post 25 Jul, 2007 - 02:38 PM
Post #7


New D.I.C Head

*
Joined: 9 Jul, 2007
Posts: 11


My Contributions


Thank you for your posts. I uploaded the scripts to my web host and now everything works just fine. I use file_get_contents and it's a great function and I thank you again!
Seemed as 127.0.0.1's php was malfunctioning. I reinstalled and now it's fine...
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:14AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month