PHP Best Coding Practices

When I learned PHP, we followed this programming practice as described in Kevin Yanks “Build Your Own Database Driven Website Using PHP and MySQL”:
[FONT=“Courier New”]
<?php

$sql = “SELECT joketext FROM joke”;

$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching jokes: ’ . mysqli_error($link);
include ‘error.html.php’;
exit();
}

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

include ‘jokes.html.php’;
[/FONT]
and then use a foreach loop to display the information in the web page:

<p>Here are all the jokes in the database: </p>
<?php foreach ($jokes as $joke): ?>
<blockquote><p>
<?php echo htmlspecialchars($joke, ENT_QUOTES, ‘UTF-8’); ?>
</p></blockquote>
<?php endforeach; ?>

I am currently working as an intern developing a information management system for my a department at my college, and the internship supervisor suggests completing the same task as follows:
[FONT=“Courier New”]
<?php

$sql = “SELECT joketext FROM joke”;

$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching jokes: ’ . mysqli_error($link);
include ‘error.html.php’;
exit();
}
?> [/FONT]
and then using the while loop to directly output the information from the database, basically negating the need for the array named “$row”:
[FONT=“Courier New”]

<p>Here are all the jokes in the database: </p>

<?php while ($joke = mysqli_fetch_array($result)){ ?>

<blockquote><p>
<?php echo htmlspecialchars($joke[‘joketext’], ENT_QUOTES, ‘UTF-8’); ?>
</p></blockquote>

<?php } ?>[/FONT]

While I understand how each version works, I’m curious as to why either method would be more desirable. I’d thought of server resources as a reason, given that there would be one less array to process, but I’ve learned to trust the information in Sitepoints books.

Does anyone have any ideas as to why one way would be preferable over the other?

I don’t think it matters too much. I’ve always used a foreach loop to output information from a database and any of the frameworks I’ve used have advocated using foreach loops as well.

Method 1 is like me buying a book for your birthday (the MySQL result) and book with blank pages (the array), then me copying over all the text from the book to the blank book, and then giving you the no longer blank book.
It would have been easier for me to just give you the book now wouldn’t it? First of all I would have to carry just one book home instead of two (less memory usage), and I didn’t have to copy one book to another (less processor cycles needed).

Of course for small result sets it doesn’t really matter what you use --the difference in speed will be barely noticable-- but since you asked for best coding practices: method 2 is better :slight_smile:

That’s what I thought of when I was first shown the second solution. I just don’t understand why Sitepoint would advocate the use of the first method if it was less functional, less efficient, and didn’t offer any other benefits.

While they both work, method 1 is the only one you should use. Why? Seperation of concerns. This is a tiny example, but if you carry on coding like method 2 you will end up with what’s called spaghetti code.

What if down the track you decided to get your jokes from a csv file? You can’t just drop in a new controller (the logic that fetches the jokes and makes $jokes) you have to alter the template as well. The template should not care at all where the jokes come from, it should just know how to display a bunch of them.

The amount of memory used or extra processing is completely insignificant.

I would recommend you stick with the method of collecting the data in an array before output. Separating data and display like this will yield more flexible, readable and manageable code as hash as mentioned.

This is why colleges/universities are terrible places to learn programming. The lecturers are clueless about new (year 2000) technologies. Save your money and drop out, is my advice :slight_smile:

This is possibly the worst post I’ve seen on SP.

Second that!

I’ve just graduated from university and I can say with certainty I never would have been where I am today without it!
It’s true they don’t teach at a practical hands-on level (I guess this is what AlienDev was trying to say), but they teach you the abstract basics of programming, which are IMHO far more important than real-life examples.

Sheesh …

This is why colleges/universities are terrible places to learn programming. The lecturers are clueless about new (year 2000) technologies. Save your money and drop out, is my advice

Yuck. LOL

While I agree that post secondary courses appear to focus on archaic techniques, if you want to be on the bleeding edge of development you become regualrs at places like SitePoint, where others in your field are the ones pushing the boundreis of best practices, etc.

That being said, university or college is by no means a waste of time, the experience, colleuges, contacts alone justify your stay.

Stay in school, I dropped out and I wish I had stayed to persue a phd I would love to get paid for doing research, are you kidding me, thats my dream job. :stuck_out_tongue:

Cheers,
Alex

In my opinion, you have to learn two things, way more important than this:
Code highlighting and code indention.
These are the best two things since programming were invented.

As for the question asked, understanding of the best method comes from understanding of templates (or, as hash said more precisely - Seperation of concerns. ). One have to grow to understand these matters though