Trying to create a comment function
Trying to create a comment function
am 06.08.2009 21:20:11 von Allen McCabe
--0016367f96f026706204707e0211
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
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 '
this is a comment displayed in html
';
[/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 '
Comment:
';
echo ''. $newComment .'
';
}
[/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 '
Comment:
';
echo ''. $newComment .'
';
}
[/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?
--0016367f96f026706204707e0211--
RE: Trying to create a comment function
am 06.08.2009 21:26:53 von Asher Snyder
Allen,
Local variables are indeed cleared at the end of a function, see
http://www.php.net/manual/en/language.variables.scope.php for more
information.
Furthermore, there are numerous ways to use variables within a string, the
simplest method is the one that you described by enclosing your variable
within your double quoted string. However, you could also use brackets
within your string in addition to concatting your string with your variables
such as:
echo "this is some string" . $variable;
I hope this helps. However before asking these sorts of questions of the
list, you might want to refresh your memory using the PHP Language
Reference, http://www.php.net/manual/en/langref.php. All the information
listed above, is also listed there, and explained in significantly more
detail.
-----Original Message-----
From: Allen McCabe [mailto:allenmccabe@gmail.com]
Sent: Thursday, August 06, 2009 3:20 PM
To: phpList
Cc: ollisso; Martin Scotta
Subject: [PHP] Trying to create a comment function
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 '
this is a comment displayed in html
';
[/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 '
Comment:
';
echo ''. $newComment .'
';
}
[/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 '
Comment:
';
echo ''. $newComment .'
';
}
[/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?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Trying to create a comment function
am 06.08.2009 22:01:57 von Jonathan Tapicer
>
> [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(" ").
>
If you want the string to be rendered as it is without variable
replacements you can use single quotes, like this:
comment('test of $newComment');
That will render exactly this:
test of $newComment
Hope that helps you.
Jonathan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Trying to create a comment function
am 07.08.2009 12:18:31 von M.Ford
> -----Original Message-----
> From: Allen McCabe [mailto:allenmccabe@gmail.com]
> Sent: 06 August 2009 20:20
[....]
=20
> It was working wonderfully, until I wanted to display "test of
> $newComment"
> as a comment.
>=20
> [code]
>=20
> comment("test of $newComment");
>=20
> [/code]
>=20
> This rendered a comment that said "test of ".
>=20
> 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 answe=
r to this is "No", because parsing of escape sequences and interpolation of=
variables are done before the resultant string is even passed to the funct=
ion (in the case of escape sequences, *long* before -- before your script e=
ven 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:
>=20
> [code]
>=20
> function comment($commentText =3D "empty comment")
> {
>=20
> $healthy =3D "\$";
> $yummy =3D "$";
> $newComment =3D str_replace($healthy, $yummy, $commentText);
> echo '
Comment:
';
> echo ''. $newComment .'
';
>=20
> }
>=20
> [/code]
>=20
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 foll=
owing $ 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 strin=
g that is not what you see in the script -- so \$ stores just the $, \t sto=
res 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
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
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