fork download
  1. <?php
  2.  
  3. function my_var_export($var, $text = '...', $output_type = TRUE) {
  4.  
  5. if (gettype($var) === 'string') {
  6. $var = htmlentities($var);
  7. }
  8. return $text . ($output_type ? ' (' . gettype($var) . ')' : '') . ':'. PHP_EOL
  9. . var_export($var, TRUE) . PHP_EOL . PHP_EOL;
  10. }
  11.  
  12.  
  13. $myObject = new stdClass();
  14. $myObject->foo = 'bar';
  15. $myObject->asd = 'blabla';
  16. $copyObject = $myObject;
  17.  
  18. echo my_var_export($myObject, '$myObject');
  19. echo my_var_export($copyObject, '$copyObject');
  20.  
  21. function stuff($anObject){
  22. $anObject->foo = 'asdasd';
  23. }
  24.  
  25. stuff($copyObject);
  26. echo my_var_export($myObject, '$myObject after calling stuff()');
  27. echo my_var_export($copyObject, '$copyObject after calling stuff()');
  28.  
  29. $copyObject->foo = 'bizbaz';
  30.  
  31. echo my_var_export($myObject, '$myObject after $copyObject->foo = "bizbaz"');
  32. echo my_var_export($copyObject, '$copyObject after $copyObject->foo = "bizbaz"');
  33.  
  34. $copyObject->anotherfoo = 'háblüháblü';
  35. echo my_var_export($myObject, '$myObject after $copyObject->foo = "bizbaz"');
  36. echo my_var_export($copyObject, '$copyObject after $copyObject->foo = "bizbaz"');
  37.  
  38. $copyObject2 = clone $copyObject;
  39. $copyObject2->anotherfoo = 'hiblihubli';
  40.  
  41. echo my_var_export($copyObject, '$copyObject after cloning');
  42. echo my_var_export($copyObject2, '$copyObject2 after cloning');
  43.  
  44. echo '_________________________________________________________________________'.PHP_EOL;
  45.  
  46. $array = array('asd', 'foo', 'bla');
  47. echo my_var_export($array, '$array');
  48.  
  49. function arrayMod($arr){
  50. $arr[0] = 'qweqwe';
  51. }
  52.  
  53. arrayMod($array);
  54. echo my_var_export($array, '$array after calling arrayMod()');
  55.  
  56. function arrayModRef(&$arr){
  57. $arr[0] = 'qweqwe';
  58. }
  59.  
  60. arrayModRef($array);
  61. echo my_var_export($array, '$array after calling arrayModRef()');
  62.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
$myObject (object):
stdClass::__set_state(array(
   'foo' => 'bar',
   'asd' => 'blabla',
))

$copyObject (object):
stdClass::__set_state(array(
   'foo' => 'bar',
   'asd' => 'blabla',
))

$myObject after calling stuff() (object):
stdClass::__set_state(array(
   'foo' => 'asdasd',
   'asd' => 'blabla',
))

$copyObject after calling stuff() (object):
stdClass::__set_state(array(
   'foo' => 'asdasd',
   'asd' => 'blabla',
))

$myObject after $copyObject->foo = "bizbaz" (object):
stdClass::__set_state(array(
   'foo' => 'bizbaz',
   'asd' => 'blabla',
))

$copyObject after $copyObject->foo = "bizbaz" (object):
stdClass::__set_state(array(
   'foo' => 'bizbaz',
   'asd' => 'blabla',
))

$myObject after $copyObject->foo = "bizbaz" (object):
stdClass::__set_state(array(
   'foo' => 'bizbaz',
   'asd' => 'blabla',
   'anotherfoo' => 'háblüháblü',
))

$copyObject after $copyObject->foo = "bizbaz" (object):
stdClass::__set_state(array(
   'foo' => 'bizbaz',
   'asd' => 'blabla',
   'anotherfoo' => 'háblüháblü',
))

$copyObject after cloning (object):
stdClass::__set_state(array(
   'foo' => 'bizbaz',
   'asd' => 'blabla',
   'anotherfoo' => 'háblüháblü',
))

$copyObject2 after cloning (object):
stdClass::__set_state(array(
   'foo' => 'bizbaz',
   'asd' => 'blabla',
   'anotherfoo' => 'hiblihubli',
))

_________________________________________________________________________
$array (array):
array (
  0 => 'asd',
  1 => 'foo',
  2 => 'bla',
)

$array after calling arrayMod() (array):
array (
  0 => 'asd',
  1 => 'foo',
  2 => 'bla',
)

$array after calling arrayModRef() (array):
array (
  0 => 'qweqwe',
  1 => 'foo',
  2 => 'bla',
)