3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php //old code function v1($first, $last) { $result = trim($first); $lastName = trim($last); if ($lastName) { if (!empty($result)) { $result .= ','; } $result .= $lastName; } return $result; } //my function v2($first, $last) { return implode(",", [trim($first), trim($last)]); } //@Hunternnm function v3($first, $last) { if(strlen($first) > 0 && strlen($last) > 0) { return $first.",".$last; } if(strlen($first) > 0) { return $first; } else { return $last; } } //@NelinD function v4($first, $last) { return implode(',', array_filter([$first, $last], function ($string) { return trim($string); })); } function v($first, $last) { echo v1($first, $last); echo PHP_EOL; echo v2($first, $last); echo PHP_EOL; echo v3($first, $last); echo PHP_EOL; echo v4($first, $last); echo PHP_EOL; echo PHP_EOL; } v('first', 'last'); v('first', ''); v('first', ' '); v('', last); v(' ', last); v(' ', last);
based on moET9
Output for 7.2.0
first,last first,last first,last first,last first first, first first first first, first, first Warning: Use of undefined constant last - assumed 'last' (this will throw an Error in a future version of PHP) in /in/IXKBB on line 62 last ,last last last Warning: Use of undefined constant last - assumed 'last' (this will throw an Error in a future version of PHP) in /in/IXKBB on line 63 last ,last ,last last Warning: Use of undefined constant last - assumed 'last' (this will throw an Error in a future version of PHP) in /in/IXKBB on line 64 last ,last ,last last
Output for 5.4.0 - 7.1.10
first,last first,last first,last first,last first first, first first first first, first, first Notice: Use of undefined constant last - assumed 'last' in /in/IXKBB on line 62 last ,last last last Notice: Use of undefined constant last - assumed 'last' in /in/IXKBB on line 63 last ,last ,last last Notice: Use of undefined constant last - assumed 'last' in /in/IXKBB on line 64 last ,last ,last last
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/IXKBB on line 20
Process exited with code 255.