Jump to content

Recommended Posts

i want to use a variable that is created inside of a function in another function

some thing like this

function show_category ($cats, $index, $level)
{
global $categories_use_categories_top_only;
$categories_use_categories_top_only='no';
.
.
$catRow = '<tr class="scPL-' . $level . '">' . "\n" . '<th colspan="' . PL_COLCOUNT . '">' . $cats[$index]['categories_name'].'<br><i>'. $categories_use_categories_top_only . '</i><br><i>'. $cat_top . '</i></th>' . "\n" . '</tr>' . "\n";
    return $catRow;
}
function show_product ($cats, $cat_index, $prod_index)
{
global $categories_use_categories_top_only;
.
.
  if ($categories_use_categories_top_only=='no' )  { 
     include(DIR_WS_MODULES . 'options.php');
}
.
.

i want to use $categories_use_categories_top_only which its value determined in the first function in the second function as i showed. the first function acts correctly

how can i do this?

Link to comment
https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/
Share on other sites

The variable would need to be a main program (global) variable to get what you are trying to work.

 

However, if you have variable that has meaning to both functions so that it would normally be shared between those functions, you should be using a class, not functions.

The way you're using it in the code you pasted is correct.  Is it not acting how you expect it to?

no it is not, i am not getting it in the second function

The variable would need to be a main program (global) variable to get what you are trying to work.

 

However, if you have variable that has meaning to both functions so that it would normally be shared between those functions, you should be using a class, not functions.

this is a gpl code that i am trying to make it work for me. i really can't write the whole thing.  is there a work around? i appreciate any help you can provide

Someone already posted in this thread what you would need to do -

The variable would need to be a main program (global) variable to get what you are trying to work.

 

The variable has got to be a global variable to start with for the global keyword to make it available inside of the function.

 

Variables created inside of a function, unless they are declared as static, are all stack based and are destroyed when the function returns/ends.

PFmabismad, he is declaring it as global within both functions.  So it's a global variable.  I'm guessing the reason it's not working is something to do with the flow of control, but it's difficult to tell with just a program fragment.  And the whole program would be quite large.

 

Frank, perhaps you can try printing out the value of the variable at different stages in the program, also printing out something to identify what part of the program it's getting printed from.  Then you can see how its value changes during execution.

he is declaring it as global within both functions.  So it's a global variable

 

No, its not. Just try it for yourself.

 

Unless there is a main program variable by that name, using global in a function does nothing.

I suggest to use inheritance.

function show_product ($cats, $cat_index, $prod_index) extends show_category()

 

So, you can use parent class values and methods in your current class.

 

1. I don't believe the original poster is writing OOP.

2. You can't extend methods.

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.