i think the problem is (and i may be wrong) that you're confusing server-side and client-side scripting. PHP is a server side language - all the processing is done at the server. Hence, if you go to a php page, it doesnt look at all different from a bog standard HTML page. That's because everything that you see as a browser is the HTML that the server sends you after it's finished doing its stuff to generate the HTML (be that a bit of code, if statements, checking a database, whatever).
Therefore, when it sends you the page, that's it - the PHP side of things is complete until you send another page request to the server (like filling in a form and clicking submit or something).
Java script is a client-side script, which means that it is executed on your computer (as a client). You can tell because if you look at the source, you can see the JS code - it's not been executed at the server, it's been sent to you for you to execture it. So it will look for things like "have you moved the mouse over an image", "am i being clicked on", that sort of thing.
Your problem is that you're writing PHP, and therefore your script is being executed on the server, and then you're being thrown some HTML - so if probably does run your if statement, but it's not true when the page is executed (since the image isnt = sflower.jpg or whatever), and so you dont get your href. You cant write php so that if something happens on your computer (like a house over) it suddenly inserts more html into the document that you've already been sent - it would have to use a refresh for that.
You need to investigate javascript more - but i'm worse at that than i am at php i'm afraid!