Using var_dump(), print_r(), and error_log()

Using var_dump(), print_r(), and error_log()

In PHP web development, var_dump() function, print_r() function, and error_log() function are important built-in functions in web development for web source code debugging and user logging purpose. Here in PHP web development each function has its own different use and commercial purpose or output format.

Using var_dump(), print_r(), and error_log()

So let’s understand these functions better in PHP web development script.

var_dump() PHP Function.

In PHP web development var_dump() function is used to display structured information data type and variable value about an existing variable. It is specially used for source code debugging in PHP web development. As this function provides a detailed output for array, class program object, and other program complex data types.

PHP web development var_dump() syntax.

var_dump($variable);

PHP Web Development var_dump() Example.

<?php

$testVar = 12131;

$strArray = [‘Php’, ‘Javascript’, ‘Html’];

$testObject = (object) [‘webname’ => ‘Vcanhelpsu’, ’email’ => ‘Vcanhelpsu@gmail.com’, ‘contact’ => 9414];

var_dump($testVar);

var_dump($strArray);

var_dump($testObject);

?>

var_dump() Function Key Points.

In PHP, for simple data types, such as integers or strings, the var_dump() function displays the data type and data value.

PHP Web Development var_dump() Function For arrays and objects, it provides detailed information about their default structure, such as array object length, array indexing, and the data types of array elements or object properties.

print_r() PHP Function.

The print_r() Function in PHP Web Development The var_dump() function is a basic option for outputting human-readable information about variables, particularly arrays and array objects, in a format that is readable. It does not display the data type or length of array elements, as the var_dump() function does.

print_r() PHP Function syntax.

print_r($variable);

print_r() PHP Function Example.

<?php

$litsArray = [‘Desktop’, ‘Laptop’, ‘Notebook’];

$sampleObject = (object) [‘laptop_name’ => ‘Apple Macbook Pro’, ‘price’ => 1000];

print_r($litsArray);

print_r($sampleObject);

?>

print_r() PHP Function Key Points.

  • Arrays – In PHP web development, the print_r() function displays more detailed, readable, program output than the var_dump() function.
  • Objects – In PHP web development, it prints and previews the public properties of class objects.

In PHP web development, the print_r() function is often used to debug and program output to quick, readable source code, especially if you are working with array data types in PHP web development.

error_log() PHP Function.

In PHP web development, the error_log() function is used to send error message information to the PHP source code error log or a specific indicated file. This is an official method of logging information to the web user, particularly for logging debug information in program error management or source code production environments where you do not want to display the error directly to the web user.

error_log() PHP Function syntax.

error_log(“Write desire message here”, [message_type], [destination], [extra_headers]);

  • message_type – This indicates where in the current file the error log message should be sent, the default for PHP’s error log is 0.
  • destination – The file or email address to send the log message to, if message_type is set to 1 or 3.        
  • extra_headers – This is used with message_type 1 to send extra headers when sending to the current email.

Example of logging to the PHP error log (default).

<?php

$sampleVar = 9414;

error_log(“\n the debug value of sample variable – ” . $sampleVar);

?>

This will log the message to the PHP error log file. E.g., /var/log/apache/error.log on many systems or wherever your PHP error log path is configured.

Example of logging to a custom file.

<?php

$stringArray = [‘C’, ‘C++’, ‘Java’, ‘Javasript’];

error_log(print_r($stringArray, true), 1, ‘/path/to/your/logfile1.log’);

Here in this example, the output of print_r($myArray, true) is sent to a custom log file (logfile1.log).

var_dump() function, print_r() function, and error_log() function key points.

  • Logging errors – In PHP web development, the error_log() function is specifically used to log PHP errors or important information, which is more helpful while debugging in a live development environment.
  • Custom log file – You can indicate a custom log file in PHP web development by using 2 as the first argument and providing the file path as the second argument.

When to use each function in PHP web development.

var_dump(): Function.

Most advanced used during code development in PHP web development, when you need detailed information about webpage source variable data types and values. It is more compatible for complex data structures like arrays and array class objects in PHP web development.

print_r(): Function.

Use this function when you want an easy, human readable-version of a variable content in PHP web development.

When you don’t need data type description in a program, arrays and array objects are a great option.

error_log(): Function.

This is the ideal function for logging errors, source code debug information or any type of runtime data in PHP web development.

This is a better choice for use in a source code production environment in PHP web development, especially if you do not want to display internal details to the end user.

With this function, you can log information to a custom file or to a system error log.

var_dump() function, print_r() function, and error_log() function Conclusion.

  • Use the var_dump() function for detailed variable information in PHP web development. Especially during complex or large web page development phases.
  • Use the print_r() function for easier, more readable output in PHP web development. Especially, when you need to handle arrays or objects in web development.
  • Use the error_log() function in PHP web development to log errors or important runtime data to files or logs, and display program source code debugging separately.