[PHP] PHP Noob

Sprouts

Regular Member
Joined
Mar 20, 2010
Messages
463
Reaction score
328
I'm trying to code a small project and I'm pretty noob at php. I've coded the following with the help of google but it doesn't seem to work?

PHP:
<?php

$file1 = "1.txt";
$openfile1 = fopen($file1, 'r');
$data1 = fread($openfile1, 150);
fclose($openfile1);
echo $data1;
?>

<br>

<?php

$file2 = "2.txt";
$openfile2 = fopen($file2, 'r');
$data2 = fread($openfile2, 150);
fclose($openfile2);
echo $data2;

?>

<br>

<?php

if ($data1 = $data2) {
echo "the files are equal";
} else {
echo "the files are not equal";
}

?>

When I run the PHP file off my server there are no errors and this is what is shown:

quikla
quikly
the files are equal

1.txt contains the text "quickla"
2.txt contains the text "quickly"

How can the files be equal?

The data is echoed correctly so it's not that, the files are definitely different so it's not that, the else statement works as I tested it with "1 < 2" and "2 < 1" and it told me the correct results?

I'm really confused.

EDIT: Both files have 777 permissions too and the problem still occurs?!
 
Last edited:

bannedfree

Regular Member
Joined
Jan 31, 2010
Messages
444
Reaction score
43
And yeah dude maintain coding ethics. That will save you a lot of time from tracking errors.

instead of:

if (
$data1 = $data2) {
echo
"the files are equal";
} else {
echo
"the files are not equal";
}


write it this way:

if (
$data1 = $data2)
{
echo
"the files are equal";
}
else
{
echo
"the files are not equal";
}
 

Sprouts

Regular Member
Joined
Mar 20, 2010
Messages
463
Reaction score
328
Oh right thanks.

And yeah dude maintain coding ethics. That will save you a lot of time from tracking errors.

instead of:

if (
$data1 = $data2) {
echo
"the files are equal";
} else {
echo
"the files are not equal";
}


write it this way:

if (
$data1 = $data2)
{
echo
"the files are equal";
}
else
{
echo
"the files are not equal";
}
 

Entrepreneur

BANNED
Joined
Oct 12, 2007
Messages
439
Reaction score
391
And yeah dude maintain coding ethics. That will save you a lot of time from tracking errors.

instead of:

if (
$data1 = $data2) {
echo
"the files are equal";
} else {
echo
"the files are not equal";
}


write it this way:

if (
$data1 = $data2)
{
echo
"the files are equal";
}
else
{
echo
"the files are not equal";
}

I actually consider it to be wrote wrong in your example and correct in his, but each to their own I suppose. :)
 

smooshie

Newbie
Joined
Dec 27, 2009
Messages
18
Reaction score
2
yea i was gonna say too, the way he wrote is the most widely used and professional code ethic, not the break after each line. Hurray for php not caring about white space
 
Top