If + echo issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yoda69
Forum Newbie
Posts: 16
Joined: Wed Jun 20, 2007 10:21 am

If + echo issue

Post by yoda69 »

hi,

can someone please help me with this "if" statement. somewhere i'm making a mistake:

Code: Select all

<?php

$haypicture="<td width="68%"><img src=\"/GY/images/masters/<?php echo \"$picture\";?>" alt="" name="teacher_image" border="0" id="teacher_image\"/></td>";

if ($rows['picture_id'] == "")
 {
   echo "
   <td width="68%">no picture available</td>";
  }else{
   echo "$haypicture";
   }
    ?>
Thanks guys,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Extra escaping is required of your double quotes. Note the highlighted code in your post for notable locations. ;)
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

Code: Select all

<?php 

$haypicture="<td width=\"68%\">".
"<img src=\"/GY/images/masters/{$picture} alt=\"\" name=\"teacher_image\" border=\"0\" id=\"teacher_image\"/></td>"; 


if ($rows['picture_id'] == "") 
 { 
   echo "<td width=\"68%\">no picture available</td>"; 
  }else{ 
   echo "{$haypicture}"; 
   } 
?>
I guess this will work fine;

when ever you want to embed a variable inside echo use flower braces;
ex:

Code: Select all

$name="Adam";
echo "Name: {$name}";
will print

Name: Adam
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$haypicture = '<td width="68%"><img src="/GY/images/masters/' . $picture .'" alt="" name="teacher_image" border="0" id="teacher_image" /></td>';

if ($rows['picture_id'] == "")
{
   echo '
   <td width="68%">no picture available</td>';
} else {
   echo $haypicture;
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This might actually be better:

Code: Select all

<?php
echo '<td width="68%">';
if ($rows['picture_id'] == '') {
  echo 'No picture available';
} else {
  echo '<img src="/GY/images/masters/' . $picture .'" alt="" name="teacher_image" border="0" id="teacher_image" />';
}
echo '</td>';
?>
Post Reply