I want to aligning my output in columns. But when I try to do this I always get this:
1. FIRSTNAME1 LASTNAME1
2. FIRSTNAME2 LASTNAME2
3. FIRSTNAME3 LASTNAME3
The output I want would be like this:
1. FIRSTNAME1 LASTNAME1 D.
2. FIRSTNAME2 LASTNAME2 D.
3. FIRSTNAME3 LASTNAME3 D.
This is the code that I use:
$log = $lastname." ".$firstname.PHP_EOL;
file_put_contents('logs/'.date("Y-m-d").'.txt', $log, FILE_APPEND);
Your names have all different lengths, so if you want your "columns" to be aligned, just concatenating a few spaces won't cut it.
You need to pad that string so that the name + the spaces afterwards always add up to the same number of characters.
Fortunately for you, there is already a function to accomplish that: str_pad.
$log = strpad($lastname, 16) . ' ' . $firstname . PHP_EOL;
Obviously, you need to choose a padding length that makes sense for your data. Otherwise, any longer $lastname
will mess up your desired output.