Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    PHP


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Return Array From Function


I want a function that is able to return a array. But the php-manual says that you can’t.
Ex. of what I want:

function arrFunc()
{
$returarray = array(“some”,”arrray”,”stuff”);
return($returarray);}




View Complete Forum Thread with Replies

Related Forum Messages:
Function() Return Array?
If i put print instead of $output[] it works. But if I do count $output I get 0. why does it do that? i cant print $output[0] or [1] PHP Code:

View Replies !
Return Array From A Function..
I have a function which querys MySQL table i have implemented. I used to have it all in the html page however decided to make my code better and continue learning by writing this as a seperate function with parameters passed into it, and then calling the function from the html page.

The issue here however is while the function works, when i want to display parts of the data at a later stage in html page it does not work.

The code that fetches the values for displaying is in the function. While i am trying to display the info in the array inside the html page within tables.

I presume its because my function does query and then does this

$data = mysql_fetch_array( $sql ); //all inside the seperate function

and then i have to return $data as a means of passing it into the HTML page when i call the function. Therefore when i am inside the html page there is actually a $data that exists??

That is what i have deduced as being the problem however i can seem to find out how to return this $data or catch it in the html page!

View Replies !
Can I Return Array With A Function ?
i try to build a fetch_col function i give in parameter the query result and the number of the col which i want to return i fill an array with result in the function but i don't know how i can return the array i try : Code:

View Replies !
Return An Array From A Function
how should i return an array from a function?

View Replies !
Is It Possible For A Function To Return An Array?
Is it possible for a function to return an array? I've created a function that does a SELECT from the DB. Then I need to echo the returned values. Trouble is, its just the one variable that is being returned. I need all the variables to be returned. The code below does not work. -- code --

