var_representation

(PECL var_representation >= 0.1.0)

var_representationReturns a short, readable, parsable string representation of a variable

Beschreibung

var_representation(mixed $value, int $flags = 0): string

var_representation() (from the var_representation PECL) returns a string with structured information about the given variable. It is similar to var_export() with differences in indentation, string escaping, and array representations.

Parameter-Liste

value

The variable to generate a representation of.

flags

Bitmask consisting of VAR_REPRESENTATION_SINGLE_LINE, VAR_REPRESENTATION_UNESCAPED. The behaviour of these constants is described on the var_representation constants page.

Rückgabewerte

Returns the variable representation.

Beispiele

Beispiel #1 var_representation() Examples

<?php
$a 
= [12, ['key' => 'value']];
echo 
var_representation($a), "\n";
echo 
var_representation($aVAR_REPRESENTATION_SINGLE_LINE), "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

[
  1,
  2,
  [
    'key' => 'value',
  ],
]
[1, 2, ['key' => 'value']]

Beispiel #2 Escaping control characters

<?php
echo var_representation("Content-Length: 123\r\n");

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

"Content-Length: 123\r\n"

Beispiel #3 Exporting stdClass

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

echo 
var_representation($person);

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Beispiel #4 Exporting classes

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

\A::__set_state([
  'var' => 5,
])

Beispiel #5 Using __set_state()

<?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_representation($a) . ';'); // $b = \A::__set_state([
                                              //   'var1' => 5,
                                              //   'var2' => 'foo',
                                              // ]);
var_dump($b);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch

  • var_export() - Liefert den Inhalt einer Variablen als parsbaren PHP-Code

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Arbeitszeitplan - KW und Datum werden falsch angezeigt

It looks like your problem may be related to how the current PHP code handles days and weeks. To fix, you can try updating your code as follows: l ...

Geschrieben von miajackson1117 am 16.05.2024 05:26:57
Forum: PHP Developer Forum
[Mini Vorstellung] AnoMarket

AnoMarket - Deine Anonym Markplace für Weed & legal/grauzone Service/Ware Was ist AnoMarket? Eine Anonym Markplace für Weed/Cannabis & legal/gr ...

Geschrieben von Anom am 16.05.2024 00:53:13
Forum: User pages
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hey, hallöchen! Du musst die Spiele so durchgehen, dass die Platzierung kontrolliert wechseln, dass jedes Team auf den Plätzen spielt. Hier ist ...

Geschrieben von Nico_Schubert am 14.05.2024 07:17:28
Forum: PHP Developer Forum
Bild drehen

Hallo, Du hast den Drehwinkel und den Pfad zum Bild bereits richtig im Post-Array. Hier ist ein vollständiges Beispiel, wie du das Bild drehst u ...

Geschrieben von Nico_Schubert am 14.05.2024 06:55:15
Forum: PHP Developer Forum