1
PHP - Short or Broken Down Syntax?
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:
It worked perfectly fine but then I start wondering if it was more synactically correct ot break it down in steps for each function,:
These both work the same, but I personally like the first one better because it's compact.
$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.
Create an account or sign in to comment.
4
I normally space my PHP code out.
Heres the file I currently have open/just finished:
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.
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.
Really depends on how you like to program, but sometimes breaking it down, and commenting can be very useful for later.
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.
I disagree with this, because spacing and organization can be very important.