Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.