I tested both scripts on my installation and they both ran without error and generated the expected output. The problem is probably where you inserted the code and/or how you’re using it.
You need to be careful where you insert code, you cannot nest <?php ?>
blocks. If you insert code into another <?php ?>
block, you need to remove the extra <?php
and ?>
tags. When inserting <?php ?>
blocks into HTML portions, you need to ensure the output does not disrupt adjacent HTML and that the final output is valid HTML. Or there just may be an extra or missing character, such as angle bracket, quote, parenthesis, semi-colon, or other similar delimiters.
This sort of code is usually inserted in the theme’s sidebar.php file, though it can go in any template file. It can also be used as a part of a plugin, but then some extra coding is required in order to inject the output where you want it to appear.