Zack
asked on
PHP Problem with retrieving variables and functions
Hi - In my PHP code I'm having problems getting variables back from the function on an include. The MySQL query works and it grabs the $ownrname as an example however when the function finishes and it continues processing the main php file the $ownrname variable is gone. I'm confused. Here is the sample code.
Thanks!
Thanks!
//main php file.
<?
if ($_GET["civil"] == "") { $civil = "FNTM"; }
include("inc/getcivil.inc.php");
getCanCivil($civil, $ownrname);
echo "<br>civil: " . $ownrname;
?>
// end of main php file
//include file
echo " running here<br>";
while($row = mysql_fetch_array($result))
{
$yrmanu = $row['yrmanu'];
$manu = $row['manu'];
$model = $row['model'];
$ownrname = $row['ownrname'] . " " . $row['ownrname2'];
$address1 = $row['addr'];
$address2 = $row['addr2'];
$city = $row['city'];
$prov = $row['prov'];
$postal = $row['postal'];
$country = $row['country'];
}
return $ownrname;
echo "<br>here:" . $ownrname;
// mysql_close($link);
}
?>
//end of include file
You can't return a value from included file. Code in the included files are executed as they are in the main file, therefore the variables you declare and alterations do in the include file is available in the main file after the include point.
ASKER
Is there anyway to share code in a separate file without using an "include" so I can retrieve the values?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so if I move the code in the include file out of the function would it work then? I'm sorry I'm not 100% sure on what you mean "You need to include the code if you need to share it."
If i understand correctly could I use the include once feature?
Thanks!
Zack
If i understand correctly could I use the include once feature?
Thanks!
Zack
Sorry, It seems I have misunderstood your question, I think there's a part missing in the code sample you posted and you include file actually carries a function called getCanCivil.
In that case forget about what I said earlier.
I see that you are passing $ownrname into the getCanCivil function and also returning a variable named $ownrname
If you want to return the value from function, call it like this:
In that case forget about what I said earlier.
I see that you are passing $ownrname into the getCanCivil function and also returning a variable named $ownrname
If you want to return the value from function, call it like this:
$ownrname = getCanCivil($civil, $ownrname);
Or, if you want the value of the variable you pass in ($ownrname) modified with in the function (same thing as above, but using by reference parameter), change the function like this:function getCanCivil($civil, &$ownrname) {
.
.
.
$ownrname = 'assing some thing here';
.
.
.
}
ASKER
Got it working thanks!
ASKER
Ahhh ok I understand now. Then $ownrname would be an array with the two values that were called in getCanCivil($a, $b), etc?
Thanks - zack
Thanks - zack
Exactly, you can pass the entire $row array like that.
ASKER
awesome! thanks!