Jump to content

[SOLVED] Pass variables back from function


EchoFool

Recommended Posts

I am trying to create a variable in a function to pass it back out to the script that called it but it does not seem to like it... i get undefined variable but in the function the variable is set because I echo'd it.... this is generally what i did:

 

<?php
function callfunction(){
$Cheese = 1;
Echo $Cheese.'<br>';
}

callfunction();
Echo $Cheese;
?>

 

The result is this:

 

1

Notice: Undefined variable: Cheese on line 7

 

Have I done something wrong here?

 

couple things wrong here.

 

1) Cheese is a local variable to the function.  your main php page has no knowledge of this variable

2) you aren't returning any value from your function

 

<?php
function callfunction() {
$cheese = 1;
return $cheese
}

$value = callfunction();
echo $value;  // this will echo "1"
?>

couple things wrong here.

 

1) Cheese is a local variable to the function.  your main php page has no knowledge of this variable

2) you aren't returning any value from your function

 

<?php
function callfunction() {
$cheese = 1;
return $cheese
}

$value = callfunction();
echo $value;  // this will echo "1"
?>

 

What if you wanted to return multiple variables?

Ok just ran into a new problem sorry :(

 

I get this error now:

 

Warning: Missing argument 1 for travellingbuttons()

 

Warning: Missing argument 2 for travellingbuttons()

 

Warning: Missing argument 3 for travellingbuttons()

 

Warning: Missing argument 4 for travellingbuttons()

 

Warning: Missing argument 5 for travellingbuttons()

 

Warning: Missing argument 6 for travellingbuttons()

 

This is the script have added more to it:

 

<?php

function travellingbuttons($Field,$Table,$IDCode,$SizeX,$SizeY,$var){ 
$Get = mysql_query("SELECT X,Y FROM $Table WHERE UserID='{$_SESSION['Current_User']}'")
Or die(mysql_error());
$row = mysql_fetch_assoc($Get);

$YouX = $row['X'];
$YouY = $row['Y'];
    $array = array($YouX, $YouY);
    return $array;
}

travellingbuttons('Y','coords','1',50,50,'1');
$GetArray = travellingbuttons();
$YouX = $GetArray[0];
$YouY = $GetArray[1];
?>

 

What is the error telling me exactly?

Just a quick thought, try substituting these two lines

 

travellingbuttons('Y','coords','1',50,50,'1');

$GetArray = travellingbuttons();

 

with this one line :

 

$GetArray = travellingbuttons('Y','coords','1',50,50,'1')

 

One other thing - why are you passing so many arguments to the function - it doesn't appear to use all of them ??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.