Jump to content

Recommended Posts

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... :)

Link to comment
https://forums.phpfreaks.com/topic/71078-help-with-an-undefined-varible-issue/
Share on other sites

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.

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";

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?

 

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!

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)

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);
?>

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.