Last active
December 17, 2015 00:19
-
-
Save mikey179/5520225 to your computer and use it in GitHub Desktop.
Which one is better? Both functions do the same, but in slightly different ways. Interestingly, the second one takes around 4 times longer than the first one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convertToStringRepresentation1($value) | |
{ | |
$string = ''; | |
$lines = explode("\n", (string) $value); | |
foreach ($lines as $lineCounter => $line) { | |
if (empty($line)) { | |
continue; | |
} | |
if (0 != $lineCounter) { | |
$string .= ' ' . $line . "\n"; | |
} else { | |
$string .= $line . "\n"; | |
} | |
} | |
return $string; | |
} | |
function convertToStringRepresentation2($value) | |
{ | |
return join("\n ", | |
array_filter(explode("\n", (string) $value), | |
function($line) | |
{ | |
return !empty($line); | |
} | |
) | |
) . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment