Lew Pitcher wrote:
> […] "richard" […] wrote:
>> On Mon, 13 Apr 2015 19:47:36 -0700 (PDT), Paul Knaggs wrote:
>>> I was looking at using Angular to try and do this but was told by a
>>> developer my code was not very good is there a better way?
>>>
>>> The code is an extract from function.php used in a dynamic page
>>> "request.php?=123"
>> I just have to ask.
>> What exactly is =123?
>> the ? begins a variable list.
>
> In PHP terms, yes.
In PHP terms, _no_. How did you get that idea?
> As defined by the W3C, the '?' begins the "query" part of the URL.
The World Wide Web Consortium (W3C) does _not_ define Internet standards
such as URIs, and associated terminology; the Internet Engineering Task
Force (IETF) and the Internet Engineering Steering Group (IESG) do.
The W3C defines *Web* standards by publishing specifications they eventually
call Recommendations. Web standards make use of Internet standards because
the Web is an application of the Internet. One of several.
> The IETF, in RFC-1738, calls it the "searchpart".
RFC 1738 (one does _not_ write a hyphen-minus there; either one writes a
space between or omit the space), since it was published in 1994 (CE), has
been updated by RFCs 1808, 2368, 2396, 3986, 6196, 6270, and obsoleted by
RFCs 4248 and 4266.
<
http://tools.ietf.org/html/rfc3986>
The current Internet standard on Uniform Resource Identifiers (URIs) in
general is STD 66 (from RFC 3986) of 2005, which says:
,-<
http://tools.ietf.org/html/std66#section-3.4>
|
| The query component contains non-hierarchical data that, along with
| data in the path component (Section 3.3), serves to identify a
| resource within the scope of the URI's scheme and naming authority
| (if any). The query component is indicated by the first question
| mark ("?") character and terminated by a number sign ("#") character
| or by the end of the URI.
|
| query = *( pchar / "/" / "?" )
|
| The characters slash ("/") and question mark ("?") may represent data
| within the query component. Beware that some older, erroneous
| implementations may not handle such data correctly when it is used as
| the base URI for relative references (Section 5.1), apparently
| because they fail to distinguish query data from path data when
| looking for hierarchical separators. However, as query components
| are often used to carry identifying information in the form of
| "key=value" pairs and one frequently used value is a reference to
| another URI, it is sometimes better for usability to avoid percent-
| encoding those characters.
>> That is followed by a variable with an assigned value.
>> such as ?page=123
>
> Not quite.
>
> The <variable list> consists of one or more variables. Each variable /may/
> have an associated value, but it is not necessary.
If there ever has been a “<variable list>” (the “register_globals” setting
that is obsolete since PHP 5.3.0 and removed since PHP 5.4.0 would suggest
that there has been: the names did create global variables if this setting
was "on", with all side effects and security issues; however, even RFC 1738
does _not_ contain that term), there is not one anymore.
As you can see above, the format of the query component of a URI is –
intentionally, because of *general* applicability (hence URI and not just
URL) – rather arbitary; however, the historical practice of having a name
associated with a value, whereas those are delimited by “=”, and several
name-values are delimited by “&”, persists. Particularly with PHP where the
names make up /keys/ of /elements/ (_not_ “variables”) of /superglobal/
/arrays/, and the values make up either the associated values, or, if the
key ends with ”[]”, the values of the array that is the value associated
with the name, with ”[]” stripped for the corresponding array key.
> Obviously, the code needs to have foreknowledge of the variable name.
> However, it doesn't need to extract information from that variable; it's
> presence or absence in the URL can be detected and used as information all
> on it's own.
>
> Consider
Considered and rejected.
> if (exists($_GET('123'))
There is no built-in exists() function, and there is no need for a misnamed
user-defined one, because there are appropriate built-in functions.
PHP arrays are subscripted with rectangular brackets (“[…]”), not
parentheses (“(…)”).
> {
> // We can do useful work, knowing that the query asked for 123
>
> echo "Found the variable"
> if (is_set($_GET('123'))
isset(), not is_set(). As a rule of thumb, only identifiers of type-
detection functions start with “is_” in PHP – like is_null(), is_string(),
and is_array(). Therefore, is_set() could be only a future function for
determining if a value was of the “set” type. As such, it should _not_ be
used as the identifier for a user-defined function (at least not without
function_exists() guard).
<
http://php.net/manual/en/ref.var.php>
<
http://php.net/function_exists>
However, isset() returns TRUE for references whose referred value is not
NULL. And a "variable" that is "not set" in a query component (i.e., the
superglobal $_GET array’s element $_GET[$name], where $name equals the
"variable" name) has the value "" of type string, not NULL of type NULL.
(*All* values are string values there.)
> echo "The variable is set to $_GET('123')"
> else
> echo "The variable is not set"
> }
> else echo "Did not find the variable"
That is not even syntactically valid PHP code, and there is no need to post
pseudo-code here. The actual (recommended) PHP syntax and approach for this
example is straightforward (although the output still uses wrong
terminology):
if (array_key_exists('123', $_GET))
{
/* We can do useful work, knowing that the query asked for 123 */
echo "Found the variable";
if ($_GET['123'] !== '')
{
echo "The variable is set to {$_GET['123']}";
}
else
{
echo "The variable is not set";
}
}
else
{
echo "Did not find the variable";
}
<
http://php.net/array_key_exists>
It should be noted that isset($a[$b]) never generates a warning if $a is not
defined, while array_key_exists($b, $a) does. But array_key_exists() is
also more appropriate here because $_GET always exists, unless you set the
“variables_order” setting differently (the default and recommended
development and production values all include "G").
<
http://php.net/manual/en/ini.core.php#ini.variables-order>
> The second query ("?123") should print out
> "Found the variable"
> and
> "The variable is not set"
It should be noted that, if your code would resemble PHP code, in the way I
suggested above, it would print out “Found the variableThe variable is not
set” because you forgot the trailing "\n" (or something to that effect)
*again*.
I suggest you either refrain from posting or prepare your postings more
carefully; in particular, if you post, *verify* the information and *test*
the code before posting it. When PHP newbies come here, and especially
“richard”, they are confused already; there is no need to confuse them
further, or confirm their misconceptions.
--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.