I was trying some new things with php today, and was commenting above each
little bit to better see what was working and/or displaying versus what was
not. My comment delineator consisted of the following:

[code]

echo '<br><br><font color="#bbbbbb"><i> this is a comment displayed in html
</i></font><br><br>';

[/code]

Then I decided to create a cool function to write that code for me every
time I wanted to write a comment formatted in HTML, so to write a comment
saying "this is a comment", I would call a function:

[code]

comment("this is a comment");

[/code]

It was working wonderfully, until I wanted to display "test of $newComment"
as a comment.

[code]

comment("test of $newComment");

[/code]

This rendered a comment that said "test of ".

So I added a \ before the $ to make it display properly, but I was wondering
if there was a way that the function could work so that it will display
anything you type within the quotes in comment(" ").

Here is my original code for the function comment() :

[code=function comment()]

function comment($commentText = "empty comment")
{
echo '<br><br><font color=#bbbbbb>Comment:</font><br>';
 echo '<font color=#bbbbbb><i>'. $newComment .'</i></font><br><br>';
}

[/code]

This would return gray text in 2 lines, Comment: then a line return with the
comment the developer put within the comment() function call in italics.

After noticing that I MUST escape the dollar sign for it to display a
function name in a comment, I tried the following:

[code]

function comment($commentText = "empty comment")
{

 $healthy = "\$";
 $yummy   = "$";
 $newComment = str_replace($healthy, $yummy, $commentText);
 echo '<br><br><font color=#bbbbbb>Comment:</font><br>';
 echo '<font color=#bbbbbb><i>'. $newComment .'</i></font><br><br>';

}

[/code]

This still does not produce the desired results, I still have to escape the
$ when I call the comment() function for the variable name to display.
Again, not a big deal, but I don't want this to beat me.

Anyone have any ideas?


Additionally, when I try to echo $newComment, nothing shows on the screen.
Is this because variables are reset or set to null, or cleared at the end of
a function in which they are used?

Reply via email to