FORUMS: list search recent posts

PHP picture inside if statement

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Robert Teixiera
PHP picture inside if statement
on Jul 26, 2012 at 4:03:40 am

Stupid PHP!
Ok totally unfair, complete newbe here.
I thought it would be easy to start with a simple if statement. The idea is to rotate a website opening banner depending on the month, but I can't even get it to display a simple JPG.

<?php
$d=date("F");

if ($d=="July")
{

<a href="#"><img src="Almost Time.jpg" alt="" width="180" height="180" />
}


I took a working if statement, and inserted a working picture, but it give me:
syntax error, unexpected '<'

I am trying to stay away from PHP specific ways to show a JPG if possible, as I like the insert picture as .PSD dreamweaver does.

As an interesting side note, if I cut the link out and use an include containing this exact code, it works perfectly. (guess I could have 4 or 5 different include pages, but I'm trying understand what is happening.)
Thanks


Return to posts index

Curtis Thompson
Re: PHP picture inside if statement
on Jul 26, 2012 at 6:37:56 pm

hello...

ahh - ya. you need to either put your html output in a php echo statement, or you need to close and re-open your php block - so either:


<?php
$d=date("F");
if ($d=="July") {
?>
<a href="#"><img src="Almost Time.jpg" alt="" width="180" height="180" />
<?php
}
?>


or:

<?php
$d=date("F");
if ($d=="July") {
echo "<a href=\"#\"><img src=\"Almost Time.jpg\" alt=\"\" width=\"180\" height=\"180\" />";
}
?>


note on the second one that you have to escape the quotes inside the echo statement so that you avoid a parse error - a simpler example:

echo "this is in \"quotes\" here";

an alternative is to use single quotes for the echo, but you can run into issues there too...as for closing php blocks and using standard html vs doing a php echo, that is personal preference...either one is "correct" technically...

hope that helps!

sitruc


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]