Page 1 of 1

PHP better code

Posted: Wed Mar 28, 2012 12:40 am
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 />


Re: PHP better code

Posted: Wed Mar 28, 2012 7:18 am
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 />'; 

Re: PHP better code

Posted: Thu Mar 29, 2012 3:03 am
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!

Re: PHP better code

Posted: Wed Apr 11, 2012 10:03 pm
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 />";