Serghei Pogor
Serghei Pogor

Reputation: 73

display function results as array

I am trying to set array 'status' as function results

<?php
function GetTags(){
	echo "#php, #html";
}

$params = array(
	'status' =>GetTags()
);

print_r ($params);

I need array 'status' to be "#php, #html" Tried a lot of combination with " and ' but without any success What I am missing here, anybody to help me?

Upvotes: 0

Views: 48

Answers (1)

ishegg
ishegg

Reputation: 9927

You can see from the result that you're getting that with your GetTags() function you're actually echoing #php, #html (which doesn't return anything so you get an empty string in the value). What you need to do is return it from the function so it will be added as status' value.

Your way:

function GetTags() {
    echo "#php, #html";
}

$params = array(
    'status' =>GetTags()
);

print_r ($params);

Result

#php, #htmlArray ( [status] => )

Notice how the text is first printed on the screen, and then you get no value in status?

Now, returning instead of echoing:

function GetTags() {
    return "#php, #html";
}

$params = array(
    'status' =>GetTags()
);

print_r ($params);

Result

Array ( [status] => #php, #html )

Upvotes: 1

Related Questions