var_export

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

var_exportMostra ou retorna uma representação estruturada de uma variável

Descrição

var_export ( mixed $expression [, bool $return ] ) : mixed

var_export() obtém informação estruturada sobre uma dada variável. Ela é similar a var_dump() com uma exceção: a representação retornada é um código PHP válido.

Parâmetros

expression

A variável que você quer exportar.

return

Se usado e definido para true, var_export() irá retornar a representação da variável ao invés de exibí-la.

Nota:

Esta função usa o interno buffer de saída com este parâmetro, então assim não pode ser usado dentro de uma função de callback para ob_start().

Valor Retornado

Retorna a representação da variável quando o parâmetro return é usado e avaliado para true. Caso contrário, esta função retornará null.

Notas

Nota:

Esta função usa o interno buffer de saída com este parâmetro, então assim não pode ser usado dentro de uma função de callback para ob_start().

Changelog

Versão Descrição
7.3.0 Now exports stdClass objects as an array cast to an object ((object) array( ... )), rather than using the nonexistent method stdClass::__setState(). The practical effect is that now stdClass is exportable, and the resulting code will even work on earlier versions of PHP.

Exemplos

Exemplo #1 var_export() Examples

<?php
$a 
= array (12, array ("a""b""c"));
var_export($a);
?>

O exemplo acima irá imprimir:

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

$b 
3.1;
$v var_export($btrue);
echo 
$v;

?>

O exemplo acima irá imprimir:

3.1

Exemplo #2 Exporting stdClass (since PHP 7.3.0)

<?php
$person 
= new stdClass;
$person->name 'ElePHPant ElePHPantsdotter';
$person->website 'https://php.net/elephpant.php';

var_export($person);

O exemplo acima irá imprimir:

(object) array(
   'name' => 'ElePHPant ElePHPantsdotter',
   'website' => 'https://php.net/elephpant.php',
)

Exemplo #3 Exporting classes (since PHP 5.1.0)

<?php
class { public $var; }
$a = new A;
$a->var 5;
var_export($a);
?>

O exemplo acima irá imprimir:

A::__set_state(array(
   'var' => 5,
))

Exemplo #4 Using __set_state() (since PHP 5.1.0)

<?php
class A
{
    public 
$var1;
    public 
$var2;

    public static function 
__set_state($an_array)
    {
        
$obj = new A;
        
$obj->var1 $an_array['var1'];
        
$obj->var2 $an_array['var2'];
        return 
$obj;
    }
}

$a = new A;
$a->var1 5;
$a->var2 'foo';

eval(
'$b = ' var_export($atrue) . ';'); // $b = A::__set_state(array(
                                            //    'var1' => 5,
                                            //    'var2' => 'foo',
                                            // ));
var_dump($b);
?>

O exemplo acima irá imprimir:

object(A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}

Notas

Nota:

Variables of type resource couldn't be exported by this function.

Nota:

var_export() does not handle circular references as it would be close to impossible to generate parsable PHP code for that. If you want to do something with the full representation of an array or object, use serialize().

Aviso

When var_export() exports objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility.

Nota:

To be able to evaluate the PHP generated by var_export(), all processed objects must implement the magic __set_state method. The only exception is stdClass, which is exported using an array cast to an object.

Veja Também

  • print_r() - Imprime informação sobre uma variável de forma legível
  • serialize() - Generates a storable representation of a value
  • var_dump() - Mostra informações sobre a variável