Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15283

Re: Is there a better way?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Is there a better way?
Date Wed, 22 Apr 2015 17:17:09 +0200
Organization PointedEars Software (PES)
Lines 185
Message-ID <3306538.k7Mm7ejDUP@PointedEars.de> (permalink)
References <767d57a3-1f14-4714-a063-53c53973ecbf@googlegroups.com> <czlr2z0318lc.1qplhidk22fv7.dlg@40tude.net> <w8zXw.232826$lO6.143872@fx24.iad>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1429715974 7618 eJwNyckBwDAIA7CZIJhjnILD/iOk+grHxSfM4YbFiknnIevqNzt/MZulGuduxmainRKlBQX4ACj/ESI= (22 Apr 2015 15:19:34 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Wed, 22 Apr 2015 15:19:34 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwNysEBwCAIA8CVDJJQ1yGV/Udo733cglwpKjkcadNYvTIcaacc6tvwETxg9f/dry5LccCFPLGfKWQRHz6uFGM=
Cancel-Lock sha1:s3vKJ2G9LUFGmLx5mY6FZHqMHlQ=
X-NNTP-Posting-Host eJwNy8EBwCAIA8CVREgo41QJ+4/Q3v/gNN4MgoHBdNk576ookAbTmmg9roYb0/OW7/wLpcnYHxBNEIE=
Xref csiph.com comp.lang.php:15283

Show key headers only | View raw


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.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is there a better way? Paul Knaggs <paul@tagmebands.co.uk> - 2015-04-13 19:47 -0700
  Re: Is there a better way? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-14 07:54 -0400
  Re: Is there a better way? Paul Knaggs <paul@tagmebands.co.uk> - 2015-04-15 04:40 -0700
    Re: Is there a better way? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-15 11:01 -0400
  Re: Is there a better way? richard <noreply@example.com> - 2015-04-15 15:04 -0400
    Re: Is there a better way? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-15 15:40 -0400
      Re: Is there a better way? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-15 17:14 -0400
      Re: Is there a better way? richard <noreply@example.com> - 2015-04-15 20:02 -0400
      Re: Is there a better way? Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-16 15:18 +0000
      Re: Is there a better way? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-22 17:17 +0200
    Re: Is there a better way? "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> - 2015-04-15 20:56 +0000
    Re: Is there a better way? Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-16 01:25 +0000
    Re: Is there a better way? Evan Platt <evan@theobvious.espphotography.com.invalid> - 2015-04-17 07:35 -0700
      Re: Is there a better way? richard <noreply@example.com> - 2015-04-17 11:52 -0400
        Re: Is there a better way? Evan Platt <evan@theobvious.espphotography.com.invalid> - 2015-04-17 10:04 -0700
          Re: Is there a better way? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-17 13:22 -0400
            Re: Is there a better way? richard <noreply@example.com> - 2015-04-17 14:49 -0400
              Re: Is there a better way? Evan Platt <evan@theobvious.espphotography.com.invalid> - 2015-04-17 12:20 -0700
              Re: Is there a better way? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-17 15:27 -0400
        Re: Is there a better way? Evan Platt <evan@theobvious.espphotography.com.invalid> - 2015-04-17 10:30 -0700
          Re: Is there a better way? richard <noreply@example.com> - 2015-04-17 14:56 -0400
            Re: Is there a better way? Evan Platt <evan@theobvious.espphotography.com.invalid> - 2015-04-17 12:25 -0700
              Re: Is there a better way? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-17 15:29 -0400
              Re: Is there a better way? Tim Streater <timstreater@greenbee.net> - 2015-04-17 22:19 +0100
      Re: Is there a better way? Richard Yates <richard@yatesguitar.com> - 2015-05-24 15:22 -0700

csiph-web