hieimora
hieimora

Reputation: 313

Changing to Comparison Operator

I was told that the previous code wasn't using parameters even when employing a library that supports it. And that I was using an assignment operator rather than a comparison. Also the true branch is redudant.

Previous Code:

if ($profilepic = $row['profilepic']){
    $profilepic = $row['profilepic'];
}else{
    $profilepic = "DamjuNoImage";
}

Current Code:

$profilepic = $row['profilepic'];
$noimage = "DamjuNoImage";

if ($profilepic != $row['profilepic']){
        echo $noimage;
    }

I wanted to double check to see if I fixed the code even though I already tested it.

Upvotes: 0

Views: 38

Answers (1)

Sampson
Sampson

Reputation: 268344

With the following:

$profilepic = $row['profilepic'];
if ($profilepic != $row['profilepic']){
    echo $noimage;
}

Your if-statement will never do anything. The first line makes $profilepic equal to $row['profilepic'] (whether that's an empty string, null, or what-have-you), and the condition checks if they are not equal - which will never be the case since you just finished making them equal.

If you want to provide a default image, do something like this:

if ( empty( $row["profilepic"] ) ) {
    $profilepic = $row["profilepic"];
} else {
    $profilepic = "DamJuNoImage";
}

Or written more concisely:

// Assign value of column if not empty, otherwise "DamJuNoImage"
$profilepic = empty( $row["profilepic"] ) 
    ? "DamJuNoImage" 
    : $row["profilepic"];

If the profile pic column is empty, we assign the a default value to $profilepic. From there we can just echo our image.

echo "<img src='{$profilepic}' />";

Demo: http://codepad.org/KXuQyVma

Upvotes: 1

Related Questions