RE: Trying to create a comment function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Allen McCabe [mailto:allenmccabe@xxxxxxxxx]
> Sent: 06 August 2009 20:20

[....]
 
> 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(" ").

You've already been correctly pointed at single quotes, but the other answer to this is "No", because parsing of escape sequences and interpolation of variables are done before the resultant string is even passed to the function (in the case of escape sequences, *long* before -- before your script even starts executing!).

[....]

> 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 won't work for the reason noted above. The \ character is never stored in your string -- it's there just to tell the PHP parser to treat the following $ as a literal $, not the start of a variable name. It's important to understand that escape sequences in general store a character in the string that is not what you see in the script -- so \$ stores just the $, \t stores a tab character, and so on. (And, of course, \\ stores a single \.)

Hope this helps you understand why what's happening is happening ;)


Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.ford@xxxxxxxxxxxxxx 
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux