iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-30-2007, 07:07 AM
WebProWorld Pro
 
Join Date: Jul 2003
Location: Canada
Posts: 272
ackerley1 RepRank 0
Default Using Variables outside a function

I am trying to create function that will assign several variables during it's process, but I cannot seem to get those variables to display outside of the function.

function example ($input_var) {
if ($input_var == option1) {
$new_var = new_desired_output1;
}
else {
$new_var = new_desired_output2;
}
}


Then I define $input_var and have a call to the function in the page:
$input_var = some_var;
example ($input_var);

Then later in the page:
echo $new_var;

But the last bit does not work?

Is there a trick to get the new variable out of the function? I can get it to work inside the function, but not outside??

Thanks
__________________
Rob
Reply With Quote
  #2 (permalink)  
Old 10-30-2007, 09:22 AM
DaveSawers's Avatar
WebProWorld Veteran
 
Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 492
DaveSawers RepRank 3DaveSawers RepRank 3
Default Re: Using Variables outside a function

Put the new_var into the argument list and pass it by reference, e.g.

function example ($input_var, &$new_var) {
...
}

$input_var = some_var;
$new_var = "";
example($input_var, $new_var);
echo $new_var;
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
  #3 (permalink)  
Old 10-30-2007, 11:08 AM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,648
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: Using Variables outside a function

Assuming you are coding in PHP: If $new_var is assigned before the function begins, you can tell the function to use the the "global" version of $new_var rather than creating a local copy using the global operator.

PHP Code:
function example ($input_var) {
   global 
$new_var;
   if (
$input_var == option1) {
      
$new_var new_desired_output1;
   }
   else {
      
$new_var new_desired_output2;
   }

This may be preferable to pass by reference as you do not need to add the parameter to existing functions, and it is simpler to implement when you are dealing with multiple global variables, although both methods should have similar overhead.
__________________
The best way to learn anything, is to question everything.
Reply With Quote
  #4 (permalink)  
Old 10-30-2007, 02:20 PM
WebProWorld Pro
 
Join Date: Jul 2003
Location: Canada
Posts: 272
ackerley1 RepRank 0
Default Re: Using Variables outside a function

global works best for my application.

Thanks
__________________
Rob
Reply With Quote
  #5 (permalink)  
Old 10-30-2007, 07:30 PM
WebProWorld Pro
 
Join Date: Sep 2005
Location: Manchester, UK
Posts: 254
mikesmith76 RepRank 0
Default Re: Using Variables outside a function

In your situation i think i'd be tempted to return an array of values from the function rather than use global variables. Just makes the function more independent and reusable. What if you decided to move that function to another file? You'd have to remember to move all the related variables also.

Something to think about.

Mike
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A little PHP function that might be helpful shannonlp Web Programming Discussion Forum 2 08-01-2007 04:54 PM
PHP help - function call bilabong Web Programming Discussion Forum 0 07-22-2007 07:56 AM
Form and Function grease Graphics & Design Discussion Forum 0 09-19-2005 12:36 AM
Everything You Must Know About ColdFusion Variables WPW_Feedbot Graphics & Design Discussion Forum 0 08-19-2005 12:37 AM
php redirect variables scollett Web Programming Discussion Forum 11 01-20-2005 11:57 PM


All times are GMT -4. The time now is 04:13 AM.



Search Engine Optimization by vBSEO 3.3.0