function won't pass variables when called via include()

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

function won't pass variables when called via include()

Postby bezmond » Wed Aug 27, 2003 12:35 pm

I have a script that uses include() at the top to include a common.php file which will include a lot of functions I wish to use in my site and will use in more than one script.

My page reads as follows:

Code: Select all

<?
include("config.php");
include("common.php");
$id = $_GET["id"];
getpassportdetails($id);
?>


My function works correctly, and I have checked that by echoing the variables within the function.

However, when I try to echo one of the variables within the acutal page, they are blank... a little help would be greatly appreciated!!

Regards,
Andrew

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: function won't pass variables when called via include()

Postby swirlee » Wed Aug 27, 2003 12:54 pm

bezmond wrote:However, when I try to echo one of the variables within the acutal page, they are blank...


Within which page? The included page, or the calling page? Are they variables from the main program flow, or are they from within a function? Where are you trying to echo them -- from a function, or in the main program flow? Are they blank, or are they zero or false or null or just not set?

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed Aug 27, 2003 12:58 pm

the variables are pulled from the database via the function which is stored in common.php. The function is called by the page "passport.php?id=x" - this page calls the function, then tries to echo the variables that the function retrieves from the database.

I will post the function if you wish.

Andrew

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Aug 27, 2003 1:03 pm

bezmond wrote:I will post the function if you wish.


Yes, that would be good. If you could post a bit of context, too (e.g. the part of the script that calls the function, etc.), that'd be helpful.

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed Aug 27, 2003 1:06 pm

the source for the function and the page that will display it are available as follows:

http://www.nvas.net/new/passport.txt
and http://www.nvas.net/new/common.txt

the result: http://www.nvas.net/new/passport.php?id=100001

Andrew

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Aug 27, 2003 2:30 pm

Ah. Just as I suspected. When you manipulate a variable inside a function (as you do with getpassportdetails()), the variables only exist within the scope of that function. They can only be accessed within that function, and once the function has finished executing, they're destroyed. The documentation on Variable scope is a good starting point to understanding this, but, as per usual, I'll give an example:

Code: Select all

<?
$a = 'swirlee';

function foo() {
   $a = 'bezmond';
   return $a;
}

foo();
echo $a;
/* output:
      swirlee
*/

echo foo();
/* output:
      bezmond
*/
?>


As you can see, $a is a completely different variable outside the function as it is inside. There are a number of ways to remedy this:

Code: Select all

<?
// 1. Just return the new value and assign it to $a
function foo1() {
   return 'bezmond';
}

$a = 'swirlee';
$a = foo1();
echo $a;
/* output:
      bezmond
*/

//// 2. Pass the variable by reference (see below)
function foo2(&$a) {
   $a = 'bezmond';
}

$a = 'swirlee';
foo2();
echo $a;
/* output:
      bezmond
*/

//// 3. Use global to access the variable from outside the scope of the function
function foo3() {
   global $a;
   $a = 'bezmond';
}

$a = 'swirlee';
foo3();
echo $a;
/* output:
      bezmond
*/
?>


So, lots of ways to do it. Concerning pass-by-reference, it's a potentially confusing topic, and there's a whole chapter about itin the documentation, so I'll just refer you to that.
Last edited by swirlee on Wed Aug 27, 2003 2:31 pm, edited 1 time in total.

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed Aug 27, 2003 2:32 pm

I think I'll go with option 3 ;)

Thanks for the help Swirlee!!

Andrew

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Aug 27, 2003 2:44 pm

bezmond wrote:I think I'll go with option 3 ;)


By the way, if you only want to get one row, you don't have to put the mysql_fetch_array in a loop. Also, it may be easier to just return the whole row, or declare the whole row as global, instead of individually doing it to each column.

Code: Select all

<?
function getpassportdetails($id) {
   global $row;
   $query = "select * from members where id='$id'";
   $result = mysql_query($query) or die(mysql_error());
   $row = mysql_fetch_array($result);
}

$id = 100001; // why do your IDs start at 100000? Just curious.
getpassportdetails($id);

echo $row['country']
// and so on...
?>


OR

Code: Select all

<?
function getpassportdetails($id) {
   $query = "select * from members where id='$id'";
   $result = mysql_query($query) or die(mysql_error());
   return mysql_fetch_array($result);
}

$id = 100001;
$row = getpassportdetails($id);

echo $row['country']
// and so on...
?>


Just some possibilities.
Last edited by swirlee on Wed Aug 27, 2003 7:39 pm, edited 1 time in total.

User avatar
tryton
New php-forum User
New php-forum User
Posts: 49
Joined: Wed Jul 09, 2003 8:25 am
Location: Joze
Contact:

Postby tryton » Wed Aug 27, 2003 7:33 pm

Damn, the boy/girl is good.

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Aug 27, 2003 7:38 pm

I prefer "man", but "guy" will do. And thanks. I was on a roll. I just hope bezmond agrees. :)

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed Aug 27, 2003 11:11 pm

yep I do... I've got to re-make a site I already did in PHP once, but with a new design, and re-make of the PHP to get rid of all my old bugs (I was new to much of the coding I used on that site).

Thanks for the help Swirlee, I know who to send the free beer's to now ;o))

Andrew

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed Aug 27, 2003 11:13 pm

oh, and ID's start at 100000 because the server software for a program that is used, is set to use a 6 digit ID number. So the founding staff get 10000x, and the regular members get 2xxxxx.

Andrew


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 6 guests