0

I have this code and there is a bug in it but I cannot see where I have gone wrong. Can someone please help? The problem is that I am trying to display a certain image to corrospond with the content of text files. I think i have that part sorted but when it comes to displaying the images there is always a bug (E.G it is always green even when the if statment says otherwize.Here is the code:

<?php
            if (empty($_GET['unit'])) {
            $output="Please Enter A Unit Number";
            echo $output;
            }
            else {
                $filepathhalf = "/data/";
                $file = "false";
                $date = date("Ymd");
                $unitnum = $_GET['unit'];
                $ext = ".txt";
                $filepath = $filepathhalf.$unitnum.$date.$ext;
                echo $filepath;
                echo $file;
                if(file_exists($filepath))
                {
                    $fh = fopen($filepath, 'r');
                    $file = fread($fh, 5);
                    fclose($fh);
                }
                echo $file; //This echo comes back as false as set but the green.png image still displays.
                if ($file = "true ")
                {
                    echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
                }                   
                else 
                {
                    echo "<img src=\"images/red.png\" width=\"15\" height=\"15\" />";
                }
                echo $_GET['unit']; 
            }
            ?>  
2
  • Thankyou for pointing everyone pointing that out :) ill accept the 1st answer just cause I can only accpept one.. Commented Jul 13, 2012 at 5:27
  • 1
    While you shouldn't be mixing concerns in a single file, regardless, you may find that dropping in and out of PHP for HTML output advantageous: if($file): ?> <img src="<?= $file; ?>" /> <? endif; Commented Jul 13, 2012 at 5:56

5 Answers 5

4

There is a difference between comparing two instances and assigning one to the other.

See the below lines from your snippet and see if you might spot the error with the above clue:

if ($file = "true ")
{
  echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}                   
else 
{
  echo "<img src=\"images/red.png\" width=\"15\" height=\"15\" />";
}

Otherwise hover with your mouse over the spoiler below!

If you want an explanation regarding the issue, do the same...

$file = "true " will always evaluate to true, first it will assign the string "true " to $file and then the value of $file will be evaluated.

You are most probably looking for if($file == true), which will compare the value of $file to true.

Sign up to request clarification or add additional context in comments.

1 Comment

@Nicholas no problem mate, glad to be of service!
3

You use a single =, which is used when assigning variables, not comparing them. When checking if two values are equal, use ==.

Comments

2
  if ($file == true)
  {
          echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
  }   

hope that helps

Comments

1

It should be == to check condition.

 if ($file != "false")
 {
   echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
 } 

Comments

1

not only you're using a single "=" but also you compare it to "true " (with a concatenated space!). I would change the code to:

if ($file === true)
{
   echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.