function values($ID)
{
$SqlSelectQuery =
("
SELECT
name,
filename
FROM
table
AND
ID = '$ID'
");

..................

View Replies !
Return An Array From Function
I am building a class to interface with MySQL(mostly for learning purposes). I have a function called getFirstRow(), which, as the name implies, is supposed to return an array of the first row. I am gettin gthis error when using it though. "Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ')'" Here is the class PHP Code:

class dbConTran{

    var $con;
    var $numRows;
    var $result;
    var $resultArray;
    var $firstRow = array();
    var $lastRow = array();
     
     
     // Open Database connection ---------
     function dbOpen($name,$host,$user,$password){
        $this->conn = @mysql_connect($host, $user, $password)or die ("Error Connecting to Database!");........

View Replies !
Trying To Return Mysql Array Items From Function
I am writing a calendar script which returns events, holidays etc.. from MySQL for days with events. This was easy BUT then I tried to return multiple events on some days and thats where I am stuck.

I found some examples of returning arrays from functions from functions but they all use some form of array(Ƈ',ƈ',Ɖ') and then use a for loop... I am using mysql_fetch_array so this does not seem to work... First, here is some of the code from the function where I am returning the value: PHP Code:

View Replies !
Attempting To Return Values From Array From W/in A Function
I am attempting to read through a text file & then update an HTML page
table w/ the output from the text file (ie; statusing by table).

What I want the code to do is read through the file, look for a line
that matches a couple of strings, next is loop through the file from
that match to 30 lines searching for a "score" from, then store that
score; and then look for the next algorithm (8 of them) run & score.

What I get is an array w/ only the most recent array entry.

I have tried several things; return(), array_push(), php manual, and a
more than a few days searching online for a similar example.

Code example below;

function fraction_0($file,$log) {
while (! feof($file)) {
$line = fgets($file,512);
if (ereg($_GET[what],$line) && ereg("_A_0",$line) &&
ereg("Request ID",$line)) {
print ("$log".htmlspecialchars($line)."<BR>");
for ($i30 = 1; $i30 < 30; $i30++){
$line = fgets($file,512);
if (!ereg("returned best solution",$line)){
if (ereg("Score from run", $line)){
print ("$log".htmlspecialchars($line)."<BR>");
$alg_score = explode(" ", $line, 512);
if (ereg($alg_score[6], 1)) {
$alg_array = array("alg0_1" =$alg_score[8]);
print "score for alg 1 is $alg_array[alg0_1]<BR>";
}elseif (ereg($alg_score[6], 2)) {
$alg_array = array("alg0_2" =$alg_score[8]);
} elseif (ereg($alg_score[6], 3)) {
$alg_array = array("alg0_3" =$alg_score[8]);
} elseif (ereg($alg_score[6], 4)) {
$alg_array = array("alg0_4" =$alg_score[8]);
} elseif (ereg($alg_score[6], 5)) {
$alg_array = array("alg0_5" =$alg_score[8]);
} elseif (ereg($alg_score[6], 6)) {
$alg_array = array("alg0_6" =$alg_score[8]);
} elseif (ereg($alg_score[6], 7)) {
$alg_array = array("alg0_7" =$alg_score[8]);
} elseif (ereg($alg_score[6], 8)) {
$alg_array = array("alg0_8" =$alg_score[8]);
}
print "<table border="1"><tr><th>Alg 1</th><th>Alg 2</
th><th>Alg 3</th><th>Alg 4</th><th>Alg 5</th><th>Alg 6</th><th>Alg 7</
th><th>Alg 8</th></tr>";
print "<tr><td>$alg_array[alg0_1]</td><td>
$alg_array[alg0_2]</td><td>$alg_array[alg0_3]</td><td>
$alg_array[alg0_4]</td><td>$alg_array[alg0_5]</td><td>
$alg_array[alg0_6]</td><td>$alg_array[alg0_7]</td><td>
$alg_array[alg0_8]</td></tr>";
print "</table>";
}
} else {
print ("$log".htmlspecialchars($line)."<BR>");
$i30=30;
}
}
}
}

View Replies !
Recursive Function Should Return An Array Of Results
im trying to write a recursive function in php where this function should return an array of results..here is my codes:

function comfibonacci2($limit){ //using recursive function
  if ($limit <= 1){
    return 1;
  }
  return (comfibonacci2($limit-1) + comfibonacci2($limit-2));
}

this is actually a fibonacci function. i want it to run same as its iterative function :

function comfibonacci($limit,&$fib){ //using iterative function
  $fib[0]=1;$fib[1]=1;
  for ($i=2; $i<=$limit;$i++){
    $fib[$i]=$fib[$i-1] + $fib[$i-2];
  }
}

can anyone correct my recursive codes?
thanks c",)

View Replies !
Recursive Function - How To Save The Results From All The Loops And Return Them All As An Array.
I have this recursive function that seems to get what I am looking for when I echo from the function but I don't understand how to save the results from all the loops and return them all as an array.

I tried a few ways which I removed from the code to clean it up but none of them even came close to working. Code:

View Replies !
Function Doesn't Return If "load" Is An Array Value
I have created a simple function to load an array with name / value pairs, and return the array. Most of the time it works as expected, but one particular name / value pair causes the function not to return. No errors are reported.

My system details:

OS: Windows XP SP2 ("Windows NT OFFICE 5.1 build 2600" reported by phpinfo)
Server: XAMPP for windows (apache)
PHP Version: PHP Version 5.1.4

The function: Code:

View Replies !
Function/Global Var To Return Name Of Calling Function?
I'm sure I saw this somewhere but can't remember where and can't find it now...

Is there a PHP function or global variable that will return name of the calling function? I want to do this for error reporting purposes without
having to hardcode the function name into my scripts.

Say the function is named getFunctionName(). I want to do something like...

function foo() {

View Replies !
Return $$ From Function
How can I use the variables generated by the following function in my code? They're just not being passed to the main script. I've tried global $$k; bu that did nothing PHP Code:

View Replies !
To Return A Function?
If you guys have a function that doesnt return anything, do you still include a 'return;' statement at the end? Is it good practice todo so or pointless?

View Replies !
Function Return
I have created an image resize code into a function which saves me outputing the code four times as my form has four image inputs.

Anyway at then end of this function I have an echo section that displays the image resized ( 4 in all images as function is called 4 times).

Anyway I would like to return the image name after the function is run, but not sure how to get this info. Code:

View Replies !
What Is A Return In Function ?
I know this is a wage question, but since iam new to the programming world jsut wanted to clarrify this silly doubts. Why do we need return in the function when we can use echo and get the output? Can someone explain me more abt funtions ?

View Replies !
Cannot Use Function Return Value
if(empty(trim($_POST["act_title"]))){
$error .= "<p>the title should not be empty!</p>";
}

is there anything wrong with the Sentence

if I remove the trim,it works ok.

View Replies !
Function Return()
if you have return $varname; in a function call, how do you then use that variable?

ex:

function test() {

$sql_query = 'select * from table'

return $sql_query;

}

is it possible to return a query like this? basically i want the function to simply query the database based on passed info, and have the parsing be handled by the calling page.

View Replies !
Return Value From Function
i've got a function which insert data into database. so if the transaction is successful, return a value. after that redirect user to another page. here is code:

View Replies !
Is Return In Function A Must
I have something like this

<?

$array1 = array("dir1", "dir2", "dir3");

$array2 = array("file1", "file2", "file3");

//function to do the same
function menu($haystack){
foreach ($haystack as $needle){
echo $var;
}
?>

I'm including that in my index and just echoing (echo menu($array1))

Now that function doesn't have return $var, and that worries me. Do functions always need to return something, or?

Is there maybe a better way to do this, so that i output it with simple echo, but again keep function propertie?

View Replies !
Can't Use Function Return Value
getting this error: Fatal error: Can't use function return value in write context in C:Program Filesxampphtdocsentries.php on line 96

this is the code, starting at line 95:
$tsid_rsDetails_total = "-1";
if (isset($_COOKIE('timesheetid'))) {
  $tsid_rsDetails_total = (get_magic_quotes_gpc()) ? $_COOKIE('timesheetid') : addslashes($_COOKIE('timesheetid'));
}
mysql_select_db($database_conn_org, $conn_org);
$query_rsDetails_total = sprintf("SELECT timesheet_entries.id_timesheets_main, (Sum(timesheet_entries.hours) + Sum((timesheet_entries.minutes)/60)) AS "rTotal" FROM timesheet_entries WHERE id_timesheets_main = '%s' GROUP BY timesheet_entries.id_timesheets_main ", $tsid_rsDetails_total);
$rsDetails_total = mysql_query($query_rsDetails_total, $conn_org) or die(mysql_error());
$row_rsDetails_total = mysql_fetch_assoc($rsDetails_total);
$totalRows_rsDetails_total = mysql_num_rows($rsDetails_total);

?>
any ideas?

View Replies !
Return In Function
function Adding($number) {
$number = $number+5;
return $number;
}
<br>
$number = 5;
$newnumber = Adding($number);
<br>
echo "$newnumber";

I was reading this tutorial but I never understood why and what return does.

View Replies !
Function Return Value
PHP Code:

if(strlen($_POST["room"]) > 1 && strlen($_POST["month"]) >= 1 && strlen($_POST["year"]) = 4){
include_once("calendar.inc.php");
calendar($_POST["room"], $_POST["month"], $_POST["year"]);
}

i'm getting an error: Fatal error: Can't use function return value in write context in.

View Replies !
Function To Return More Then One Value
is there anyway a function could return multiple values (like the function looks up in a recordset and returns the recordset).

View Replies !
How To Return An Image From A Function?
Currently i have a script that creates an array of data points. This array is then sent to a function to create a graph of the points in the array. My problem is this:

If i use the script as a php script on its own it will create the graph and i can display it using <img src= blah.php>.
However, i changed this so the graph script was a function but this outputs the jpeg code for the graph rather than the pic of the graph. i am putting this down to the fact that php cannot use embedded graphics (hence the reason i used <img src>).

So, i know the code actually creates the graph but i cannot figure out how to return the image to the main script to display the graph.

$pic = imagejpeg($image);
return($pic);

ie, create the pic and save it as a variable to be returned and then displayed...doesnt work!

i have also tried returning $image and then using imagejpeg($image) in the main script...which obviously doesnt work either. I have tried a couple of other things as well but they were equally unsuccesful. I reckon all this is probably as clear as mud so if any clarification is needed please ask!!

View Replies !
Function Return Values
If I assign a variable to a function, say,

$x = f($y);

and then f happens not to return anything, does $x just remain unset? Or is
this illegal?

View Replies !
Problems With Function Return Value
[color=blue]
> Help, I'm stuck! I've written the below function as part of my form data
> filtering steps and for some reason It won't return the right value![/color]

All of these lines:

$filter[] = strpos('to:', $post);

will add another array entry even if the function returns nothing, so
your end test is always true. Recode.

View Replies !
Using A Function To Return A Boolean
I am using a function in which I want to return a boolean and I want a variable set that I can use in the rest of the program.

Returning a boolean isn't much of a problem, but how can I set a variable called "$error" that can be used in the whole program? Now I only can use the variable within the function, how can I solve this?

View Replies !
Return Or Echo In Function
i have a function, but what I want to know is when should i use return? and when should i use echo?

View Replies !
SNMP Function Return
snmpget() - if no connection can be made to host a fatal error is encountered instead of returning false. How can I avoid this problem? i tried:

snmpget(...) or die (message); // still fatal error instead of message output

View Replies !
Return Function Loop
I have a function that returns all the products with the same ItemId. So I will usually have more than 1 row. The problem is I want to display the info from row one different than the rest. basically after I display the first row of product info - I just want a list of the other product names below: like:

  <td colspan="2"><?php 
for($i=0; $i<$numPd;$i++)
{
extract($product[$i]);
 echo $Name ."<br />"; 
}?></td>

but it complains that my array is now empty?? maybe because I already extracted it for the first product? Code:

View Replies !
How To Return 2 Value From Php Function Using Sajax?
does any one know how to return 2 value from php function using sajax?

function siteinfo()
{
     $one  = "one";
$two = "two";
return $one;
return $two;


}

thw above only return one to sajax

View Replies !
Return Function Variables?
how i could get access to all variables declared in the function, from outside the function. I wasn't sure if i could return more then one variabe Code:

View Replies !
How To Return Name Of Currently Executing Function?
Does there exist a php function that returns the name of the function from which it is called? i.e.:

my_function(){
$result = mystery_function();
}

value of $result is string "my_function"

View Replies !
Return $example From A Function My_function().
I'm trying to return $example from a function my_function(). I have it set up like this:
PHP Code:

function my_function() {
$example=array("key1" => "", "key2" => "");
return $example
}

my_function();
print_r ($example);

This doesn't work as it keeps complaining about an Undefined variable.

View Replies !
Function Double Return
My problem is that i need to return two things from my own function. How do i do that?

View Replies !
Return Two Arrays At The End Of A Function
I want to return two arrays at the end of a function. Is this possible? If so, is this the correct syntax:

return $array1, $array2;

View Replies !
Return MAX Value From Array
I need to return the county with the greatest score with the following long query:

View Replies !
Return Row # With Each Array Value?
Does anyone know how to output an array from a MySQL query, but tack on an incremental value to the end of each variable that represents the row number? I'm looking for something like this:

// query stuff here
// Now I output the query result:
while ($fetch = mysql_fetch_array($result)) {
echo "&quoteBody(CURRENT ROW NUMBER)=".$fetch["quoteBody"]."&quoteDate(CURRENT ROW NUMBER)=".$fetch["quoteDate"]."&quoteWriter(CURRENT ROW NUMBER)=".$fetch["quoteWriter"]."";}

Of course the (CURRENT ROW NUMBER) references are what I'm looking for. The way I have it now, PHP outputs an ampersand-delimited string representing all fields in the database. Output looks like this now:

&quoteBody=This is the body of the quote&quoteDate=08/11/01&quoteWriter=Mike&

I'd like to find out how to make it look like this:

&quoteBody1=This is the body of the quote&quoteDate1=08/11/01&quoteWriter1=Mike&

You get the idea. Anyone know how to do this?

View Replies !
About Return A Array
When I write this codes:

<?
function r_array(&$array){
for($i=1;$i<5;$i++) {
for($j=1;$j<5;$j++) {
$array[i][j] = "d";
}

}
}

r_array($array2);
print_r($array2);
echo $array[1][1];
?>

it display in the brower like this:

Array ( [i] =Array ( [j] =d ) )

And this means $array[1][1] can't be used. And what's the problem?

I want return a 2D array and use it like $array[1][1],what should I
do?

View Replies !
Return An Array
What is wrong here I need to return an array with the elements 1 thru 31.

$name = cal_days_in_month(CAL_GREGORIAN, 5, 2008);
for ($i = 1; $i <= $name; $i++)
{
$val = "$i|";
echo "$i";
}
$result = explode('|', $val);
foreach ($result as $vals)
{
echo "$vals";
}
?>
I am trying to check if an array now exsist but recieve no result, if I attempt to echo $vals[0] I get the weird result of 3, what am l doing here.

View Replies !
Ereg() Function Always Return FALSE.
I used ereg() function to verify input char to match the existing pattern. It
always return FALSE, $in_uname is in the pattern.

$min = 8;
$max = 50;
$pattern = "[[:alnum:] _-]{$min,$max}";
return ereg($pattern, $in_uname);

View Replies !
Problems With Return Statement In Function
Today I faced a problem where I am very confused and I could not
solve it and I am posting here....

My question is
Is is possible to return a value to a particular function

............

Below I have give a detailed description on why this question
appears to me.

Consider the following Code.

function one()
{
$ret_val = three();
echo "<br>RetVal:=".$ret_val;
}
function two($a,$b)
{
echo "<br>I am In function ".__FUNCTION__."()";
if($a==$b)
return true;
else
return false;
}
function three()
{
$array_one= array(1,2,3,4,5);
$array_two= array(1,2,3,4,5);
$a = array_rand($array_one,1);
$b = array_rand($array_two,1);
if(two($a,$b))
{
$ret_val = "$a and $b are equal";
echo "<br><br>I am In function ".__FUNCTION__."() and the
Val is $ret_val<br>";
return $ret_val;
}
else
{
three();
}

}

View Replies !
Problems With Arrays, Return, & Function
I can't seem to figure this out, even with the manual. All I want is to be able to return the array that is created in the function, and use it like I could inside the function.
PHP Code:

View Replies !
Function That Will Return The Url Of Every Page I Have In My Domain?
Is there a php function that will return the url of every page I have in my domain? I am creating a sitemap to send to google and I really don't want to have to copy and paste 500+ urls.

View Replies !
Return A Variable In An Existing Function.
I do not know where this function is called in the script... but can I return the $login variable somehow within this function? like could I set a global $theloginwas and record the value of $login in the function?

I am looking for a quick way to modify this function so I can read the $login variable outside of the function Code:

View Replies !
Return A Variable From Class Function
I have been working on something that should be simple but I cannot figure out how to do it. Basically, I am working with a PHP class that handles MySQL queries. The code to call a query looks like looks like:

$dbcn->db_qry("DELETE FROM contactus WHERE id='$id'","1");
This function is inside a class called db_connect.  The function is
function db_qry($qry, $type, $from="") {
$this->dbQryResult = mysql_query($qry) or $this->goError($qry, mysql_error(), $type, $from);
}

As the function shows, if the query fails, it is directed to the goError() function where it prints an error message. The problem is that some instances, I want it to instead return a variable back out of the function. The variable holds some information so I can print out the error message later in the page. In this case, it should return a variable indicating whether the query succeeded. That variable will be used in the following part: Code:

View Replies !
CURL Doesn't Return Value In Function
If someone could please tell me why the following code works:

<?php
/** URL to babelfish website to scrap **/
  $url2scrap = 'http://babelfish.yahoo.com/translate_txt'
  
  /** text to translate **/
  $tt_text = urlencode('speak');
  
  /** set variable to hold what to translate from and to **/
  $tl_value = urlencode('en_fr');
  #function babelScrap($url, $tt_text, $tl_value){
  
    $vars = 'trtext='.$tt_text.'&lp='.$tl_value;
    
    /** create cURL object **/
    $ch = curl_init($url2scrap);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    
    $return = curl_exec($ch);
    
    curl_close($ch);
    
    /** chop up return to the data we actually want **/
    $tmp = split('="result">', $return);
    $tmp2 = split('</div>', $tmp[1]);
    $tmp3 = split(">", $tmp2[0]);
    $return = $tmp3[1];
    
    echo $return;
  #}
  
  #echo babelScrap($url2scrap, $tt_text, $tl_value);
?>

But this code does not:

<?php
/** URL to babelfish website to scrap **/
  $url2scrap = 'http://babelfish.yahoo.com/translate_txt'
  
  /** text to translate **/
  $tt_text = urlencode('speak');
  
  /** set variable to hold what to translate from and to **/
  $tl_value = urlencode('en_fr');
  function babelScrap($url, $tt_text, $tl_value){
  
    $vars = 'trtext='.$tt_text.'&lp='.$tl_value;
    
    /** create cURL object **/
    $ch = curl_init($url2scrap);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    
    $return = curl_exec($ch);
    
    curl_close($ch);
    
    /** chop up return to the data we actually want **/
    $tmp = split('="result">', $return);
    $tmp2 = split('</div>', $tmp[1]);
    $tmp3 = split(">", $tmp2[0]);
    $return = $tmp3[1];
    
    return $return;
  }
  
  echo babelScrap($url2scrap, $tt_text, $tl_value);
?>

I've got to be missing something obvious. All I am doing is moving my test code into a function.

The first set of code displays parlez, which is correct. The second set of code displays a blank page.

View Replies !
Function To Return The Entire String
Anyone know how come the below outputs: "A" and not "ABCDEFGHIJK" and how to make it return the entire string not just the first character? Code:

View Replies !
XML Parsing And Function Return Values
I am attempting to create a script that will search through a directory tree, find any xml files, and update an index (in a MySQL DB) whenever the script finds a file that hasn't been previously indexed.

My problem is with the XML parse function (something I am not too familar with). Each xml file has a pair of ID tags that contain a unique number.

I want the "idCheck" function to return a true/false on whether it finds the number already listed in the DB, but the extra functions involved in parsing have me confused as to how best to do this. How would I get the return value from the "contents" func back to the script that calls "idCheck." Code:

View Replies !
Mail() Function Always Return Fails
Set up SMTP / smtp_from variable, as suggested in manual. But the mail() function always return fails. I was trying to use smtp.mail.yahoo.com I wonder if yahoo is somehow blocking smtp requests from php.

View Replies !
How To Return A 'sub-array' Of A Multidimensional One
I have a multi-dimensional associative array, and I'm interested in searching the array for a given key, and returning the 'sub-array' or value associated with this key. for example:

Code: $array = array( 'A'=>1, 'B'=>2,
'C'=>ARRAY( 'A2' => 'FRUIT', 'B2' => 'VEGGIE',
'C2' => ARRAY( 'MEAT1' => 'BEEF', 'MEAT2' => 'CHICKEN' ) ) );

I want to retrieve the array under the key 'C2'. I have tried to 'search' the array so that its internal pointer is pointing at this element, then using 'current', but that doens't work: Code:

array_key_exists( 'C2', $array );
var_dump( current( $array ) );

I've also tried a small variation of the above in order to get the reference to C2 rather than a deep copy: Code:

array_key_exists( 'C2', $array );
var_dump( $array[key($array)] );

I would actually prefer to get a reference into the array rather than a copy.. I guess I'm really stuck on the 'search' part of the mutlidimensional array.. Surely there's a better way (built-in function) than iterating every element and comparing the key?

View Replies !

Copyright © 2005-08 www.BigResource.com, All rights reserved