
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP Variable Handling var_export() Function
The PHP Variable Handling var_export() function is used to show a variable's structure. It works similarly to var_dump(), except the output is true PHP code. This means that you can copy and paste the output into your PHP scripts. The results can be printed directly or returned as a string. If you set the second parameter to true, it will output the result.
If you do not specify true, it will merely print the output. It is useful for debugging and storing variable values in a file. It supports arrays, objects, and a number of additional variables.
Syntax
Below is the syntax of the PHP Variable Handling var_export() function −
string|null var_export ( mixed $value , bool $return = false )
Parameters
Below are the parameters of the var_export() function −
$value − (Required) It is the variable you want to export.
$return − (Optional) If used and set to true, var_export() will return the variable representation instead of outputting it.
Return Value
The var_export() function returns the variable representation when the return parameter is set to TRUE. Otherwise, this function will return NULL.
PHP Version
First introduced in core PHP 4.2.0, the var_export() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
First we will show you the basic example of the PHP Variable Handling var_export() function with a basic string variable. It will directly output the variable's structured representation in valid PHP code.
<?php // Define a string variable $name = "Sonali Sharma"; // Use var_export() to display the structure var_export($name); ?>
Output
Here is the outcome of the following code −
'Sonali Sharma'
Example 2
This program uses an array in the var_export() function. It explains how to represent an array in proper PHP format. You can copy and paste the results into another script.
<?php // Define an array $numbers = [1, 2, 3, 4, 5]; // Use var_export() to display the array structure var_export($numbers); ?>
Output
This will generate the below output −
array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, )
Example 3
Now in the below code Here, var_export() function returns the output instead of printing it. We will store the result in a variable and then use file_put_contents() to save it in a file. This is useful when you want to save variable values for later use.
<?php // Define an associative array $data = ["name" => "Alia", "age" => 25, "city" => "New Delhi"]; // Store var_export() output in a variable $output = var_export($data, true); // Save the output in a file file_put_contents("/PHP/PhpProjects/newfile.php", "<?php\nreturn $output;\n?>"); echo "Content has been saved in the given file."; ?>
Output
This will create the below output −
Content has been saved in the given file.
Here is the saved content in the newfile.php −
<?php return array ( 'name' => 'Alia', 'age' => 25, 'city' => 'New Delhi', ); ?>
Example 4
In the following example, we are using the var_export() function to work with objects. It exports the object structure so it can be reused later. This is helpful for debugging or saving object data.
<?php // Define a simple class class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } // Create an object $person = new Person("Bob", 30); // Export the object var_export($person); ?>
Output
Following is the output of the above code −
\Person::__set_state(array( 'name' => 'Bob', 'age' => 30, ))