Hi,

I am having trouble calling variables from an include file, I have tried making them global, here is my code:

function ride_level_message() {
    require '../../includes/levels.php';    
global $level_message; global $level; echo "<p>Available at: $level_message</p>"; } if ($level > $league_total_calc){ echo "<br>"; ride_level_message(); }

The include file looks like this:

if (($level > 1 && $level < 10))
        {
    $level_message = "Level 1";
}

Can someone please show me how to fix this? The level is defined throughout my script having been pulled from the DB, I just dont get why it is failing here.

Thanks,

G

    Inputs to your function should be passed in as call time parameters. Output from your function should be returned to the calling code. Functions should normally not be responsible for echoing output, so that you can use them in any situation (producing a web page, producing an email, producing a json encoded data...) By following these conventions, your function will be general purpose and will also avoid the problems you are currently having with the code.

      Thanks, I have followed the standards for functions but I am still getting the same issue that the message is an undefined variable. I know that includes act just like they are pasted into the file itself and these variables need to be declared global, but I cant see why this isn't working. Here is what I have:

      function level_message($level, $level_message) {
            require '../../includes/levels.php';   
      global $level_message; global $level; } if ($level > $total_calc){
      require '../../includes/levels.php';
      echo "<br>"; echo "<p>Available at". level_message($level_message); echo "</p>"; }

      Thanks,

      G

        Inputs to your function should be passed in as call time parameters.

        The input to the function is the $level that you want to covert to the corresponding message.

        Output from your function should be returned to the calling code.

        The output from the function is the $level_message that the conditional logic produced.

        Therefore, the function definition would be -

        function level_message($level){
        	require '../../includes/levels.php'; // not sure why you would want the additional step of defining this one-use logic in an external file, just to require it.
        
            // levels.php logic -
        //if (($level > 1 && $level < 10)){
        	//$level_message = "Level 1";
        //}
        return $level_message;
        }

        Usage -

          if ($level > $total_calc){   
        echo "<br>"; echo "<p>Available at". level_message($level); echo "</p>"; }

          While I do not advocate the use of "global" within a function definition, if you do use it, I believe it has to come before any other commands within the function.

            NogDog;11054931 wrote:

            While I do not advocate the use of "global" within a function definition, if you do use it, I believe it has to come before any other commands within the function.

            Not Necessarily, but it does need to come before any attempted use of that variable. In this case, he is requiring before calling global to pull the variables into scope. As such, $level doesn't exist in scope until after require.

              Which probably shows you how much I use "global". 😉

                From the looks of it, the whole business of $level and $level_message in levels.php should have been a function instead of involving global variables. pbismad's post #4 outlines the right approach.

                  NogDog;11054937 wrote:

                  Which probably shows you how much I use "global". 😉

                  Which is probably a Good Thing(tm), at least if you're writing all your own code.

                  First thought that struck me: the require and global calls were reversed. But it does look a good bit like some mental spaghetti was thrown onto the platter up there.

                    dalecosp;11054941 wrote:

                    But it does look a good bit like some mental spaghetti was thrown onto the platter up there.

                    I don't think it would have helped having the variables declared global and passing them in as parameters would have helped.

                      The code I posted from the include file was just a snippet, I am trying to return a message to show which level something is available at, so there could be hundreds of these levels going forward. This include is going to be used throughout the site as well so I want a central place to define these levels and the messages that get sent back to the page.

                      Here is where I am at, and strangely I am not getting any errors at this point from these examples (I changed a few variables):

                      function ride_level_message($ride_level){ 
                          require '../../includes/ride_levels.php'; 
                      global $ride_level_message; 
                      return $ride_level_message;
                      
                      }  
                      
                        if ($ride_level > $league_total_calc){   
                      echo "<br>"; echo "<p>Available at". ride_level_message($ride_level); echo "</p>"; }

                      More from the include file:

                      if (($ride_level > 1 && $ride_level < 10))
                              {
                          $ride_level_message = "Level 1";
                      }
                      if (($ride_level > 10 && $ride_level < 20))
                              {
                          $ride_level_message = "Level 2";
                      }
                      if (($ride_level > 20 && $ride_level < 40))
                              {
                          $ride_level_message = "Level 3";
                      }
                      

                      I have the return value in the first part of the function, I have also tried it in the include file too:

                      if (($ride_level > 1 && $ride_level < 10))
                              {
                          $ride_level_message = "Level 1";
                      }
                      if (($ride_level > 10 && $ride_level < 20))
                              {
                          $ride_level_message = "Level 2";
                      }
                      if (($ride_level > 20 && $ride_level < 40))
                              {
                          $ride_level_message = "Level 3";
                      }
                      return $ride_level_message;
                      

                      Thanks for all responses, I feel like I am missing something really obvious here.

                      G

                        The name of your function, variables, and require(d) file are changing in every post. It is impossible to help you debug problems when what you show us doesn't match what you are doing.

                        I/we got that what you posted for the require(d) file logic was just an example. This isn't relevant, as long as the complete logic doesn't contain a syntax error and produces the correct result. However, do you have <?php ... ?> opening and closing tags around the php code that is in the required file?

                        If you do have more than a few comparisons in the required file logic, you would use a different coding method. You would put the values into an array and iterate over the array to find the output that corresponds to the input value.

                        The information I gave about writing proper functions was to avoid the use of the global keyword and in the code I posted in reply #4, there is none. In your last posted code, remove the global $ride_level_message; line of code.

                        Your last post states you are not getting any errors, but you didn't state what sort of symptom or problem you are still having.

                          Edit to the above, since I don't have edit privileges yet - if you do have several values/levels, you would store this in a database table and retrieve the result using an sql query.

                            Thanks for that, removing the global part got things working - just wanted to print the message on the page. As for the include, I did it this way so that if the levels change I reorganize the whole structure relatively easily. What would be the benefits of getting this form the database as opposed to this route?

                            G

                              The benefit of using either an array (for a relatively small number of values) or a database table (any number of values) to hold the information is that you won't have to edit program logic to add, delete, or change any of the values. If using an array, it is a data statement and would only contain the values. Any typo would be easy to see and fix, because the values aren't inline with all the conditional statements and <, > comparison operators. If using a database table, again, you would only be entering/editing the data values. For either of these methods, the php program logic or the sql query statement won't change just because you added, deleted, or changed any of the values.

                              Speaking of the php program logic, for the array method, the php comparison logic will only exist one time in the code, so fixing it or changing how it works, will only need to be done in one place. Speaking of fixing the program logic, your current code has a flaw in the logic. If the level is exactly 10, 20, or 40 your code doesn't match anything. You need to make either one of the other end point inclusive by adding an equal to the < or the > comparison.

                              Another advantage of having this in a database table is if the input level is coming from the result of a database query, you can just JOIN that query with the level/message table and directly get the message without needing any php code.

                                Write a Reply...