What are code tags?
Code tags, also known as code bbcode or code vBcode, are wrapped around programming code in the editor window when typing out a post. They tell DaniWeb to output the code all nicely formatted, in a fixed-width (monospace) font, while preserving whitespace, so that it looks just like it would in your favorite code editor or IDE.
Another advantage to using code tags is the Toggle Plain Text link, which puts the code into a textbox where it can easily be copied and pasted. This is very useful to quickly test out programming code that has been posted on DaniWeb while trying to debug errors.
Using [code] [/code] bbcode
The simplest way to use code tags is to encase your code within [code] and [/code]. When using this method, you can optionally use [color] bbcode within your [code] to point out a particular word or line.
echo "Hello World!";
$foo = "Foo";
$bar = "Bar";
if ($foo == $bar)
{
	$baz = $bar;
	$bat = $foo;
}
else
{
	echo "DaniWeb";
}
Using [code=syntax] [/code] bbcode
DaniWeb supports syntax highlighting for a limited number of programming languages. You can specify which language to use via a syntax keyword. Any syntax keyword that is used within our code snippet library is also available for bbcode. Encase your code within [code=syntax] and [/code]. Nesting bbcode, such as [color] within [code], when a syntax language is specified does not work because this method automatically applies styling, such as color, to your code. Additionally, this method automatically adds line numbers so that a particular line of your code can easily be referenced. Therefore, there shouldn't be a need to manually color something to point it out.
  1. echo "Hello World!";
  2. $foo = "Foo";
  3. $bar = "Bar";
  4. if ($foo == $bar)
  5. {
  6. $baz = $bar;
  7. $bat = $foo;
  8. }
  9. else
  10. {
  11. echo "DaniWeb";
  12. }
Using [icode] [/icode] bbcode
Sometimes it's necessary to just mention a single code expression or line within text without it occupying a full code block. By encasing a small bit of code within [icode] and [/icode], you can display it inline such as echo "Hello World!"; . You can nest other bbcode, such as [color], within your inline code.
What if I don't use code tags?
Three things will happen if you don't use code tags when you're supposed to. Firstly, not wrapping your code within [code] tags violates our rules, so you may get in trouble with a forum moderator. Secondly, instead of looking like the above examples, the same block of code will look like it does below. In other words, it won't be in a fixed-width font and whitespace (hard returns and tabs) won't be preserved. Thirdly, because your code will be virtually uncomprehensible, no one will bother reading your post. A lot of forum members have the attitude that they're not going to take their time to help you if you're not going to take the time to correctly formulate your question. That's a lot of bad stuff for not taking an extra second to type 12 characters.
echo "Hello World!"; $foo = "Foo"; $bar = "Bar"; if ($foo == $bar) { $baz = $bar; $bat = $foo; } else { echo "DaniWeb"; }