htmlentities et al.: relationship between quote_style and charset parameters (was: character switch) – PHP

  php

Q(Question):

(Crossposted and followups set. Hope you don’t mind Markus.)

The Manual expresses the parameters of htmlentities as:

string string [, int quote_style [, string charset]]

http://www.php.net/manual/en/function.htmlentities.php

The meaning is unambiguous: the first parameter, "string", must be
present; the second parameter, "quote_style", is optional; and the
final parameter, "charset", is optional too, but its presence
requires the, prima facie, ought-to-be dispensable second
parameter.

Why does the existence of the charset parameter depend on the
existence of the quote_style parameter? What’s the connection?


Jock

A(Answer):

"John Dunlop" <jo*********@johndunlop.info> wrote in message
news:MP************************@news.freeserve.net …

(Crossposted and followups set. Hope you don’t mind Markus.)

The Manual expresses the parameters of htmlentities as:

string string [, int quote_style [, string charset]]

http://www.php.net/manual/en/function.htmlentities.php

The meaning is unambiguous: the first parameter, "string", must be
present; the second parameter, "quote_style", is optional; and the
final parameter, "charset", is optional too, but its presence
requires the, prima facie, ought-to-be dispensable second
parameter.

Why does the existence of the charset parameter depend on the
existence of the quote_style parameter? What’s the connection?

You can’t pass an optional 3rd parameter if there isn’t a second parameter,
otherwise it would be the second parameter. 🙂

Paulus

A(Answer):

"John Dunlop" <jo*********@johndunlop.info> schrieb im Newsbeitrag
news:MP************************@news.freeserve.net …

(Crossposted and followups set. Hope you don’t mind Markus.)

No problem; I am not really a usenet specialist…
The Manual expresses the parameters of htmlentities as:

string string [, int quote_style [, string charset]]

http://www.php.net/manual/en/function.htmlentities.php
That’s interesting; I had not seen this before.
The meaning is unambiguous: the first parameter, "string", must be
present; the second parameter, "quote_style", is optional; and the
final parameter, "charset", is optional too, but its presence
requires the, prima facie, ought-to-be dispensable second
parameter.

Why does the existence of the charset parameter depend on the
existence of the quote_style parameter? What’s the connection?

The quote_style parameter only defines which quotes to replace (only double
quotes, both single and double, or none of them), there is no way to define
what they are going to be replaced by. Double quotes are replaced by &quot;
and single ones by '

I tried this with Latin-1 and Chinese Big 5 encodings, there was no
difference.

So I think it is just the name of the parameter "quote_style" which is
confusing, as you don’t set the style. You can’t set them to &rsquo; or
&raquo; or ASCII something, or whatever. So it seems to be independent from
the encoding but rather just provide a possibility for coders to handle
quotes in connection with their coding style, as in some cases you might
want to keep a " as it is and not replace it by &quot;


Markus

A(Answer):

Markus Ernst wrote:

So I think it is just the name of the parameter "quote_style" which is
confusing, as you don’t set the style.

I hadn’t actually considered that. But, with further pondering, I
believe the parameter’s name could’ve conceivably been better
chosen. Dare I say a misnomer even?
So [quote_style] seems to be independent from the encoding […]

Yes, that’s the crux of the matter. The conversion of quotation
marks *is* independent of the character set.

Why must I explicitly set the quote_style value — even if that
value is equivalent to its default — whenever I wish to use a
different character set? Why is quote_style not optional whenever
I change character sets?

IOW, why is the parameters’ notation not:

string string [, int quote_style] [, string charset]


Jock

A(Answer):

On Thu, 9 Oct 2003 11:28:01 +0100, John Dunlop wrote:

IOW, why is the parameters’ notation not:

string string [, int quote_style] [, string charset]

Not possible in PHP. That would require something like a strongly
typed language (e.g. Java or C++) and overloading. This is not really
what PHP is.

A(Answer):

Gerhard Fiedler wrote:

string string [, int quote_style] [, string charset]

Not possible in PHP. That would require something like a strongly
typed language (e.g. Java or C++) and overloading. This is not really
what PHP is.

Right. A slip-up on my part then. Thank you.

After further reading, I notice that The Manual’s notes about "How
to read a function definition (prototype)" says next to nothing
about optional parameters. Someone without much knowledge, reading
that page — me to a tee! — might benefit from supplementary
documentation.
http://www.php.net/manual/en/about.prototypes.php

So, RTFM was unsuccessful. And so was googling, which brought up
page upon page of clues about optional parameters and variable-
length parameter lists in *user-defined* functions. The Manual
already explains those in enough detail IMO. Ho hum.
http://www.php.net/manual/en/functions.arguments.php

Thanks again, Gerhard.


Jock

A(Answer):

On Thu, 9 Oct 2003 17:35:24 +0100, John Dunlop wrote:

>string string [, int quote_style] [, string charset]

Not possible in PHP. That would require something like a strongly
typed language (e.g. Java or C++) and overloading. This is not really
what PHP is.

Right. A slip-up on my part then. Thank you.

No problem.

I actually didn’t include everything above. What I sometimes do with
user-defined functions in such cases (but that’s not what the PHP
internal functions do) is to pass in an associative array. This has
the same effect as named arguments, and then the order is not relevant
anymore, and whan needs to be supplied is no longer restricted by the
language syntax, but only by the program logic.

LEAVE A COMMENT