Redaldo
Redaldo

Reputation: 19

Var_dump with echo output

When using var_dump with a function that have an echo like :

<?php 

function foo()
{
  echo 'Hello';
}

var_dump (foo());

?>

the output is:

HelloNULL

I want to know where the NULL came from

Upvotes: 1

Views: 1765

Answers (3)

A.A Noman
A.A Noman

Reputation: 5270

var_dump always shows the variable type like int or string or etc

When you call the function foo() and has no return type then print Hello and var_dump declares foo() is NULL since it has no return type.

<?php 
    function foo(){
        echo 'Hello';
        //return 'StackOverFlow';
    }
    var_dump(foo());
?>

Look this second one

<?php
    function foo2(){
    }
    var_dump(foo2()); 
?>

output=>NULL

That means var_dump can't declare what type of variable function foo2()

Upvotes: 0

Kowsigan Atsayam
Kowsigan Atsayam

Reputation: 453

Simply you can return the value in the function foo(). Or just use print_r to print the foo() value.

<?php 
function foo()
{
  echo 'Hello';
}
print_r (foo());
?>

The output will be Hello.

Upvotes: 0

scikid
scikid

Reputation: 7

You must set the return the value of the function.

function foo()
{
  return 'Hello';
}

var_dump (foo());

Then if you want to retrieve the value of the function just do:

echo foo();

Upvotes: 1

Related Questions