Skip to content

Instantly share code, notes, and snippets.

@stemar
Created September 15, 2019 05:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stemar/a3475878ec75fab055da6eb7d411ef9c to your computer and use it in GitHub Desktop.
Save stemar/a3475878ec75fab055da6eb7d411ef9c to your computer and use it in GitHub Desktop.
PHP var_dump() without newline after =>
<?php
/**
* PHP var_dump() without newline after => .
*
* NOTE: The only issue is when a string value has `=>\n[ ]+`, it will get converted to `=> `
* @link https://www.php.net/manual/en/function.var-dump.php
*/
function vardump($value, $return=FALSE) {
ob_start();
var_dump($value);
$dump = ob_get_clean();
$dump = preg_replace("/=>\n[ ]+/m", '=> ', $dump);
if ((bool)$return) return $dump; else echo $dump;
}
<?php
$obj = new stdClass;
$obj->first = 'First property';
$obj->second = 'Second property';
var_dump($obj);
/*
class stdClass#2 (2) {
public $first =>
string(14) "First property"
public $second =>
string(15) "Second property"
}
*/
vardump($obj);
/*
class stdClass#2 (2) {
public $first => string(14) "First property"
public $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)
}
}
}
*/
NOTE: The only issue is when a string value has `=>\n[ ]+`, it will get converted to `=> `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment