1

PHP - Short or Broken Down Syntax?

[] Xero []'s Avatar [] Xero []6/2/14 11:26 pm
1 emeralds 269 4
6/3/2014 12:52 am
4377699683's Avatar 4377699683
Hello, just few minutes ago I was making this variable in PHP, it indexes the directory and sorts the array, removes some entries, ect... But what I did was clump a bunch of the functions and arrays together:
$folderIndex = array_reverse(array_values(array_diff(scandir(getcwd()), [".", "..", "home.php", "Home.php", "non-listed"])))

It worked perfectly fine but then I start wondering if it was more synactically correct ot break it down in steps for each function,:

$getDirItems = scandir(getcwd());
$removeUnwanted = array_diff($getDirItems, [".", "..", "home.php", "Home.php", "non-listed"]);
$fixCount = array_values($removeUnwanted);
$folderIndex = array_reverse($fixCount);


These both work the same, but I personally like the first one better because it's compact.
Posted by [] Xero []'s Avatar
[] Xero []
Level 1 : New Miner
1

Create an account or sign in to comment.

4

4377699683
06/03/2014 12:52 am
Level 25 : Expert Network
4377699683's Avatar
I normally space my PHP code out.
Heres the file I currently have open/just finished:
<?php

if(!empty($_GET['u'])){

$name['normal'] = basename($_GET['u']);
$name['lower'] = strtolower(basename($_GET['u']));
$file = json_decode(file_get_contents("test.json"), true);



$img = imagecreatefrompng("sig1.png");
$background = imagecolorallocate( $img, 255, 255, 255 );
$text_color = imagecolorallocate( $img, 0, 0, 0 );
imagettftext( $img, 19, 0, 45, 76, $text_color, "Baron Neue Bold.ttf", $file[$name['lower']]['blames']);



header( "Content-type: image/png" );
imagepng( $img );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $img );

}
else{
$img = imagecreatefrompng("sig1.png");
$background = imagecolorallocate( $img, 255, 255, 255 );
$text_color = imagecolorallocate( $img, 0, 0, 0 );
imagettftext( $img, 19, 0, 45, 76, $text_color, "Baron Neue Bold.ttf", '0');



header( "Content-type: image/png" );
imagepng( $img );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $img );

}
?>


You can see that I enter/space everything out. I don't really comment much, but I do if I think i'm gonna be coming back to the file and doing anything else to it.

It doesn't really matter if you're the only person who is gonna see your code, but it is good practice to comment/space stuff out.
1
planetminecrafter52342
06/03/2014 12:34 am
Level 17 : Journeyman Zombie
planetminecrafter52342's Avatar
Really depends on how you like to program, but sometimes breaking it down, and commenting can be very useful for later.
1
Swimmer1929
06/02/2014 11:40 pm
Level 40 : Master Taco
Swimmer1929's Avatar
Like pretty much every other programming language out there, the spacing and organization doesn't really matter as long as the same code is there. It's really up to you.
1
[] Xero []
06/03/2014 12:44 am
Level 1 : New Miner
[] Xero []'s Avatar
I disagree with this, because spacing and organization can be very important.
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome