Doc #62997 [Opn]: Section on variables: explain $1 is always literal
| From: | php at richardneill dot org | Date: | Mon, 03 Sep 2012 08:53:37 +0000 |
| Subject: | Doc #62997 [Opn]: Section on variables: explain $1 is always literal | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8805@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62997&edit=1
ID: 62997
User updated by: php at richardneill dot org
Reported by: php at richardneill dot org
Summary: Section on variables: explain $1 is always literal
Status: Open
Type: Documentation Problem
Package: Documentation problem
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I see your point entirely, and why you wouldn't want to encourage beginners into using that
syntax. But especially for the cases like pg_query_params, I think it's worth mentioning.
In particular, is "$1" intentionally literal, or is it merely an artefact of the parser
that doesn't (yet) throw an error? $1, $2 etc do have meaning in bash/perl, so I think a
definition is worth having. Perhaps the docs for pg_query_params() should be the place to state it?
[Incidentally, on a similar note, how does one embed a raw '$1' into pg_query_params,
without it being interpreted as a placeholder?]
Previous Comments:
------------------------------------------------------------------------
[2012-09-03 02:57:43] aharvey@php.net
It's deliberate in the sense that it's not a valid variable name (it's kind of hinted
at in the String Parsing section, if not explicitly stated), but I'm not sure that it's a
code pattern that we want to be pushing, really: it's probably simpler for new developers to
just understand that $ is magic in double quoted strings and push them towards single quoted strings
in this case, and let them work out the corner cases later if need be.
Does anyone else on the doc team have any thoughts? Not sure whether to Won't Fix this or not.
------------------------------------------------------------------------
[2012-09-02 14:10:57] php at richardneill dot org
Description:
------------
In something like pg_query_params(), it is OK to use eg
$sql = "select * from table where id=$1"
In other words, because variable names cannot begin with numbers, constructs
such as $1, $2 etc are literal, even within double-quotes.
This is actually quite helpful - and it seems to be deliberate (at any rate, no errors appear in
logfile). I think the documentation should make it clearer.
Test script:
---------------
echo 'You owe me $100';
echo "You owe me \$100";
echo "You owe me $100";
all produce the same output. The 3rd of these is (to me) rather surprising, though useful, and
isn't documented, either in the variable-naming or the double-quoted string sections. It should
also appear in the pg_query_params() docs.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62997&edit=1