Saturday 15 August 2015

php - Variable data behaving strangely inside a function -


I am working on a CodeIgniter application.

A part of the Navi Menu for my application has been generated since using session data, I have to print the same thing in many places, I've written a function for printing . The file that creates the menu is given below. Function print_roles_assigned () This file is used multiple times.

  $ roles_assigned = $ this- & gt; Session-> User data ('specify roles'); Print_roles_assigned () {$ output = ''; If ($ roles are assigned) {foreach ($ roles are assigned as $ role) {$ output. = '& Lt; Li & gt; . Anchor ('home / house' 'role- & gt; role_name, $ role- & gt; resin e_name)' & lt; / Li & gt; '; }} And {$ Output. = '& Lt; Li & gt; No role was assigned. & Lt; / Li & gt; '; } Return $ Output; }   

The code given above does not work. Outside of any option, I used to use the $ global , such an issue has never happened to me before and I am not sure if the $ global If the usage is appropriate then the new code is given below:

  $ global ['assigned to roles'] = $ this- & gt; Session-> User data ('roles hired'); // change done here print_roles_assigned () {$ output = ''; $ Roles_assigned = $ Global ['roles_assigned']; // Use the global variable within the function if {$ roles_assigned} {foreach ($ roles role referred to as role) {$ output}. = '& Lt; Li & gt; . Anchor ('home / house' 'role- & gt; role_name, $ role- & gt; resin e_name)' & lt; / Li & gt; '; }} And {$ Output. = '& Lt; Li & gt; No role was assigned. & Lt; / Li & gt; '; } Return $ Output; }   

I would like to know:

  • Why did my initial code fail to work?
  • Is $ global suitable?
  • What could be an alternative method to fix this problem?

    Why did my initial code fail to work?

    In each function, the variable is called a scope. The declared variables outside the function are not accessible from within the function, unless they are passed as parameters, they are members of the class which is a member of the function, or explicitly declared to be global is done.

    There are three different ways to do this, take your pick.

    The easiest way to do this is to pass the function as the parameter IE

      $ roles_assigned = $ this- & gt; Session-> Userdata ('roles_assigned'); Print_roles_assigned ($ roles_assigned) {$ output = ''; If ($ roles are assigned) {foreach ($ roles are assigned as $ role) {$ output. = '& Lt; Li & gt; . Anchor ('home / house' 'role- & gt; role_name, $ role- & gt; resin e_name)' & lt; / Li & gt; '; }} And {$ Output. = '& Lt; Li & gt; No role was assigned. & Lt; / Li & gt; '; } Return $ Output; }   

    Another option is $ roles_assigned class members, IE

      $ this- & gt; Roles_assigned = $ this- & gt; Session-> Userdata ('roles_assigned'); Print_roles_assigned () {$ output = ''; If ($ this- & gt; roles are signed) {foreach ($ this-> roles are referred to as $ role) {$ Output. = '& Lt; Li & gt; . Anchor ('home / house' 'role- & gt; role_name, $ role- & gt; resin e_name)' & lt; / Li & gt; '; }} And {$ Output. = '& Lt; Li & gt; No role was assigned. & Lt; / Li & gt; '; } Return $ Output; }   

    The second option (not recommended) is to use the global keyword IE

      $ roles_assigned = $ this-> Session-> User Data ('roles_assigned'); Print_roles_assigned () {global $ roles_assigned; $ Output = ''; If ($ roles are assigned) {foreach ($ roles are assigned as $ role) {$ output. = '& Lt; Li & gt; . Anchor ('home / house' 'role- & gt; role_name, $ role- & gt; resin e_name)' & lt; / Li & gt; '; }} And {$ Output. = '& Lt; Li & gt; No role was assigned. & Lt; / Li & gt; '; } Return $ Output; }    

No comments:

Post a Comment