Last active
March 27, 2026 19:34
-
-
Save stemar/a3475878ec75fab055da6eb7d411ef9c to your computer and use it in GitHub Desktop.
PHP var_dump() without newline after =>
This file contains hidden or 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
| <?php | |
| /** | |
| * PHP var_dump() without newline after => . | |
| * | |
| * NOTE: The only issue is when a string value has exactly `]=>\n\s+`, it will get converted to `] => ` | |
| * @link https://www.php.net/manual/en/function.var-dump.php | |
| */ | |
| function vardump(mixed $value, bool $print = TRUE) { | |
| ob_start('mb_output_handler'); | |
| var_dump($value); | |
| $dump = ob_get_clean(); | |
| $dump = preg_replace("/]=>\n\s+/", "] => ", $dump); | |
| if (!$print) return $dump; | |
| print $dump; | |
| } |
This file contains hidden or 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
| <?php | |
| $obj = new stdClass; | |
| $obj->first = 'First property'; | |
| $obj->second = 'Second property'; | |
| var_dump($obj); | |
| /* | |
| object(stdClass)#1 (2) { | |
| ["first"]=> | |
| string(14) "First property" | |
| ["second"]=> | |
| string(15) "Second property" | |
| } | |
| */ | |
| vardump($obj); | |
| /* | |
| object(stdClass)#1 (2) { | |
| ["first"] => string(14) "First property" | |
| ["second"] => string(15) "Second property" | |
| } | |
| */ | |
| $arr = ['first'=>'First value', 'second'=>"Second | |
| 'multiline']=> | |
| value", 'null'=>NULL, 'array'=>[2,3,[4,5]]]; | |
| var_dump($arr); | |
| /* | |
| array(4) { | |
| ["first"]=> | |
| string(11) "First value" | |
| ["second"]=> | |
| string(31) "Second | |
| 'multiline']=> | |
| value" | |
| ["null"]=> | |
| NULL | |
| ["array"]=> | |
| array(3) { | |
| [0]=> | |
| int(2) | |
| [1]=> | |
| int(3) | |
| [2]=> | |
| array(2) { | |
| [0]=> | |
| int(4) | |
| [1]=> | |
| int(5) | |
| } | |
| } | |
| } | |
| */ | |
| vardump($arr); | |
| /* | |
| array(4) { | |
| ["first"] => string(11) "First value" | |
| ["second"] => string(31) "Second | |
| 'multiline'] => value" | |
| ["null"] => NULL | |
| ["array"] => array(3) { | |
| [0] => int(2) | |
| [1] => int(3) | |
| [2] => array(2) { | |
| [0] => int(4) | |
| [1] => int(5) | |
| } | |
| } | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment