Link to home
Create AccountLog in
Avatar of intangiblemedia
intangiblemediaFlag for Afghanistan

asked on

PHP If Else Echo HTML not working

Hi,

I need to get this working - its the echo of html that isnt working for me. I dont know if I need quotes, backslashes or what?? Thanks!
<?php
$store = Mage::app()->getStore();
$name = $store->getStoreId();
if ( $name == 3 ) {
 echo '<div class="header" style="background:url('http://www.e-cocinasplus.com/skin/frontend/default/caramel-red/images/header-logo-ecplus-english.gif') no-repeat; white">';
}
else {
  echo '<div class="header" style="background:url('http://www.e-cocinasplus.com/skin/frontend/default/caramel-red/images/header-logo-ecplus-spanish.gif') no-repeat; white">';
}
?>

The code above doesnt work -

The code below does:

<?php
$store = Mage::app()->getStore();
$name = $store->getStoreId();
if ( $name == 3 ) {
 echo '<div class="header">';
}
else {
  echo '<div class="header">';
}
?>

Thank you for your help!
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You need to escape your quotes

Wrong

echo "This is a "test"";

Correct

echo "This is a \"test\"";
OR
echo 'This is a "test"';

Wrong

echo 'This is a 'test'';

Correct
echo 'This is a \'test\'';
OR
echo "This is a 'test'";

When to use " and when to use '

Double quotes tells php to evaluate vars in the string so

$test = "good test";
echo "This is a $test";

Gives
This is a good test

echo 'This is a $test';
Gives
This is a $test

heredoc also allows vars in the string.

Also consider printf and sprintf

printf("This is a %s", $test);
$newstring = sprintf("This is a %s", $test);

Mix and match to suite your requirements
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Good point, Chris! Elegant and concise.
Avatar of intangiblemedia

ASKER

Many thanks guys, sorry for the delay in coming back to you!
For other readers - to clarify.

The problem the author was experiencing was because quotes in his string where not escaped.

The other posts highlight alternative solutions to performing the output but in terms of the question asked the answer is - quotes were not escaped and that is why the echo's were failing - refer my post on this