On Tue, 2003-06-17 at 07:47, Chris Hayes wrote:
> At 16:37 17-6-03, you wrote:
> >On Tue, 2003-06-17 at 09:09, nabil wrote:
> > > A side question along with  this ,,, how can I include 
> > $_POST['foo']  in the
> > > :
> > > $sql ="select * from db where apple = '$_POST['foo']'  ";
> > >
> > > without getting an error ??
> > > should I append it as $var= $_POST['foo']; before???
> > >
> >
> >The rule of thumb I follow with these and other Associative Arrays is:
> >
> >when setting the variable its $_POST['foo']
> >when accessing the variable its $_POST[foo]
> 
> May i rephrase that to:
> 
> "Use quotes whenever possible, with one exception: within double quotes."
> 
> Reason: your rule of thumb fails with:
>   $val=$_POST['val'];
>
> Plus: when using CONSTANTs as keys, do not try to access them within quoted 
> strings.

You've gotten the closest so far. :)

 o Use quotes when the key is a string literal, and one of the 
   following is true: A) the array reference is not inside a string or
   heredoc; or B) the array reference _is_ within a string or heredoc, 
   but is written using complex syntax (see below).
 o If the key is an integer, you do not need quotes, but you may use
   them if you wish.
 o If the key is a constant, do not use quotes.
 o If the key is a variable, single-quotes will prevent PHP from    
   interpreting the variable, and the result will not be what you 
   want. Use no quotes around variable keys (or double-quotes, but
   that's pointless).
 o If the key is some other kind of expression (function call, math
   operation, etc) do not use quotes.
 o Inside a double-quoted string or heredoc, you can opt to leave out
   the quotes if you're using a string literal key. 
 o In general within strings and heredocs, you may want to adopt the
   practice of always using the complex syntax to express array
   references: $str = "This is a string {$array['user_name']}";
   (Complex syntax isn't complex; just wrap the array reference
   between a { and a }). Note that in this case, you again need the
   quotes. Always using the complex syntax for array (and object)
   referencing within strings will help prevent odd errors with the
   arrays not being interpreted correctly, and indeed, if you want to
   go any further than one level deep into an array or object within a
   string, you must use the complex syntax.


I've spent some time this morning cleaning up the Array type page, so
hopefully some of this will be a bit clearer (at least, after the docs
get generated). There is also definitive information on the string
variable interpolation topic here:

   http://www.php.net/manual/en/language.types.string.php


Hope this helps,

Torben



-- 
 Torben Wilson <[EMAIL PROTECTED]>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====-----




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to