Echo If Isset Function |
[eluser]smithme08[/eluser]
I have created a helper library with a function like: Code: function echo_if_set($arg_var_name) <input... value='<?php echo_if_set("some_val_name");?>'> and other places too. I have tried using $$arg_var_name, both with and without declaring it global in addition to using the $GLOBALS array above. In all cases the variable is never "set" inside the function. Is there some way to do something like this? What am I doing wrong? Thanks for the help!!!!
[eluser]WanWizard[/eluser]
Where is this "some_val_name" defined? Is it a global? Or is it something else? And who still uses globals these days? That is so 90's...
[eluser]smithme08[/eluser]
Ahh, sorry! They are set in the controller, which given how things are working seems to NOT make them global. For example Code: function index() I am trying to find a way for the view to call the echo_if_set helper function using "some_value" as the argument to output what was passed via the controller in $data, but I'm starting to believe its not possible.
[eluser]smithme08[/eluser]
I'm sure its an issue with the data being local to the view and therefore inaccessible in the function, but I'm hoping that maybe there's a way and therefore I'm wrong ![]()
[eluser]CroNiX[/eluser]
If you define something in the controller (a php class), it will only be visible in that controller (depending on how, and where in your class you declare it). I don't think you fully understand how PHP variable scope works...
[eluser]smithme08[/eluser]
$data["some_value"] is declared in the controller, but is accessible as $some_value in the view, so your comment is not entirely accurate in this case. Its a question of HOW its available in the view which makes all the difference. If it was as a global, there is a chance my function might work, but if they are locals (which my tests/attempts seem to indicate), then its a much different task. I'm not sure I want to either push them all into globals instead (by overriding/extending the core), OR (much better to avoid name conflicts etc) push/merge the original array (or an empty one if none give to the view) into a single global. Everyone is so anti-global in general, so there must be a good reason, he he he ![]() I'm just being over-safe wrapping every echo in if isset() probably, regardless. In production the notice of an unset variable being accessed wouldn't be printed anyway and behavior would generally be correct for the simple cases.
[eluser]CroNiX[/eluser]
$data[“some_value”] and $some_value are 2 different variables, created in 2 different places, so my comment is accurate.
[eluser]Aken[/eluser]
Variables from the $data array in your controller, when passed to the view, go through PHP's extract() function. That's what makes them go from $data['some_value'] in the controller to $some_value in the view. They are NOT globals - they're created locally when the view is parsed (loaded). Having globals is asking to have conflicts with variable names at some point. It seems pretty unnecessary in most cases when using CI.
[eluser]pbflash[/eluser]
[quote author="smithme08" date="1327362446"]$data["some_value"] is declared in the controller, but is accessible as $some_value in the view, so your comment is not entirely accurate in this case. Its a question of HOW its available in the view which makes all the difference.[/quote] The comment is accurate. The variable is available in the view because you are passing the $data array to the view function which then makes it available in the view.
[eluser]smithme08[/eluser]
Thank you Aken. You answered the (poorly asked initially, my apologies) question while also helping educate about exactly how CI manages these things. |
Welcome Guest, Not a member yet? Register Sign In |