1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[PHP] PHP Noob

Discussion in 'PHP & Perl' started by Sprouts, Sep 4, 2010.

  1. Sprouts

    Sprouts Regular Member

    Joined:
    Mar 20, 2010
    Messages:
    461
    Likes Received:
    324
    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($openfile1150);
    fclose($openfile1);
    echo 
    $data1;
    ?>

    <br>

    <?php

    $file2 
    "2.txt";
    $openfile2 fopen($file2'r');
    $data2 fread($openfile2150);
    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:

    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: Sep 4, 2010
  2. flaktrak

    flaktrak Power Member

    Joined:
    Mar 1, 2010
    Messages:
    522
    Likes Received:
    139
    Location:
    South!
    if ($data1 = $data2) should be if ($data1 == $data2)
     
    • Thanks Thanks x 1
  3. Sprouts

    Sprouts Regular Member

    Joined:
    Mar 20, 2010
    Messages:
    461
    Likes Received:
    324
    Ah thanks your a life saver :D

     
  4. bannedfree

    bannedfree Regular Member

    Joined:
    Jan 31, 2010
    Messages:
    444
    Likes Received:
    43
    Occupation:
    World Wide Web Worker
    Location:
    near an active volcano
    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";
    }
     
    • Thanks Thanks x 1
  5. Sprouts

    Sprouts Regular Member

    Joined:
    Mar 20, 2010
    Messages:
    461
    Likes Received:
    324
    Oh right thanks.

     
  6. Entrepreneur

    Entrepreneur Regular Member

    Joined:
    Oct 12, 2007
    Messages:
    438
    Likes Received:
    379
    I actually consider it to be wrote wrong in your example and correct in his, but each to their own I suppose. :)
     
  7. smooshie

    smooshie Newbie

    Joined:
    Dec 27, 2009
    Messages:
    18
    Likes Received:
    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
     
  8. deziner

    deziner Newbie

    Joined:
    Oct 22, 2010
    Messages:
    25
    Likes Received:
    15
    also use file_get_contents()
     
  9. risefromdeath

    risefromdeath Power Member

    Joined:
    Jul 1, 2009
    Messages:
    650
    Likes Received:
    107
    use strcmp at better alternative for strings :) or strcasecmp