5 Replies - 297 Views - Last Post: 02 December 2019 - 08:09 AM Rate Topic: -----

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 339
  • Joined: 13-March 18

PHP proper tabbing for clean code.

Posted 01 December 2019 - 08:37 AM

I began going through my website and i started cleaning up the code. I realize by trying to debug its hard to find exactly where its going wrong. Problem is im trying to strike a balance in my code and not waste excessive lines for just a blank space.

Here is an example.

while($feed = mysqli_fetch_array($gfeed)){
	$stars = '';
	$star = 1;
	while($star <= 5){
		if($feed['rating'] >= $star){
			$stars .= '<span class="fa fa-star checked"></span>';
		}else{
			$stars .= '<span class="fa fa-star"></span>';
		}
		$star++;
	}
	echo '<table class="w3-table w3-card-2"><tr><td>'.$stars.' '.$feed['rating'].'</td></tr><tr><td><b>'.$feed['title'].'</b></td></tr><tr><td>'.$feed['message'].'</td></tr><tr><td>'.date("g:i:sa M d Y",$feed['date']).'</td></tr></table>';
}



You can see above its properly tabbed and is very easy to read. But if you view source its all 1 line.

I figure i could just do the following.

echo '<table class="w3-table w3-card-2"><tr><td>'.$stars.' '.$feed['rating'].'</td></tr><tr><td><b>'.$feed['title'].'</b></td></tr><tr><td>'.$feed['message'].'</td></tr><tr><td>'.date("g:i:sa M d Y",$feed['date']).'</td></tr></table>
	';



notice the new line on the bottom where echo ends?

Yea it reads clean when viewing source but in code this looks ugly.

Anyone got tips for doing this stuff?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP proper tabbing for clean code.

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5524
  • View blog
  • Posts: 14,526
  • Joined: 18-April 07

Re: PHP proper tabbing for clean code.

Posted 01 December 2019 - 11:40 AM

Ok a few tips here from someone who has been doing web development for nearly 20 years...

1) As with all programming, do NOT be afraid of putting in white space. It literally has no impact on speed but without it destroys readability. Readability is key. So put in a blank line or that spacing between else and a curly brace to break up code and group logical statements together. While your example may seem easy to read, I can tell you it can be better.

2) View source is nice, but don't try to make the view source perfect or immaculate. It just will drive you mad. It also doesn't matter much anymore given that dev tools in all major browsers re-build the DOM in their windows and cleans everything up for you. You will be looking at that more than the raw view source anyways these days. Besides, when you get more into the world of minification/uglifying then it will all bleed together and look ugly as hell.

3) Take advantage of string interpolation when you can. This is a tip I am actively adopting more into my own code. Instead of concatenating everything, doing something like...

echo "Your rating is {$feed['rating']} and it is super!";



Really makes it nicer than just concatenating everything into a string. Now this of course has its limitations, but it is perfect for variables, arrays and method calls/properties of objects. Plus it has some benefits when you want to stick a variable output right up against some text.

Strive for readability, then later think about performance if you need to and use view source simply to see if things are coming out valid HTML/JS/CSS. Use your dev tools for your debugging and looking at nicely formatted DOM nodes. :)
Was This Post Helpful? 0
  • +
  • -

#3 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 339
  • Joined: 13-March 18

Re: PHP proper tabbing for clean code.

Posted 01 December 2019 - 01:24 PM

do you have suggestions for DOM readibility?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,946
  • Joined: 08-August 08

Re: PHP proper tabbing for clean code.

Posted 01 December 2019 - 02:17 PM

Separate your view code from your business logic. Then you can make both readable.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5524
  • View blog
  • Posts: 14,526
  • Joined: 18-April 07

Re: PHP proper tabbing for clean code.

Posted 01 December 2019 - 02:49 PM

View PostBobby_Bubbles, on 01 December 2019 - 12:24 PM, said:

do you have suggestions for DOM readibility?


Well like I said, browsers will typically organize the DOM in the dev tools and make it pretty readable. But if you are looking for some useful tips in general, you can find some here...

https://www.w3school...tml5_syntax.asp

I will say however that their recommendation on indentation is just plain wrong. So be sure to use indentation and keep the indentation consistent. Of course the two space rule on indentation is up for debate. I use tab myself and that has been fine.

This post has been edited by Martyr2: 01 December 2019 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3070
  • View blog
  • Posts: 11,779
  • Joined: 03-December 12

Re: PHP proper tabbing for clean code.

Posted 02 December 2019 - 08:09 AM

I wou;dn't both trying to clean up what goes to the browser. Minified is typically preferable for css and js. While I understand the why you want to do it, I think you are working on cleaning up something that has little return value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1