0

Here my if condition is showing "new" as output why? I expect "old":

$d = date("Y"); 
$string = substr($v, 0,  4) . '<br>';  //here string stores 2015

if($string != $d) {
    echo "new";         
} else {
    echo 'old';         
}
2
  • 1
    What is the output of var_dump($string); ? Commented Apr 4, 2015 at 17:30
  • 1
    I doubt that var_dump($string); only shows you 2015. So what is the real output? (From a-z, most times it's something like: string(4) "2015" or so) Commented Apr 4, 2015 at 17:36

1 Answer 1

3

Why would 2015 be the same as 2015<br> ?

As noted by Gordon, you're comparing two strings, so it's really like this

if ( "2015" == "2015<br>" ) // false

Stop adding the <br> and it probably works

$d = date("Y"); 
$string = substr($v, 0,  4);

if($string != $d) {
    echo "new";         
} else {
    echo 'old';         
}
Sign up to request clarification or add additional context in comments.

5 Comments

@adeneo You may want to give the AH!!! effect to OP by letting him writing: var_dump($string); and then let him look at the output, so he sees it with his own eyes :)
@Rizier123 - but var_dump in a HTLM document will parse the break as HTML, but even if you can't see it, doesn't mean it's not present in the string
@adeneo Well he won't see the break line directly unless he looks at the source code, but he still can see it: var_dump("2015<br>"); -> string(8) "2015 " I think then he should see that 8 can't match with only 2015
@Rizier123 - Yes, that's what I was thinking, and as Gordon is saying, that would be true as both are strings, I was just stumped at how PHP does type conversion.
Yes, @adeneo you are right.Code considered <br> too. Thank you for valuable suggestion.

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.