Jump to content

Having a problem with error-trapping in live situation


brucemand

Recommended Posts

not sure if it makes sense with such a small snippet, but if i'll add more if it's needed to figure out what's going on.it's basically a translation program;

foreach($searchfor as $chkwrd) {  $found=strpos($word,$chkwrd);  if($found!==false) {//  $keys=array_keys($assignas); reset($assignas);	EDIT: my mistake, this line is redundant and has been taken out  while (list($key, $val) = each($assignas))	  <-- this is now Line #70 and *still* the problem.	{ if ($key==$chkwrd) echo $val; }...continues...

where;

#$searchfor=array("Eins", "Zwei", "Drei");$assignas=array( "Eins"=> "One", "Zwei"=> "Two", "Drei"=> "Three");

$word is the input string from the user, exploded by chr(32)in the localhost environment, everything (seems to) works out fine, but on the host i get;

Warning: array_keys() [function.array-keys]: The first argument should be an array in .../.../translate.php on line 70Free Web HostingPHP Error MessageWarning: reset() [function.reset]: Passed variable is not an array or object in .../.../translate.php on line 70Free Web HostingPHP Error MessageWarning: Variable passed to each() is not an array or object in .../.../translate.php on line 71
i believe the problem is a question of error-trapping because if it gets the expected input , it does work okay.the same error happened in getting/processing/reading-in the $word-s, on line 60-ish; when you just type in single words (nothing to 'explode') so i filtered it with if(is_array($sentence)); and the parser accepted it, but this is now a declared array, how could it be NOT an array ??unless the error is not actually on line 70 itself ?EDIT:ahh, silly me - line 70 is actually redundant !!i just noticed the $keys and $key and retraced what that was supposed to be doing, i think it was leftover code from prior attempts at the array handling.line 70 has now been taken out, but the problem remains with (ex- line 71) ;
Warning: Variable passed to each() is not an array or object in /.../.../translate.php on line 70
why is that, $assignas is an array, right ?
Link to comment
Share on other sites

Are you trying to use a global variable in a function without declaring it as global? Is that code inside a function?
ahh yes, me and my sloppy program flow !!they are all in the global scope, but $assignas was declared AFTER the each() ! :) (i had initially used a function to do the declaring 'up top'; i understand in PHP, functions can be declared "anywhere" in the program? (within the same global scope) - but then i dispensed with the function, and obviously forgot to move the var decl. code up top where the function was originally)lesson learned; no matter how simple the program, declare all variables up top, then functions, *THEN* the actual program !is there a STRICT(?) setting that allows such sloppiness to go unpunished ? why does it not trigger an error in my localhost environment ? (not that i have any debugger in place, but the code executes with no apparent problem.)
Link to comment
Share on other sites

Remember it is good programming practice to avoid global variables. Passing data in arguments and return values usually solves whatever problem you are using globals to solve.OTOH, if the value of your global variables never changes, consider using the define() function to define them as constants, which are globally available without explicit declarations. PHP uses define() to help optimize memory management.

Link to comment
Share on other sites

Remember it is good programming practice to avoid global variables. Passing data in arguments and return values usually solves whatever problem you are using globals to solve.
nicely summarised. yes, i've "scrubbed"(?) my older "libraries"(!!) of unnecessary globals now.
OTOH, if the value of your global variables never changes, consider using the define() function to define them as constants, which are globally available without explicit declarations. PHP uses define() to help optimize memory management.
ok, thanks.good tip, will read up on it and try to incorporate it into my standard portfolio.how about this, though?
is there a STRICT(?) setting that allows such sloppiness to go unpunished ? why does it not trigger an error in my localhost environment ? (not that i have any debugger in place, but the code executes with no apparent problem.)
Link to comment
Share on other sites

One reason is that PHP allows you to create dynamic variables (also called variable variables) using $$ syntax. This means that the environment has no way of predicting in advance whether a local variable with a particular name will exist during runtime or not. So it has to let stuff like that pass through at compile-time.It's not really a big deal. PHP is so abstracted from the metal that undefined variables don't create a fatal bus error or anything that would crash the system or the environment.

Link to comment
Share on other sites

#CUT#It's not really a big deal. PHP is so abstracted from the metal that undefined variables don't create a fatal bus error or anything that would crash the system or the environment.
what does 'metal' here mean ?
Link to comment
Share on other sites

Programmer slang. If you have the right tools, you can write a program that directly accesses the instruction set built into a microprocessor and address actual memory locations in RAM. Higher level languages exist so you don't have to do that. So we have a continuum representing "distance" from the chips ("the metal").At one extreme, the programmer interacts with the chips pretty directly. The people who write operating systems do a lot of this. Assembly is very close to the metal.At the other extreme, you don't need to know anything about the microprocessor, and the programming environment handles all the memory management for you. Scripting environments like PHP and JavaScript tend to be like this.In the middle are languages like C and C++ that require the programmer to manage memory and give the programmer access to memory locations through devices like pointers. Usually, you can even embed sections of Assembly code right in a section of a C program.The closer to the metal your language gets, the faster your program usually will run, so processor-intensive applications tend to be written in intermediate- or low-level languages (sections of them, anyway). The trade off is development time. Programming in PHP is comparatively easy. Only a few experts program in Assembly these days.

Link to comment
Share on other sites

Programmer slang. If you have the right tools, you can write a program that directly accesses the instruction set built into a microprocessor and address actual memory locations in RAM. Higher level languages exist so you don't have to do that. So we have a continuum representing "distance" from the chips ("the metal").
ahh okay, them that be holding the electrons carrying the '0's and '1's !i thought it had something to do with being the next lower level after 'chrome' (after 'frame', after 'window').
##CUT##Only a few experts program in Assembly these days.
yes, "machine language" !!i actually tried to dabble a bit with that for the 8080 (!!) showing age here...also, i forget which language had the PEEK & POKE, i think that was BASIC.
Link to comment
Share on other sites

Yeah, that's BASIC. That functionality was used a lot when desktop computers were simpler and commercial programs were actually written in BASIC. A strange mixture of high-level and low-level programming. BASIC was originally designed to AVOID that kind of thing. It was a necessary kludge, though, because better tools did not yet exist. Once C compilers became common for DOS computers, commercial programs were no longer written in BASIC. Imagine Microsoft Excel written in BASIC. Ridiculous. But that's where spreadsheet software was invented.

Link to comment
Share on other sites

#CUT#Imagine Microsoft Excel written in BASIC. Ridiculous. But that's where spreadsheet software was invented.
OUCH!although i guess it's not "that" difficult.i use OpenOffice now and sometimes, during "moments of clarity" :) , i can just make out what the scripting code might look like to perform a particular task. (eg. for the filtering of columns)after all, spreadsheets are really just flat-files, and arrays can handle that quite simply i imagine.infact that's what my personal projects are, i take a spreadsheet that i used in the late 90s and now try to convert it into a browser interfaced application - it beats making all those macros - and having to remember them too !
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...