Axel Latvala
Axel Latvala

Reputation: 576

Can I write this return in one line?

I work with databases in PHP and often fetch data from one. My question is: Can I write this in one line?

$res = mssql_fetch_assoc($result);
return $res['col'];

I have tried multiple approaches including

return (mssql_fetch_assoc($result))['col'];

and

return mssql_fetch_assoc($result)['col'];

but nothing seems to work.

Any ideas?

Upvotes: 2

Views: 85

Answers (2)

You can use:

function getvalue($array, $key)
{
  return $array[$key];
}

and then return getvalue(mssql_fetch_assoc($result),'col');

Upvotes: 1

Jon
Jon

Reputation: 437574

Only if you are on PHP >= 5.4.0, where this was implemented with the name array dereferencing.

Upvotes: 3

Related Questions