Q:

How to add a else command in php?

I have an attribute called "Merchant_Name".
The following code will place the "Merchant_Name" Just below the product name in product page.
Now I need to add an else command to this code like this:
If Merchant_Name is there then it should be "By Merchant_Name" else "XXXXX".
Hope you get me...
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>By <?php echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); } ?>
</div>

Thanks in Advance...

A:

You mean, with an if/else?
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');

if ($merchant_name) { 
    echo "By " . $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); 
} else {
    echo "By XXXXX";
}
?>

Or maybe this would be cleaner:
<?php
if($_product->getAttributeText('merchant_name')) {
    $name = $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
    $name = "XXXXX";
}
?>
<div class="Merchant_Name">By <?php echo $name ?>

A:

It is pretty simple and it would be simpler if you didn't switch in and out of PHP so often:
<div class="Merchant_Name">
By 
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){
    echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); 
} else {
    echo 'XXXXX';
} 
?>
</div>

