0

Which formatting is correct? I don't know exactly how to indent PHP code yet. I feel like Set 2 is correct but I don't know yet.

Set 1

$connection = mysqli_connect('localhost', 'ijdbuser', 'ijdbpw');

if (!$connection)
{
    $error = 'Unable to connect to the database server.';
    include 'error.html.php';
    exit();
}

if (!mysqli_set_charset($connection, 'utf8'))
{
    $output = 'Unable to set database connection encoding.';
    include 'output.html.php';
    exit();
}

if (!mysqli_select_db($connection 'ijdb'))
{
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';
    exit();
}

    $result = mysqli_query($connection, "SELECT joketext FROM joke");

        if (!$result)
        {
            $error = 'Error fetching jokes: ' . mysqli_error($connection
            );
            include 'error.html.php';
            exit();
        }

                while ($row = mysqli_fetch_array($result))  
                {  
                    $jokes[] = $row['joketext'];  
                }  
                include 'jokes.html.php';  

?>

Set 2

$connection = mysqli_connect('localhost', 'ijdbuser', 'ijdbpw');

if (!$connection)
{
    $error = 'Unable to connect to the database server.';
    include 'error.html.php';
    exit();
}

if (!mysqli_set_charset($connection, 'utf8'))
{
    $output = 'Unable to set database connection encoding.';
    include 'output.html.php';
    exit();
}

if (!mysqli_select_db($connection 'ijdb'))
{
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';
    exit();
}

$result = mysqli_query($connection, "SELECT joketext FROM joke");

if (!$result)
{
    $error = 'Error fetching jokes: ' . mysqli_error($connection
    );
    include 'error.html.php';
    exit();
}

while ($row = mysqli_fetch_array($result))  
{  
    $jokes[] = $row['joketext'];  
}  
include 'jokes.html.php';  

?>
6
  • 1
    #2 is correct. Statements that are at the same nesting level should be indented the same. Commented May 11, 2015 at 10:05
  • Why aren't you using an editor that indents your code automatically for you? Commented May 11, 2015 at 10:05
  • 1
    For the standard coding styles take a look at the PHP-Fig website especially PSR 1 and PSR 2 Commented May 11, 2015 at 10:07
  • I'm using Sublime. I'm new to Sublime 3 so maybe I don't have the plugin to do indenting. If you know of a plugin let me know. Thx. So for my if statements the left curly bracket should be if (!$result) { on the same line as opposed to if (!$result) <newline> {. Is that right, according to PSR 2? Commented May 11, 2015 at 10:23
  • what the IDE did you used ? The PatentStorm will prefect format you code,it's support flexible format configurations. Commented May 11, 2015 at 10:39

4 Answers 4

2

There is no "right" or "wrong" formatting, not really. Just be consistent, and choose the style you like best. However, there is such a thing as PHP-FIG. The coding style they propose is the most commonly adopted one, and the one you'll see used in almost all major open-source projects.

Personally, I try to subscribe to their coding standard as much as possible, though I tend to prefer allman-style brackets, too (if ()\n{\n). I think I sent them a PR a few years back to allow for that indentation style, too, but it got rejected (mainly to avoid having merge conflicts surrounding conflicting whitespace, so I understand where they're coming from).

Sign up to request clarification or add additional context in comments.

3 Comments

While there are lots of detailed style recommendations that are matter of opinion, I don't think there's any disagreement about basic indenting. His first version is really wrong.
@barmar: It's wrong in the sense that the indentation looks like it's a copy-paste job into vim without a :set paste, but the code itself works, and is syntactically valid (PHP not having significant whitespace). But I agree, and I'll edit my answer to reflect on the nonsensicalness of the indentation in the first snippet
The only difference I can see between the two versions is the indentation. And his question specifically asks about style.
1

PSR - These are not law-like rules but if you follow these you can not be wrong.

1 Comment

Don't post link-only answers. Copy the relevant text from the remote site into your answer, and use the link for support. I couldn't even find the answer to his question in there myself.
1

Please use this. PHP beautifier can also help alot.

<?php
$connection = mysqli_connect('localhost', 'ijdbuser', 'ijdbpw');

if (!$connection)
    {
    $error = 'Unable to connect to the database server.';
    include 'error.html.php';

    exit();
    }

if (!mysqli_set_charset($connection, 'utf8'))
    {
    $output = 'Unable to set database connection encoding.';
    include 'output.html.php';

    exit();
    }

if (!mysqli_select_db($connection'ijdb'))
    {
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';

    exit();
    }

$result = mysqli_query($connection, "SELECT joketext FROM joke");

if (!$result)
    {
    $error = 'Error fetching jokes: ' . mysqli_error($connection);
    include 'error.html.php';

    exit();
    }

while ($row = mysqli_fetch_array($result))
    {
    $jokes[] = $row['joketext'];
    }

include 'jokes.html.php';

?>

2 Comments

I don't like this formatting at all. Also, if it is your personal preference please state it, otherwise refer the style guide you're using.
I stated the source to be PHP beatifier. I have lots of preference especially when building classes.
1

Set 2

seems more readable. Yes, clean, readability, clear, easy to maintain are some factors to more than just standards. Have a look at Proposed Standards Recommendation (PSR) but also look at different php open source frameworks and their style guides or coding convention or contributions styleguide on github.

For example:

I go with what the team and community/organisation of the project is most comfortable with.. fluid.

Also will help by: ( Clean Code by Robert C.Martin )

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.