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.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

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

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

Seltsames Verhalten von execute() oder Fehler meinerseits

Hello You can check below steps - Check for errors after executing the statement. Bind parameters to placeholders in the SQL query. Confirm t ...

Geschrieben von nolanmaris am 20.05.2024 12:12:16
Forum: SQL / Datenbanken
Ein Problem mit der Lampe

Yes ..i was facing the same issue .

Geschrieben von nolanmaris am 20.05.2024 12:02:55
Forum: Off-Topic Diskussionen
Hello

Hello , I am new member here. I have around 8years of experience in developing User Interfaces for Mobile/Tablet/Desktop using HTML, CSS, JavaScri ...

Geschrieben von nolanmaris am 20.05.2024 12:01:22
Forum: User stellen sich vor
Daten einer Abfrage mit Hilfe von Thumbnails nebeneinander ausgeben

Warum soll ich float meiden? Siehe https://www.linkedin.com/pulse/css-float-vs-grid-flexbox-sachin-tiwari Falls es mit Englisch nicht so klappt: ...

Geschrieben von scatello am 18.05.2024 05:57:56
Forum: PHP Developer Forum