Page 1 of 1

[solved] brace in a php contruction???

Posted: Sun Jul 19, 2009 2:25 pm
by robbedoes
While creating a module I had a look at the News module. In News/action.defaultadmin.php I found this code snippet:

if( $this->CheckPermission( 'Modify Templates' ) ) {
   echo $this->StartTab('summary_template', $params);
   {
     echo ''.$this->Lang('title_available_templates').'';
     $this->_AdminCreateTemplateList($id,$returnid,
     'summary',
     'default_summary_template_contents',
     'summary_template',
     'current_summary_template',
     $this->Lang('title_summary_template'));
   }
   echo $this->EndTab();

My question is what this brace is doing:

echo $this->StartTab('summary_template', $params);
   {

Thanks,

Rob

Re: brace in a php contruction???

Posted: Sun Jul 19, 2009 11:49 pm
by Nullig
I think what he's saying is that there's no "conditional" statement to warrant the "{".

Nullig

Re: brace in a php contruction???

Posted: Mon Jul 20, 2009 7:55 am
by robbedoes
Nullig wrote: I think what he's saying is that there's no "conditional" statement to warrant the "{".

Nullig
Indeed :-) No If, no function, just an opening brace, some code and then a closing brace.
I’ve had a look at the PHP documentation but there is nothing about this kind of use.

Rob

Re: brace in a php contruction???

Posted: Mon Jul 20, 2009 11:36 am
by Dee
AFAIK the curly braces are (besides control structures) mainly used for complex syntax in strings.

I use it a lot when I'm using object methods in sql queries, for example:

Code: Select all

$sql = "SELECT * FROM {$config->GetTable()} WHERE some_field = {$someobject->GetSomeValue()}";
(to prevent having to do something like:)

Code: Select all

$sql = "SELECT * FROM " . $config->GetTable() . " WHERE some_field = " . $someobject->GetSomeValue()";
(or:)

Code: Select all

$table = $config->GetTable();
$value = $someobject->GetSomeValue();
$sql = "SELECT * FROM $table WHERE some_field = $value;
When they're used around a some lines of code like in this case I think it's just to show that the lines/statements form a "code block"/statement group (for readability purposes).
statements can be grouped into a statement-group by encapsulating a group of statements with curly braces
Regards,
D

Re: brace in a php contruction???

Posted: Tue Jul 21, 2009 9:01 am
by robbedoes
I am coding PHP for years but never saw this handy construction.
Thank you Dee for the kind explanation.

Best regards,

Rob