WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox 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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-30-2007, 06:07 AM
WebProWorld Pro
 

Join Date: Jul 2003
Location: Canada
Posts: 263
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 Ackerley
Sovereign Web Design
www.sovereignwebdesign.com
Reply With Quote
  #2 (permalink)  
Old 10-30-2007, 08:22 AM
DaveSawers's Avatar
WebProWorld Veteran
 

Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 384
DaveSawers RepRank 1
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, 10:08 AM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,782
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
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, 01:20 PM
WebProWorld Pro
 

Join Date: Jul 2003
Location: Canada
Posts: 263
ackerley1 RepRank 0
Default Re: Using Variables outside a function

global works best for my application.

Thanks
__________________
Rob Ackerley
Sovereign Web Design
www.sovereignwebdesign.com
Reply With Quote
  #5 (permalink)  
Old 10-30-2007, 06:30 PM
WebProWorld Pro
 

Join Date: Sep 2005
Location: Manchester, UK
Posts: 257
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

vB 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 03:54 PM
PHP help - function call bilabong Web Programming Discussion Forum 0 07-22-2007 06:56 AM
Form and Function grease Graphics & Design Discussion Forum 0 09-18-2005 11:36 PM
Everything You Must Know About ColdFusion Variables WPW_Feedbot Graphics & Design Discussion Forum 0 08-18-2005 11:37 PM
php redirect variables scollett Web Programming Discussion Forum 11 01-20-2005 10:57 PM


Search Engine Optimization by vBSEO 3.2.0