Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the position of the brackets really matter in PHP (for instance, for the sake of readability)?

Tags:

php

I started with CSS so I always used brackets this way:

#content {
    selector: value;
}

While using CodeIgniter I noticed that each file have the brackets like this:

function something here() 
{
    $this->$item('item');
    $data['rows'] = $this->data_model->getAll();

    $this->load->view('home', $data);
}

Is the second example better for readability or copying or pasting?


2 Answers

That actually looks a bit wrong for CodeIgniter; according to the Style Guide, they use Allman style. See this article on Wikipedia for some background on indentation styles. [NB: indentation now fixed in question].

Really, though, it's just a matter of religion (note that Wikipedia's Indent Style article is currently linked to from its Wars of Religion article!)

The best thing to do is to be consistent.

So, for your own project, use whatever you feel is the most readable/usable. But if you wanted to contribute code to CodeIgniter, you'd use the Allman style, and if you're writing a project that just uses CodeIgniter, it might be easier to follow their style simply to stop your brain from having to swap between two styles while you're simultaneously digging through their code and writing yours.

like image 196
Matt Gibson Avatar answered Oct 19 '25 20:10

Matt Gibson


No difference at all, you can use this style just if it's more understandable for you.

like image 38
Centurion Avatar answered Oct 19 '25 22:10

Centurion