Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Robert Cummings

On Thu, 2008-03-20 at 09:22 -0700, Lamp Lists wrote:
 hi,
 I saw several times that some people use this
  
 $parameters = array(
   'param1' = {$_POST[param1]},
   'param2' = {$_POST[param2]}
  );

Ignorance.

 or
 
  $query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');

Ignorance/Habit since you only need to do that if the context of the
variable is ambiguous... for instance:

   $something = Something blah $blehblah blah blah.

When what was really wanted was:

   $something = Something blah {$bleh}blah blah blah.

One would hope that in the example you provided the developer properly
escaped $session_id.

 I would use:
 
 $parameters = array(
   'param1' = $_POST[param1],
   'param2' = $_POST[param2]
  );

I sure would too (although I'd use single quotes for the array indices).
I'd also append a dangling , to that last array entry to make it
simplistic to add another entry and never worry about having to add a ,
to the previous entry. The following is perfectly legal in PHP and is an
intentional feature:

$parameters = array
(
'param1' = $_POST['param1'],
'param2' = $_POST['param2'],
);
 
  and
 
  $query = mysql_query(SELECT * FROM table1 WHERE id=' .$session_id.
 ' );

That's broken unless you're relying on a MySQL type conversion trick to
match the session ID since you've prepended and appended the id match
with spaced. You probably meant to type:

SELECT * FROM table1 WHERE id='.$session_id.' 

In that case, I normally do similar for queries, except mine look more
like:

SELECT * FROM table1 WHERE id=.$db-quote( $session_id ). 

Where the quote() method performs both the quoting and the escaping.

 does it really matter? is there really difference or these are just two 
 styles?

It matters greatly if it's incorrect :) Otherwise it matters less so but
one is more efficient than the other. There's absolutely no reason to
interpolate a value if the value is the variable's value itself.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Jason Pruim


On Mar 20, 2008, at 12:22 PM, Lamp Lists wrote:

hi,
I saw several times that some people use this

$parameters = array(
 'param1' = {$_POST[param1]},
 'param2' = {$_POST[param2]}
);

or

$query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');

I would use:

$parameters = array(
 'param1' = $_POST[param1],
 'param2' = $_POST[param2]
);

and

$query = mysql_query(SELECT * FROM table1 WHERE id=' . 
$session_id. ' );



I may have been the one you were talking about so I figured I should  
reply. I picked up the habit doing that for stuff that was being  
displayed using HEREDOC. When I asked for some help awhile back on a  
project I was working on and they said to use HEREDOC, and gave an  
example of how to do it and they had all the variables surrounded with  
{} So I assumed you had to do it that way...


Anyone care to correct me?

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
[EMAIL PROTECTED]




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



Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Nathan Nobbe
On Thu, Mar 20, 2008 at 12:22 PM, Lamp Lists [EMAIL PROTECTED] wrote:

 hi,
 I saw several times that some people use this

 $parameters = array(
  'param1' = {$_POST[param1]},
  'param2' = {$_POST[param2]}
  );

 or

  $query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');

 I would use:

 $parameters = array(
  'param1' = $_POST[param1],
  'param2' = $_POST[param2]
  );

  and

  $query = mysql_query(SELECT * FROM table1 WHERE id=' .$session_id. '
 );


 does it really matter? is there really difference or these are just two
 styles?


the short answer is yes.
i think you can find a sufficient explanation here,
http://us.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple
and here
http://us.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

-nathan


Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Ray Hauge

Lamp Lists wrote:

hi,
I saw several times that some people use this
 
$parameters = array(

  'param1' = {$_POST[param1]},
  'param2' = {$_POST[param2]}
 );

or

 $query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');

I would use:

$parameters = array(
  'param1' = $_POST[param1],
  'param2' = $_POST[param2]
 );
 
 and


 $query = mysql_query(SELECT * FROM table1 WHERE id=' .$session_id. ' );


does it really matter? is there really difference or these are just two 
styles?

thanks.

-ll


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


The brackets are used to enforce that the entire contents between them 
is a variable.  It helps when you're using class members.


$example = this is an {$example-text};

It's also handy when you're putting variables in a heredoc.

I would suggest not using {$_POST[param1]}, like you said.  It's 
just going to make PHP figure out the string, then put the value in that 
string.  If you really wanted to make sure it's a string type then you 
can do (string)$_POST['param1'].


The short answer is you can do it the way you're doing and everything 
will work out just fine :)


--
Ray Hauge
www.primateapplications.com

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



Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Robin Vickery
On 20/03/2008, Lamp Lists [EMAIL PROTECTED] wrote:
 hi,
  I saw several times that some people use this

  $parameters = array(
   'param1' = {$_POST[param1]},
   'param2' = {$_POST[param2]}
   );

  or

   $query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');

  I would use:

  $parameters = array(
   'param1' = $_POST[param1],
   'param2' = $_POST[param2]
   );

   and

   $query = mysql_query(SELECT * FROM table1 WHERE id=' .$session_id. ' );


  does it really matter? is there really difference or these are just two 
 styles?

yes, it matters when you're trying to include a complex variable

this is a $variable; # ok
this is an $array[12]; # ok
this is an $array[word]; # warning under E_STRICT
this is an $array[two words]; # not ok, can't have whitespace
this is an {$array[two words]}; # not ok, indexes should be quoted
this is an {$array['two words']}; # ok
this is an $object-property; # ok if you're after the property
this is an {$object}-property; # but you need brackets if you want
the object as a string

etc...

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



Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Brady Mitchell


On Mar 20, 2008, at 922AM, Lamp Lists wrote:

$query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');


For a non-array value, the curly braces are unnecessary:
$query = mysql_query(SELECT * FROM table1 WHERE id='$session_id')

WIth an array element, you have to either use the curly braces or do  
as you have and end the string, concat the value and start the string  
again as needed.

$query = mysql_query(SELECT * FROM table1 WHERE id='{$clean['id']}')

When dealing with array elements, I use the curly braces method. IMO  
it's cleaner and easier to read.



I would use:

$parameters = array(
 'param1' = $_POST[param1],
 'param2' = $_POST[param2]
);


This is how I do it as well.

does it really matter? is there really difference or these are just  
two styles?


IMO, it's a matter of preference and style. There may be performance  
differences between the two, but I doubt that either one is so much  
worse than the other that it would have a noticeable impact on your  
script in most situations.


Brady

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



Re: [PHP] why use {} around vraiable?

2008-03-20 Thread Lamp Lists
- Original Message 
From: Nathan Nobbe [EMAIL PROTECTED]
To: Lamp Lists [EMAIL PROTECTED]
Cc: php-general@lists.php.net
Sent: Thursday, March 20, 2008 11:35:42 AM
Subject: Re: [PHP] why use {} around vraiable?

On Thu, Mar 20, 2008 at 12:22 PM, Lamp Lists [EMAIL PROTECTED] wrote:

 hi,
 I saw several times that some people use this

 $parameters = array(
  'param1' = {$_POST[param1]},
  'param2' = {$_POST[param2]}
  );

 or

  $query = mysql_query(SELECT * FROM table1 WHERE id='{$session_id}');

 I would use:

 $parameters = array(
  'param1' = $_POST[param1],
  'param2' = $_POST[param2]
  );

  and

  $query = mysql_query(SELECT * FROM table1 WHERE id=' .$session_id. '
 );


 does it really matter? is there really difference or these are just two
 styles?


the short answer is yes.
i think you can find a sufficient explanation here,
http://us.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple
and here
http://us.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

-nathan



ok. I got it.
actually, my question was about: these two examples

$fruits = array('strawberry' = 'red', 'banana' = 'yellow');
echo A banana is {$fruits['banana']}.;
echo A banana is  . $fruits['banana'] . .;

are the same.

Though, learned few more other things too :D

Thanks guys.

-ll


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