Oihana PHP

toPhpHumanReadableScalar.php

Table of Contents

Functions

toPhpHumanReadableScalar()  : string
Converts a scalar value (string, boolean, float, etc.) to a simplified, human-readable PHP string representation.

Functions

toPhpHumanReadableScalar()

Converts a scalar value (string, boolean, float, etc.) to a simplified, human-readable PHP string representation.

toPhpHumanReadableScalar(mixed $value[, string $quote = 'single' ][, bool $compact = false ]) : string

Useful when a less verbose and more natural output is desired, especially for inline dumps or generated code. Supports custom quote style for strings ('single' or 'double') and optional compact escaping (control characters and trimming).

  • Strings are quoted and escaped appropriately using formatQuotedString().
  • Booleans are represented as true or false.
  • Floats with no decimal part (e.g., 42.0) are simplified as integers (e.g., 42).
  • Null is represented as null.
  • Other types (e.g. arrays, objects) fall back to var_export() representation.
Parameters
$value : mixed

The scalar value to convert.

$quote : string = 'single'

The quote style for strings: 'single' or 'double'. Default is 'single'.

$compact : bool = false

If true, trims and escapes control characters in strings, even with single quotes.

Tags
example
echo toPhpHumanReadableScalar("hello");                        // 'hello'
echo toPhpHumanReadableScalar("He said: \"ok\"", 'double');   // "He said: \"ok\""
echo toPhpHumanReadableScalar(true);                          // true
echo toPhpHumanReadableScalar(false);                         // false
echo toPhpHumanReadableScalar(42.0);                          // 42
echo toPhpHumanReadableScalar(3.14);                          // 3.14
echo toPhpHumanReadableScalar(null);                          // null
echo toPhpHumanReadableScalar("Line\nBreak", 'single');       // 'Line
                                                            // Break'
echo toPhpHumanReadableScalar("Line\nBreak", 'single', true); // 'Line\nBreak'
see
formatQuotedString()
since
1.0.0
author

Marc Alcaraz

Return values
string

The human-readable PHP representation of the scalar value.


        
On this page

Search results