Mike Thrussell
Mike Thrussell

Reputation: 4525

Printing in a loop with echo or var_dump gives three values, but dd() only gives the first value

I have one matching row in my query.

echo $result gives 100

dd($result) gives 1

var_dump($result) gives int(1) int(0) int(0)

What's going on here?

Relevant code:

$results= DB::table('answers')->where('qid', $question->id)->where('answer', 'yes')->count();

Upvotes: 0

Views: 772

Answers (3)

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

echo print the output, and in this case output will be the count of your query as a string 100, so you use echo when printing strings

var_dump() and print_r() php functions to print objects and array, where you can not print these with echo but the var_dump() function print also the data type of keys and values in arries.

this example:

$a = array(1, 2, array("a"));
var_dump($a);
echo '<br/>------------------------------------------<br/>';
print_r($a);

and the output will be:

enter image description here

dd() is a laravel helper function relate to dump and die, you can read about this here

Upvotes: 0

Mike Thrussell
Mike Thrussell

Reputation: 4525

Turns out I'm an idiot, it's within a foreach statement and iterating 3 times.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163938

echo is just printing $result array/object vars. It's like you use toString(). Result is '1, 0, 0' which you actually see as 100.

var_dump shows you three vars inside $result. dd() shows you and object. Click arrow in browser near '1' and it will unfold contents of the $result.

Upvotes: 3

Related Questions