1

Consider the following php code used for HTML output:

        echo "<div id='logo2'>
                <a href='if(checkLanguage()) index.php; else index.php?lang=en;'>
                    <img src='img/logotext1.png' width='".$logoWidth2."'height='".$logoHeight2."' 
                    border='0';/>
                </a>
        </div>";

As you see the if and else conditions in the a tag aren't working properly because of some concatenation mistake. Any help making the hyperlinks work is greatly appreciated.

George
  • 2,050
  • 6
  • 26
  • 36
  • 2
    In addition, you should be using double quotes (") for HTML attributes, not apostrophes ('). – halfer Mar 23 '12 at 16:06

5 Answers5

4

Change

<a href='if(checkLanguage()) index.php; else index.php?lang=en;'>

By

<a href='".(checkLanguage() ? 'index.php' : 'index.php?lang=en').">

But, I will suggest this :

<div id="logo2">
    <a href="<?php echo (checkLanguage() ? 'index.php' : 'index.php?lang=en'); ?>">
    <img src="img/logotext1.png" width="<?php echo $logoWidth2; ?>" height="<?php echo $logoHeight2; ?>" border="0" />
    </a>
</div>

Also :

  • Use double quotes for HTML code and apostrophes for PHP.
  • Take apart the if else within a variable
David Bélanger
  • 7,400
  • 4
  • 37
  • 55
  • It should be noted that the shorthand variable print "=" has been on the chopping block several times, and may become deprecated in future versions of PHP. – Jordan Mack Mar 23 '12 at 16:25
  • There was nothing technically wrong with it before. It is still valid usage and has not been deprecated yet. I just wanted it noted, but using the full PHP tags is probably the safest bet for future compatibility. Here is a good post on the subject: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Jordan Mack Mar 23 '12 at 20:50
  • 1
    Yeah it is still valid but if we today ajust ourself to the future, it will be easier, isnt ? Good topic btw. – David Bélanger Mar 23 '12 at 20:55
2

That's not how PHP works...

<?php ?>
<div id="logo2">
   <a href="<?= checkLanguage() ? 'index.php' ? 'index.php?lang=en' ?>">
      <img src="img/logotext1.png" width="<?= $logoWidth2 ?>" height="<?= $logoHeight2" border="0" /></a>
</div>

Note that I'm using the shorthand <?= construct, which only works in short_tags is enabled in your PHP config. Otherwise, it'll have to be <?php echo.

As well, note that I'm spending the majority of this code in "html mode". When you're doing a major of NON-php code, it's best to just embed small <?php ?> blocks inside that non-php code, rather than having to mess with echoes, quote escapes, etc... Any decent editor worth is salt will highlight the PHP code for you anyways, so legibility is much improved and the PHP sections will stand out nicely as well.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

You haven't enclosed your PHP sections with <?php and ?>.

Edit; ah, this is one large echo statement. It would be better to make it all HTML and open out PHP tags when you need them. That is to say, the <div> and the <a> tags don't need to be echoed - just put them outside of PHP tags and open <?php for the if.

halfer
  • 19,824
  • 17
  • 99
  • 186
1

Would reccoment taking the conditional out to make your code cleaner:

$href = (checkLanguage() ? 'index.php' : 'index.php?lang=en');
echo '
<div id="logo2">
    <a href="'.$href.'">
        <img src="img/logotext1.png" width="'.$logoWidth2.'" height="'.$logoHeight2.'" border="0" />
    </a>
</div>';
Ingmar Boddington
  • 3,440
  • 19
  • 38
1

Since you are just starting out, I've written this block of code in the most verbose and easily understandable format. I have also removed your HTML formatting and replaced it with the preferred CSS formatting.

<?php
echo '<div id="logo2">';

if(checkLanguage())
{
    echo '<a href="index.php">';
}
else
{
    echo '<a href="index.php?lang=en">';
}
echo '<img src="img/logotext1.png" style="width: '.$logoWidth2.'px; height: '.$logoHeight2.'px; border: none;" />';
echo '</a>';

echo '</div>';
?>
Jordan Mack
  • 8,223
  • 7
  • 30
  • 29
  • @DavidBélanger You've missed the point entirely. This is purposely verbose because the original poster is a beginner. I would never recommend ternary syntax to someone who is struggling with a condition string print. – Jordan Mack Mar 23 '12 at 16:13
  • Sorry. When I wrote my comment, you haven't wrote comment in your comment. Just your code. Good point but never too late to learn the right way. – David Bélanger Mar 23 '12 at 16:17