PHP better code

C++, C#, Java, PHP, ect...
Post Reply
Zester
Posts: 53
Joined: Sat Mar 03, 2012 11:33 pm

PHP better code

Post by Zester »

Hi all,

I just have question about which way of coding this is better or right?
have seen both ways done but I have no idea witch is better.

Code: Select all

	$sql = "select * from lqlotl.inventory where lqlotl.inventory.charid='$charid' AND lqlotl.inventory.itemid='$itemid' AND lqlotl.inventory.randid='$randid'";
 	$iteminfo = mysql_query($sql) or die("Could not item info get item");
	$iteminfo2 = mysql_fetch_array($iteminfo);
This way :

Code: Select all

print "<strong>ITEM INFO</strong><br />
		Name: " . $iteminfo2['name'] . "<br />
		Type :" . $iteminfo2['type'] . "<br />
		SubType :" . $iteminfo2['subtype'] . "<br />
		Stack size :" . $iteminfo2['stack'] . "<br />
		Quantity :" . $iteminfo2['quantity'] . "<br />
		Weight :" . $iteminfo2['weight'] . "<br />";
Or this this way

Code: Select all

$name = $iteminfo2['name'];
	$stack = $iteminfo2['stack'];
	$quantity = $iteminfo2['quantity'];
	$type = $iteminfo2['type'];

print "<strong>ITEM INFO</strong><br />
		Stack: " . $stack . "<br />
		Type :" . $type . "<br />
		Quantity :" . $quantity . "<br />

User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP better code

Post by Chris »

It's pretty much down to personal preference. As long as it works.

I personally would go for method 1. Saves some memory, not that it's much. And you can see instantly where those values are coming from.

What I would also do is name my array something else, I tend to avoid using numbers when naming my variables if they aren't needed. mysql_fetch_assoc would also be better in this case rather than mysql_fetch_array.

Code: Select all

$inventory_query = mysql_query("SELECT * FROM lqlotl.inventory
                                        WHERE lqlotl.inventory.charid = '$charid'
                                          AND lqlotl.inventory.itemid = '$itemid'
                                          AND lqlotl.inventory.randid = '$randid'");

$inventory_array = mysql_fetch_assoc($inventory_query);

echo '<strong>ITEM INFO</strong><br />
      Name : ' . $inventory_array['name'] . '<br />
      Type : ' . $inventory_array['type'] . '<br />
      SubType :' . $inventory_array['subtype'] . '<br />
      Stack size :' . $inventory_array['stack'] . '<br />
      Quantity :' . $inventory_array['quantity'] . '<br />
      Weight :' . $inventory_array['weight'] . '<br />'; 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP better code

Post by Jackolantern »

Yeah, pretty much personal preference. However, I would probably go for method 2 because it makes it a bit more readable, and creates a template of sorts you can use to make sure you don't forget any of the columns when you are actually echo'ing the text and tags.

But as Chris said, it is up to you!
The indelible lord of tl;dr
brockfanning
Posts: 10
Joined: Wed Apr 11, 2012 9:09 pm

Re: PHP better code

Post by brockfanning »

I like readability too, and don't believe the memory difference would matter that much. Also it's worth pointing out that in #2, since you're using double-quotes, you don't have to concatenate all the variables: you can put them directly in the main string.

Code: Select all

print "<strong>ITEM INFO</strong><br />
  Stack: $stack<br />
  Type: $type<br />
  Quantity: $quantity<br />";
Also you can do that same thing in #1 if you use curly braces:

Code: Select all

print "<strong>ITEM INFO</strong><br />
  Name: {$iteminfo2['name']}<br />
  Type: {$iteminfo2['type']}<br />
  SubType: {$iteminfo2['subtype']}<br />
  Stack size: {$iteminfo2['stack']}<br />
  Quantity: {$iteminfo2['quantity']}<br />
  Weight: {$iteminfo2['weight']}<br />";
Post Reply

Return to “Coding”