473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

function call as an array

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
6 1603
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

"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
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
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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
17
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
3
by: almo | last post by:
hi guys, I have question about writing a function that will store some data from a file in an array, then when that function is call in main, I can use that array in the main() for calculation. Here...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.