473,387 Members | 1,476 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,387 software developers and data experts.

Return Array from PHP

Hi all,

Could anyone help me about returning the array from a function in PHP? What is the right way to do it in PHP?
I've tried by reference and also returned as a local variable. Both works but is they good for big arrays??? Is they safe??


However, my current knowledge prompts me (from C++) that returning a pointer or a reference might be dangerous (this variable can go out of scope).

I appreciate for your information.

P.S. Can you also suggest any documentation about the good style of programming in PHP. I often have such questions:(.
Apr 16 '07 #1
4 4440
code green
1,726 Expert 1GB
I have often agonised over this myself being from a C background.
But by return from a function are you refering to the return value or a referenced function argument? The following is my understanding from reading various articles (sorry no bibliography).
There are no real pointers in PHP, only references that have to be looked up each time you refer to one. From a performance point, it takes as long to look up a reference as it does to make a copy. However I find that hard to believe when passing arrays or recordsets. There must be a mid point where the size of the array dictates it is better to use a reference. My instinct tells me to use references for any array but not for variables, as in C.
I would normally create an array or a string outside the function even if empty then pass it as a referenced function argument. I try to limit function returns to an integer or a boolean. But most PHP system functions seem to return array or string results and none to my knowledge accept a pointer, so is my practice the most efficient?
Apr 17 '07 #2
devsusen
136 100+
Hi,

I prefer to go through good documentation or tutorial of php. search it in google, I hope u will good ones.

I am writing a sample code to return array

[PHP]function sendArray()
{
$arr = array('test1', 'test2', 'test3', 'test4');
return $arr;
}[/PHP]

its really simple, thats why I like php :)
Apr 18 '07 #3
Motoma
3,237 Expert 2GB
Essentially there are not pointers in PHP. In my opinion the only time you will want to pass an array by reference to a function is when you want the function to modify the value of the array.
When considering the speed efficiency, you should first think about where the bottleneck in the operations are going to be. Most likely, under a scrutinizing eye, the bottleneck for any PHP application will be in the actual interpretation of the script, not the memory access and allocation (which happens in RAM, and is usually done in the MMU)
Additionally, you will not need to worry about memory management issues, as PHP is a dynamic-typed language, and will not perform any 'dangerous' operations. In fact, unless you specifically unset() a variable, it remains in memory until the end of the scripts lifetime, even if you leave the scope in which the variable was declared in.
PHP.net is the best reference by far for the inner workings of the language. I would strongly suggest you take the time to do a little reading.

Hi all,

Could anyone help me about returning the array from a function in PHP? What is the right way to do it in PHP?
I've tried by reference and also returned as a local variable. Both works but is they good for big arrays??? Is they safe??


However, my current knowledge prompts me (from C++) that returning a pointer or a reference might be dangerous (this variable can go out of scope).

I appreciate for your information.

P.S. Can you also suggest any documentation about the good style of programming in PHP. I often have such questions:(.
Apr 18 '07 #4
Thank you very much for your replies!!! They really helped me!
Apr 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
5
by: shashi59 | last post by:
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.