PHP Manual

PHP function var_export()

07. 10. 2019

Obsah článku

Available from: `PHP 4.2.0`

This function returns a string that represents the inserted variable as if it were written in a PHP script. The output can be used directly for a PHP parser.

Parameters

Parameter Data type Default value Note
$expression mixed not Variable with the data we want to export.
$return bool null If true, the function returns the value of the parameter instead of using it directly.

Return values

mixed

This function returns the representation of the inserted variable if the second parameter was true.

If the second parameter was false, the function always returns null.

Example of dumping an array

Let's have an input:

$a = [1, 2, ['a', 'b', 'c']];
var_export($a);

Returns:

array (
  0 => 1,
  1 => 2,
  2 =>
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)

Dumping more complex objects and formatting

Often you need to dump (export) objects with a very useful structure, or even generate an entire PHP class according to a specification.

The PhpGenerator package, which is directly part of the Nette framework, is a good way to do this.

It is easy to use:

$class = new Nette\PhpGenerator\ClassType('Demo');
$class
->setFinal()
->setExtends('ParentClass')
->addImplement('Countable')
->addTrait('Nette\SmartObject')
->addComment("Class description.\nSecond row\n")
->addComment('@property-read Nette\Forms\Form $form');
// you can generate the code simply by rewriting it to a string or using echo:
echo $class;

Generates:

/**
* Class description
* Second line
*
* @property-read Nette\Forms\Form $form
*/
final class Demo extends ParentClass implements Countable
{
use Nette\SmartObject;
}

Dumping constants and arrays in variables:

$class->addConstant('ID', 123);
$class->addProperty('items', [1, 2, 3])
->setVisibility('private')
->setStatic()
->addComment('@var int[]');

Generates:

const ID = 123;

/** @var int[] */
private static $items = [1, 2, 3];

See official documentation for more examples.

Other resources

[Official var-export documentation](- Official var_export manual)

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

1.

V jiných jazycích

2.
Status:
All systems normal.
2024