FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » When do I use {}?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
When do I use {}? [message #169528] Fri, 17 September 2010 02:13 Go to next message
bruceaj is currently offline  bruceaj
Messages: 30
Registered: September 2010
Karma: 0
Member
I have code samples where some variables are wrapped in {} (curly
brackets.) My problem is that I can find anything in the documentation
when they can be uses.

Can someone tell me or point me to some documentation??

Thanks...

Bruce
Re: When do I use {}? [message #169529 is a reply to message #169528] Fri, 17 September 2010 02:57 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
Curly brakets just wrap code... if they are after a control statement...
like an if, a do, a while, an each, a foreach, switch or others... they
determine the code which is going to be executed when necessary.

On 09/16/2010 10:13 PM, bruceaj wrote:
> I have code samples where some variables are wrapped in {} (curly
> brackets.) My problem is that I can find anything in the documentation
> when they can be uses.
>
> Can someone tell me or point me to some documentation??
>
> Thanks...
>
> Bruce
Re: When do I use {}? [message #169530 is a reply to message #169528] Fri, 17 September 2010 03:47 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/16/2010 10:13 PM, bruceaj wrote:
> I have code samples where some variables are wrapped in {} (curly
> brackets.) My problem is that I can find anything in the documentation
> when they can be uses.
>
> Can someone tell me or point me to some documentation??
>
> Thanks...
>
> Bruce

You use them when you want to evaluate non-simple variables (i.e. array
elements) in a double-quoted string, such as:

$a = 'World"; // init variable
$b = array('Hello', 'World');

echo "Hello $a"; // Works - $a is a simple variable
echo "Hello {$a}"; // Also works - and may be clearer

echo "Hello $b[1]"; // Does NOT work - $b is an array
echo "Hello {$b[1]}"; // This one works
echo "{$b[0]} {$b[1]}"; // Also works

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: When do I use {}? [message #169531 is a reply to message #169529] Fri, 17 September 2010 09:28 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Marious Barrier wrote:
> Curly brakets just wrap code... if they are after a control statement...
> like an if, a do, a while, an each, a foreach, switch or others... they
> determine the code which is going to be executed when necessary.
>

Strictly they are BLOCK statement delimiters.


so if($something)
{
//then this block
...
...

..} //end if
and so on.

Basically the way PHP does a GOTO without using a GOTO :-)


> On 09/16/2010 10:13 PM, bruceaj wrote:
>> I have code samples where some variables are wrapped in {} (curly
>> brackets.) My problem is that I can find anything in the documentation
>> when they can be uses.
>>
>> Can someone tell me or point me to some documentation??
>>
>> Thanks...
>>
>> Bruce
Re: When do I use {}? [message #169533 is a reply to message #169531] Fri, 17 September 2010 09:36 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 9/17/2010 11:28 AM, The Natural Philosopher wrote:
> Marious Barrier wrote:
>> Curly brakets just wrap code... if they are after a control
>> statement... like an if, a do, a while, an each, a foreach, switch or
>> others... they determine the code which is going to be executed when
>> necessary.
>>
>
> Strictly they are BLOCK statement delimiters.
>
>
> so if($something)
> {
> //then this block
> ..
> ..
>
> .} //end if
> and so on.
>
> Basically the way PHP does a GOTO without using a GOTO :-)

AAARGH!
Stop that please please.

If you keep bringing that GOTO up in here I'll have to track you down,
find your house, and put a VIC20 through your throat for every time you
used that word!
That is a joke, don't worry. I am not a violent man, and would never do
such things to a VIC20s.

Regards,
Erwin Moller


>
>
>> On 09/16/2010 10:13 PM, bruceaj wrote:
>>> I have code samples where some variables are wrapped in {} (curly
>>> brackets.) My problem is that I can find anything in the documentation
>>> when they can be uses.
>>>
>>> Can someone tell me or point me to some documentation??
>>>
>>> Thanks...
>>>
>>> Bruce


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Re: When do I use {}? [message #169534 is a reply to message #169529] Fri, 17 September 2010 09:53 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On 17 Sep, 03:57, Marious Barrier <marious.barr...@gmail.com> wrote:
>> I have code samples where some variables are wrapped in {}  (curly
>> brackets.) My problem is that I can find anything in the documentation
>> when they can be uses.
>
>> Can someone tell me or point me to some documentation??
>
>> Thanks...
>
>> Bruce
> Curly brakets just wrap code... if they are after a control statement...
> like an if, a do, a while, an each, a foreach, switch or others... they
> determine the code which is going to be executed when necessary.
>
> On 09/16/2010 10:13 PM, bruceaj wrote:

Please do not top post (top posting fixed)
Also your answer is not the correct answer to the OP's question. See
Jerry's correct answer later in this thread.
Re: When do I use {}? [message #169536 is a reply to message #169533] Fri, 17 September 2010 11:29 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/17/2010 5:36 AM, Erwin Moller wrote:
> On 9/17/2010 11:28 AM, The Natural Philosopher wrote:
>> Marious Barrier wrote:
>>> Curly brakets just wrap code... if they are after a control
>>> statement... like an if, a do, a while, an each, a foreach, switch or
>>> others... they determine the code which is going to be executed when
>>> necessary.
>>>
>>
>> Strictly they are BLOCK statement delimiters.
>>
>>
>> so if($something)
>> {
>> //then this block
>> ..
>> ..
>>
>> .} //end if
>> and so on.
>>
>> Basically the way PHP does a GOTO without using a GOTO :-)
>
> AAARGH!
> Stop that please please.
>
> If you keep bringing that GOTO up in here I'll have to track you down,
> find your house, and put a VIC20 through your throat for every time you
> used that word!
> That is a joke, don't worry. I am not a violent man, and would never do
> such things to a VIC20s.
>
> Regards,
> Erwin Moller
>
>
>>
>>
>>> On 09/16/2010 10:13 PM, bruceaj wrote:
>>>> I have code samples where some variables are wrapped in {} (curly
>>>> brackets.) My problem is that I can find anything in the documentation
>>>> when they can be uses.
>>>>
>>>> Can someone tell me or point me to some documentation??
>>>>
>>>> Thanks...
>>>>
>>>> Bruce
>
>

Erwin,

Don't bother. TNP has no idea what structured programming (or any other
programming, for that matter) is about. He just hacks out code until he
gets a half-assed correct result.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: When do I use {}? [message #169545 is a reply to message #169528] Fri, 17 September 2010 13:13 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(bruceaj)

> I have code samples where some variables are wrapped in {} (curly
> brackets.) My problem is that I can find anything in the documentation
> when they can be uses.
>
> Can someone tell me or point me to some documentation??

http://www.php.net/manual/en/language.types.string.php#language.types.strin g.parsing.complex

Micha
Re: When do I use {}? [message #169547 is a reply to message #169529] Fri, 17 September 2010 13:31 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 17/09/10 03:57, Marious Barrier wrote:
> Curly brakets just wrap code... if they are after a control statement...
> like an if, a do, a while, an each, a foreach, switch or others... they
> determine the code which is going to be executed when necessary.
>
> On 09/16/2010 10:13 PM, bruceaj wrote:
>> I have code samples where some variables are wrapped in {} (curly
>> brackets.) My problem is that I can find anything in the documentation
>> when they can be uses.
>>
>> Can someone tell me or point me to some documentation??

Perhaps he means:

echo "some string with a $array[member]\n";

vs:

echo "some string with a {$array['member']}\n";

I *think* you need to use {} if you want to stack a multiple dimensional
or nested array ref in an echo, eg:

echo "some string with a {$array['row']['column']['layer']}\n";

Rgds

Denis McMahon
Re: When do I use {}? [message #169550 is a reply to message #169547] Fri, 17 September 2010 16:39 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On 09/17/2010 09:31 AM, Denis McMahon wrote:
> On 17/09/10 03:57, Marious Barrier wrote:
>> Curly brakets just wrap code... if they are after a control statement...
>> like an if, a do, a while, an each, a foreach, switch or others... they
>> determine the code which is going to be executed when necessary.
>>
>> On 09/16/2010 10:13 PM, bruceaj wrote:
>>> I have code samples where some variables are wrapped in {} (curly
>>> brackets.) My problem is that I can find anything in the documentation
>>> when they can be uses.
>>>
>>> Can someone tell me or point me to some documentation??
>
> Perhaps he means:
>
> echo "some string with a $array[member]\n";
>
> vs:
>
> echo "some string with a {$array['member']}\n";

He did... but I was too drunk that I just read some words yesterday and
started to write. lol
Re: When do I use {}? [message #169551 is a reply to message #169534] Fri, 17 September 2010 16:40 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On 09/17/2010 05:53 AM, Captain Paralytic wrote:
> On 17 Sep, 03:57, Marious Barrier<marious.barr...@gmail.com> wrote:
>>> I have code samples where some variables are wrapped in {} (curly
>>> brackets.) My problem is that I can find anything in the documentation
>>> when they can be uses.
>>
>>> Can someone tell me or point me to some documentation??
>>
>>> Thanks...
>>
>>> Bruce
>> Curly brakets just wrap code... if they are after a control statement...
>> like an if, a do, a while, an each, a foreach, switch or others... they
>> determine the code which is going to be executed when necessary.
>>
>> On 09/16/2010 10:13 PM, bruceaj wrote:
>
> Please do not top post (top posting fixed)

Mistake... how many times more have I done it?

> Also your answer is not the correct answer to the OP's question. See
> Jerry's correct answer later in this thread.

Not the correct because I did not read correctly... however it still is
good to know lol.
Re: When do I use {}? [message #169552 is a reply to message #169530] Fri, 17 September 2010 17:44 Go to previous messageGo to next message
bruceaj is currently offline  bruceaj
Messages: 30
Registered: September 2010
Karma: 0
Member
On Sep 16, 11:47 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 9/16/2010 10:13 PM, bruceaj wrote:
>
>> I have code samples where some variables are wrapped in {}  (curly
>> brackets.) My problem is that I can find anything in the documentation
>> when they can be uses.
>
>> Can someone tell me or point me to some documentation??
>
>> Thanks...
>
>> Bruce
>
> You use them when you want to evaluate non-simple variables (i.e. array
> elements) in a double-quoted string, such as:
>
> $a = 'World";  // init variable
> $b = array('Hello', 'World');
>
> echo "Hello $a";  // Works - $a is a simple variable
> echo "Hello {$a}";  // Also works - and may be clearer
>
> echo "Hello $b[1]";  // Does NOT work - $b is an array
> echo "Hello {$b[1]}";  // This one works
> echo "{$b[0]} {$b[1]}";  // Also works
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Sorry I didn't explain the problem more clearly. Yes, this what I was
asking about curly brackets. I'm aware of curly bracket when wrapping
code but I can't seem to find the explanation when using them to wrap
array elements. That is where I have seen them used in code examples,
likc "SELECT a FROM b WHERE t={$_GET['x']}";

Your explanation meets this example. Thanks....

Bruce
Re: When do I use {}? [message #169553 is a reply to message #169545] Fri, 17 September 2010 17:48 Go to previous messageGo to next message
bruceaj is currently offline  bruceaj
Messages: 30
Registered: September 2010
Karma: 0
Member
On Sep 17, 9:13 am, Michael Fesser <neti...@gmx.de> wrote:
> .oO(bruceaj)
>
>> I have code samples where some variables are wrapped in {}  (curly
>> brackets.) My problem is that I can find anything in the documentation
>> when they can be uses.
>
>> Can someone tell me or point me to some documentation??
>
> http://www.php.net/manual/en/language.types.string.php#language.types...
>
> Micha

Thanks for the pointer to documents. Helps greatly...

Bruce
Re: When do I use {}? [message #169554 is a reply to message #169552] Fri, 17 September 2010 18:06 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 17, 1:44 pm, bruceaj <bruc...@bellsouth.net> wrote:
> On Sep 16, 11:47 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>
>
>
>> On 9/16/2010 10:13 PM, bruceaj wrote:
>
>>> I have code samples where some variables are wrapped in {}  (curly
>>> brackets.) My problem is that I can find anything in the documentation
>>> when they can be uses.
>
>>> Can someone tell me or point me to some documentation??
>
>>> Thanks...
>
>>> Bruce
>
>> You use them when you want to evaluate non-simple variables (i.e. array
>> elements) in a double-quoted string, such as:
>
>> $a = 'World";  // init variable
>> $b = array('Hello', 'World');
>
>> echo "Hello $a";  // Works - $a is a simple variable
>> echo "Hello {$a}";  // Also works - and may be clearer
>
>> echo "Hello $b[1]";  // Does NOT work - $b is an array
>> echo "Hello {$b[1]}";  // This one works
>> echo "{$b[0]} {$b[1]}";  // Also works
>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Sorry I didn't explain the problem more clearly. Yes, this what I was
> asking about curly brackets. I'm aware of curly bracket when wrapping
> code but I can't seem to find the explanation when using them to wrap
> array elements. That is where I have seen them used in code examples,
> likc "SELECT a FROM b WHERE t={$_GET['x']}";

This, I hope you realize, is a terrible example because it
demonstrates a tried a true method of allowing SQL injection.
Re: When do I use {}? [message #169557 is a reply to message #169530] Fri, 17 September 2010 19:37 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
Jerry Stuckle wrote:
> On 9/16/2010 10:13 PM, bruceaj wrote:
>
>> I have code samples where some variables are wrapped in {} (curly
>> brackets.) My problem is that I can find anything in the documentation
>> when they can be uses.
>>
>> Can someone tell me or point me to some documentation??
>>
>> Thanks...
>>
>> Bruce
>>
>
> You use them when you want to evaluate non-simple variables (i.e. array
> elements) in a double-quoted string, such as:
>
> $a = 'World"; // init variable
> $b = array('Hello', 'World');
>
> echo "Hello $a"; // Works - $a is a simple variable
> echo "Hello {$a}"; // Also works - and may be clearer
>
> echo "Hello $b[1]"; // Does NOT work - $b is an array
> echo "Hello {$b[1]}"; // This one works
> echo "{$b[0]} {$b[1]}"; // Also works
>
>

Actually ....
echo "Hello $b[1]"; // DOES work - $b[1] is not an array

You need them when you want to evaluate *associative* array elements
within a double-quoted string, such as:
$b = array('first' => 'Hello', 'second' => 'World');

Then ...
echo "Hello $b['second']"; // does NOT work - syntax error
echo "Hello {$b['second']}"; // DOES work

And as a side note ...
echo "Hello $b[first]";
.... also works. Constants are not evaluated when within double quoted
strings.
See
http://www.php.net/manual/en/language.types.array.php#language.types.array. foo-bar
(read "More examples ...").

Curly brackets are also required when a valid variable name character
follows a variable within a string.
$a = 'underscore';
echo "I like to use $as" instead of spaces"; // does NOT work
// $as is undefined
echo "I like to use $a's" instead of spaces"; // DOES work
// ' is not a valid variable name character
echo "I like to use {$a}s" instead of spaces"; // DOES work
// {$a} is evaluated separate from the s

--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
Re: When do I use {}? [message #169561 is a reply to message #169552] Fri, 17 September 2010 20:51 Go to previous message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(bruceaj)

> On Sep 16, 11:47 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Sorry I didn't explain the problem more clearly. Yes, this what I was
> asking about curly brackets. I'm aware of curly bracket when wrapping
> code but I can't seem to find the explanation when using them to wrap
> array elements. That is where I have seen them used in code examples,
> likc "SELECT a FROM b WHERE t={$_GET['x']}";

Stone the one to death who gave you this example. ;-)

(Keyword for further reading: "SQL Injection")

Micha
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: OOP, classes and databases
Next Topic: @sessionstart()
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 09:01:54 GMT 2024

Total time taken to generate the page: 0.02577 seconds