0

I'm thinking it's just a PHP code error, but for some reason the following code won't work, it just keeps giving me Internal Server Error 500

<?php
  $ajfl_adLuck = rand(1,3);
  if ($ajfl_adLuck == 2) {
    echo "<div id=\"sidebar-awesome-container\">";
    echo "<div id=\"sidebar-awesome-wrapper\">";
    echo "<img src=\"" . bloginfo('template_url'); . "/img/sample-ad-side.png\" alt=\"Sample Advertisement\">";
    echo "</div>";
    echo "</div>";
  } else {
    echo "";
  }
?>

Basically, my site is slightly different in that the sidebar is inside the WordPress Loop, because rather than a list of categories and metadata stuff it displays the post's information. I want an ad displayed down the sidebar, but not on every post, only on, let's say, a third of them.

I was wondering why my current code won't work. Quite new to PHP so I'm guessing it's a syntax error, but I've been working on it fot over an hour and still can't find the fix.

Any help is appreciated, whether it's a fix or a better way to do this.
Thanks.

  • 2
    A 500 error is not a PHP error, it's your server realising something went wrong and you made no attempt to report what it was, it's like saying all car related difficulties are a 'traffic jam', your engine exploded? Traffic jam, crumbs on the back seat? traffic jam! Go look at your error logs or turn on error logging then come back – Tom J Nowell Feb 16 '13 at 23:42
0
echo "<img src=\"" . bloginfo('template_url'); . "/img/sample-ad-side.png\" alt=\"Sample Advertisement\">";

You added a semicolon, what comes after that semicolon is a new statement. . "img/ etc is not valid PHP, so it errors out.

Also bloginfo does not return its value, it prints it, so you need to change that to get_bloginfo.

I would suggest you look up basic PHP error handling so that you can look at the error logs. I also recommend you get a proper code editor that will highlight syntax errors in bright red. Sublimetext/Komodo/PHPStorm/Netbeans/etc will all do this. Syntax errors are a solved problem, you shouldn't have to look for them, they should be pointed out to you by your code editor

  • 1
    I have Sublime Text and the syntax highlighting was perfect, maybe a bug or something. Thanks for the answer though, it worked! – Elliot Ellis Feb 16 '13 at 23:46
  • <3 sublime, good choice, there's a PHP syntax checker extension, and if you're feeling particularly advanced, the PHPCS package will check the quality and complexity of what you write, though that may be jumping ahead too far – Tom J Nowell Feb 16 '13 at 23:49
  • Wow! Thanks for noting the Syntax Checker, just been playing round with it and it's great! – Elliot Ellis Feb 16 '13 at 23:55
0

Not really a Wordpress question, but I think here is the sintax error

  echo "<img src=\"" . bloginfo('template_url'); . "/img/sample-ad-side.png\" alt=\"Sample Advertisement\">";

to

 echo "<img src=\"" . bloginfo('template_url') . "/img/sample-ad-side.png\" alt=\"Sample Advertisement\">";

No semicolon if you concatenate a string. If I may dare, it is good practice to save your string into a variable and then echo the variable itself; for best readability use concatenation in your variable through the use of .=.

  • Worked perfectly. Still quite new to PHP, so thanks a lot. – Elliot Ellis Feb 16 '13 at 23:47
0

see answer by @Sunyatasattva;

also, in string concatenation, you need to use get_bloginfo()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.