By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

function call as an array

P: n/a
Hi all,

I'm after the PHP equivlent to this Perl construct:

print @{[someFunctionCall()]}[0];

In the above example someFunctionCall() would return an array, and we're
printing the first item from the returned array..

How do I do this in PHP ? (without first assigning the output of the
function call to a variable)

Chris
Apr 25 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Skeleton Man wrote:
Hi all,

I'm after the PHP equivlent to this Perl construct:

print @{[someFunctionCall()]}[0];

In the above example someFunctionCall() would return an array, and we're
printing the first item from the returned array..

How do I do this in PHP ? (without first assigning the output of the
function call to a variable)

Chris

Chris,

I don't think you can.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 25 '07 #2

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:ws******************************@comcast.com. ..
| Skeleton Man wrote:
| Hi all,
| >
| I'm after the PHP equivlent to this Perl construct:
| >
| print @{[someFunctionCall()]}[0];
| >
| In the above example someFunctionCall() would return an array, and we're
| printing the first item from the returned array..
| >
| How do I do this in PHP ? (without first assigning the output of the
| function call to a variable)
| >
| Chris
| >
| >
|
| Chris,
|
| I don't think you can.

currently, you can't in php. you have to return the full array to a variable
and then access the element(s).
Apr 25 '07 #3

P: n/a
news:v7******************************@wightman.ca. ..
Hi all,

I'm after the PHP equivlent to this Perl construct:

print @{[someFunctionCall()]}[0];

In the above example someFunctionCall() would return an array, and we're
printing the first item from the returned array..

How do I do this in PHP ? (without first assigning the output of the
function call to a variable)
If it indeed is the first element, then you can use reset (and for the last
element end) but there is no such functionality for returning the n'th
element.

echo reset(functioncall());

http://fi2.php.net/manual/en/function.reset.php
reset() rewinds array's internal pointer to the first element and returns
the value of the first array element, or FALSE if the array is empty.
--
Ra*********@gmail.com

"Good tea. Nice house." -- Worf
Apr 25 '07 #4

P: n/a
Rami Elomaa wrote:
If it indeed is the first element, then you can use reset (and for the last
element end) but there is no such functionality for returning the n'th
element.

echo reset(functioncall());
If you need the nth item, it's fairly easy to write your own function to
do this:

function array_n($array, $n) { return $array[$n]; }

echo array_n(functioncall(), 2);

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 25 '07 #5

P: n/a
Skeleton Man wrote:
print @{[someFunctionCall()]}[0];
PS -- this sort of thing is generally unsafe, as you've not checked that
the returned array even *has* an element with index 0. someFunctionCall
might be defined as:

function someFunctionCall ()
{
if (some error condition)
return FALSE;
else
return array(etc, etc, etc);
}

You should really check that someFunctionCall has returned an array, and
that the array has an element 0 before you try to print element 0 out!

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 25 '07 #6

P: n/a
>You should really check that someFunctionCall has returned an array, and
>that the array has an element 0 before you try to print element 0 out!
I know it will always return an array with at one least element.. but
that's besides the point really..

Chris
Apr 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.