php

How to display PHP variable values with echo, print_r, and var_dump

In this tutorial, we’re going to see some examples of how to display PHP variable values with echo(), print_r(), and var_dump().
 

Output the content of a variable by echo()

The following examples show how different variables can be displayed by echo() and which output is generated exactly. Variables of the data type integer, float, string, boolean, and array can be displayed by echo().
Note: The output usually ends with “\n”, which is the character for a line break.
 

Display Integer
<?php
    // Integer
    $int = 5;
    echo($int); // output: 5
?>

Output:

5

 

 

Display Float
<?php
    // Float
    $float = 1.5;
    echo($float); // output: 1.5
    echo("\n");
 
    // Output of float values with a comma instead of a period as a separator for the fractional parts
    echo(number_format($float, 2, ',', '.')); // output: 1,50
?>

Output:

1.5
1,50

 

Display String
<?php
    // Strings
    $str = "Lorem ipsum";
    echo($str); // output: Lorem ipsum
?>

Output:

Lorem ipsum
PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
 

Display Boolean
<?php
    // Boolean values
    // true can be echoed directly, but false does not produce any output.
    // Boolean values should therefore be converted before output,
    // true = 1 and false = 0
    $boolTrue = true;
    echo($boolTrue); // output: 1
    echo("\n");
    echo((int)$boolTrue); // output: 1
    echo("\n");
 
    $boolFalse = false;
    echo($boolFalse); // does not output anything
    echo("\n");
    echo((int)$boolFalse); // output: 0
?>

Output:

1
1

0

 

Display Array
<?php
    // Array to be displayed
    $array = array('Alex', 'Emily', 'Bob');
    echo($array[0] .', '. $array[1] .', '. $array[2]);
?>

Output:

Alex, Emily, Bob

 

Associate variables with strings to display
<?php
    // Variables may also be included in text in the output
    $var = 123;
    echo("This is some text - $var - Some more text.\n"); 
 
    // If single quotes are used, variables are not interpreted by PHP
    $var = 123;
    echo('This is some text - $var - Some more text.');
    echo("\n");
 
    // The variable must be associated explicitly with the string in order to output it
    $var = 123;
    echo('This is some text - '.$var.' - Some more text.'); 
?>

Output:

This is some text - 123 - Some more text.
This is some text - $var - Some more text.
This is some text - 123 - Some more text.
 

print_r() as an alternative to echo()

The function print_r() is comparable to echo(). This outputs strings, floats, integers and Boolean values just like echo(). And display arrays in full, including keys and values.

<?php
    print_r(123); echo("\n");
    print_r(2.525); echo("\n");
    print_r("Alex"); echo("\n");
    print_r(false); echo("\n");
    print_r(array('A'=>'Alex', 'B'=>'Bob')); echo("\n");
    print_r(null);
?>

Output:

123
2.525
Alex

Array
(
    [A] => Alex
    [B] => Bob
)

 

Display data with var_dump()

An alternative function for dumping variables is var_dump($var). This function adds information to the variable and is therefore more suitable for debugging. Output to visitors of a page should not be done with this function.

<?php
    var_dump(123);
    var_dump(2.525);
    var_dump("Alex");
    var_dump(false);
    var_dump(array('A'=>'Alex', 'B'=>'Bob'));
    var_dump(null);
?>

Output:

int(123)
float(2.525)
string(4) "Alex"
bool(false)
array(2) {
  ["A"]=>
  string(4) "Alex"
  ["B"]=>
  string(3) "Bob"
}
NULL

The number behind “string”(string(x) “abc”) indicates the character length of the string. If again nothing is output by an echo(), it is worthwhile to let var_dump() display the content of the variable. Particularly, you should pay attention if the string to be dumped is empty or does not contain any characters.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *