Reputation: 147
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';
?>
Upvotes: 0
Views: 189
Reputation: 1085
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.
Upvotes: 1
Reputation: 835
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';
?>
Upvotes: 1
Reputation: 76408
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).
Upvotes: 2
Reputation: 101
PSR - These are not law-like rules but if you follow these you can not be wrong.
Upvotes: 1