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!

 
//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

Open in new window

LVL 4
zack4x4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sudaraka WijesingheWeb Application ProgrammerCommented:
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.
0
zack4x4Author Commented:
Is there anyway to share code in a separate file without using an "include" so I can retrieve the values?
0
Sudaraka WijesingheWeb Application ProgrammerCommented:
You need to include the code if you need to share it. If you don't want to execute the code each time you include it, move you code in the include file to a function (that returns a value) and call the function from the main file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

zack4x4Author Commented:
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
0
Sudaraka WijesingheWeb Application ProgrammerCommented:
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:
$ownrname = getCanCivil($civil, $ownrname);

Open in new window

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';
	.
	.
	.
}

Open in new window

0
zack4x4Author Commented:
Got it working thanks!
0
zack4x4Author Commented:
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
0
Sudaraka WijesingheWeb Application ProgrammerCommented:
Exactly, you can pass the entire $row array like that.
0
zack4x4Author Commented:
awesome! thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.