gr8graphix Posted September 28, 2007 Share Posted September 28, 2007 My skills are basic at best, but I can normally edit existing codes to suite my needs and someday hope to be good at it (any good coaches out there?)... Any way, I am working on this piece of code that pulls in all the echos from a language file include. Works fine but it generates dozens of "undefined variable" warnings each time it goes to the language file. For instance, the language file contains: $l_admin18 = "The webpage <b>$file</b> has been successfully edited!!!"; All the other files that do the work, includes the language file and lets says one of them calls "$l_admin18" variable. The "$file" part of the variable is undefined within the language file but IS defined somewhere else, so it works fine, but I get the undefined notice. Am I making sense? My thoughts are to write a snippet that checks for a value, if none exist, a value is assigned. Is this the best way to go or is some "empty" function better? or maybe just add the "@" to ALL the lines involved? Well, I did add: error_reporting (E_ALL ^ E_NOTICE); To the beginning of the main "index.php" file which suppressed all the notices and the script works fine, but I read somewhere that you really should clean the code up so those messages don't show up - that is what I am trying to do. the language file is just a list of variable declarations $I_head1 = "text of some sort" $I_head2 = "some text" $I_head3 = "more text" $I_head4 = "and even more" etc... it is all the messages that get printed on screen and there are different version based on different languages. the problem comes from when there is a variable called in the language code that has not been defined $I_head2 = "the file named $file is big" the $file variable is declared somewhere else, so when the language file is included, I get the "undefined" warning for each occurrence of the $file variable in the language php file. Is there some code like "check variable if empty, ignore"? if ( ! empty($file) (???) ok, I am rambling...thanks in advanced and you'll probably see me on this forum ALOT... Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/ Share on other sites More sharing options...
rarebit Posted September 28, 2007 Share Posted September 28, 2007 Can you show how you are including or reading the language file. And yes 'empty' is a valid function (http://uk3.php.net/manual/en/function.empty.php), you could also look at 'isset' (http://uk3.php.net/manual/en/function.isset.php) Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357372 Share on other sites More sharing options...
wildteen88 Posted September 28, 2007 Share Posted September 28, 2007 Is $file set before or after when the language file is included. The file that sets $file must be included before your include the language file. PHP does not back tract when calling variables. Variables must be set before that can be used. Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357377 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 after a quick review of your post, i am guessing that the language files are like so $color = "colour"; $style = "Cool"; $effect= "none"; $color = "color"; $effect= "lots"; now the problem with the above is if you include the UK.php it will be fine but if you called the US.php $style isn't defind, so you really need to add this in to the US.php file.. OR... create a new file with all the variables and call that first ie $color = ""; $style = ""; $effect= ""; $lang = "US"; include "preset.php"; include "$lang.php"; Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357380 Share on other sites More sharing options...
gr8graphix Posted September 28, 2007 Author Share Posted September 28, 2007 The include is simple, I have several in the begining: // Config.php is the main configuration file. include('config.php'); // Password file. if (is_file("$datadir/user_pass.php")) { include ("$datadir/user_pass.php"); } // Language file. include("lang/$language"); // Name of page for links, title, and logout. $logout = "index.php"; if ($su == "on") { $page_name = "su"; } else { $page_name = "index"; } But I guess I wasn't clear on the execution of the command. Let me try again... the language file is just a list of feedback, kindof like what MadTechie is saying. Lets say my index.php has a line that says (echo "You have successfully edit $page"). Since I want this available in different languages, this line is changed to (echo "$admin14"). The "$admin14" is pulled from the language file that was included which is ($admin14 = "You have successfully edit $page") for the english file. The problem occurs because the $page variable is not defined in the language file so when you first start the program and the index.php loads the language file, the index.php references the "$admin14" line in the language file 20 times...so I get 20 warning messages... better? Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357399 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 in this case, you could just add something like: // add to config.php $DefaultLang = "UK.PHP"; include('config.php'); // Language file. $language = (empty($language))?$DefaultLang:$language; include("lang/$language"); at the start of the index.php file! Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357420 Share on other sites More sharing options...
gr8graphix Posted September 28, 2007 Author Share Posted September 28, 2007 Sorry MadTechie, guess i am as clear as mud... Maybe I am over explaining... I am including a file which contains undefined variables. Is there a way to tell PHP to ignore those variables during the "include"... (the variables are declared later) Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357437 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 can't really ignore them but can stop the error message appearing <?php // error_reporting(0);// Turn off all error reporting // or just // Report all errors except E_NOTICE // This is the default value set in php.ini error_reporting(E_ALL ^ E_NOTICE); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357539 Share on other sites More sharing options...
gr8graphix Posted September 28, 2007 Author Share Posted September 28, 2007 Yeah, if you look at the beginning of my post, you'll see that is my current solution... I just thought there had to be a better way. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357550 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 basically error_reporting(E_ALL ^ E_NOTICE); will ignore the error.. or you set the variables before trying to use them.. their the 2 choices Quote Link to comment https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/#findComment-357559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.