Page 1 of 2

Warning, poor coding...

Posted: Tue Jun 03, 2003 7:07 am
by mikusan
I have just changed webserver, and unfortunately they have error and warnings displayed on the main page and what i never bothered with before, well now i have to.

I think i have some poor coding here but you be my guest:

Code: Select all

$change = '';
	$change['{main}'] = 'Home';
	$change['{main_val}'] = $CFG['site_url'];

while (list($k,$v) = each($change))
		{
			$mkstring = str_replace($k, $v, $mkstring);
		}
The error i get:

Warning: Variable passed to each() is not an array or object

Posted: Tue Jun 03, 2003 7:20 am
by []InTeR[]
Try changing the $change = ''; into unset($change); maybe it handles it as a string instead of a array.

And if that don't work, try a var_dump($change); so we can see what's in the change var.

Posted: Tue Jun 03, 2003 8:10 am
by nielsene
You may need a

Code: Select all

reset($change);
right before the while loop.

Posted: Tue Jun 03, 2003 8:28 am
by mikusan
For the above:
Warning: Variable passed to reset() is not an array or object

Posted: Tue Jun 03, 2003 8:36 am
by nielsene
Is the code you posted above "complete" or did you snip out some intervening stuff?

Posted: Tue Jun 03, 2003 8:37 am
by []InTeR[]
What's the output of a var_dump?

Posted: Tue Jun 03, 2003 8:44 am
by nielsene
What version of PHP are you running?

For reference I just tried:

Code: Select all

<?php
error_reporting(E_ALL);
$change = '';
$change['{main}'] = 'Home';
$change['{main_val}'] = $CFG['site_url'];
while (list($k,$v) = each($change))
{
  echo $k.$v;
  $mkstring = str_replace($k, $v, $mkstring);
}
?>
Which output'ed

Code: Select all

Warning: Undefined variable: CFG in /var/www/compinabox/temp.php on line 5
&#123;main&#125;Home
Warning: Undefined variable: mkstring in /var/www/compinabox/temp.php on line 10
&#123;main_val&#125;
The $change array behaved fine.

Posted: Tue Jun 03, 2003 8:50 am
by Takuma
Get rid of { and }, I think its giving error because of that since when you do this

$

Code: Select all

<?php
something = '{$something2}other stuff';
?>
it uses variable $something2.

Posted: Tue Jun 03, 2003 8:57 am
by nielsene
The opening brace '{' is only "special" when adjacent to a dollar sign ($). So that shouldn't cause a problem here.

Posted: Tue Jun 03, 2003 9:00 am
by Takuma
Just another idea, how about delcaring $change as an array like this?

Code: Select all

<?php
$change = array("{main}"=>"Home", "{mail_val}"=>$CFG["site_url"]);

while (list($k,$v) = each($change)) 
{ 
   $mkstring = str_replace($k, $v, $mkstring); 
}
// Notice that I have used " instead of '
?>

Posted: Tue Jun 03, 2003 9:24 am
by twigletmac
Another option would be to do:

Code: Select all

$change_from = array('{main}', '{mail_val}');
$change_to = array('Home', $CFG['site_url']);

$mkstring = str_replace($change_from, $change_to, $mkstring);
Mac

Posted: Tue Jun 03, 2003 9:41 am
by twigletmac
Takuma wrote:Notice that I have used " instead of '
Why?

Mac

Posted: Tue Jun 03, 2003 10:03 am
by discobean
I done the same as nielsene and got the same result $change seemed ok.

Posted: Fri Jun 06, 2003 9:14 am
by mikusan
You must have E_ERRORS or something on, and all errors are logged or displayed on your page. I was busy recently and now i have to fix this problem, unfortunately trying this

Code: Select all

$change = array("{main}"=>"Home", "{mail_val}"=>$CFG["site_url"]);

would radically mean changing ALOT of lines of code unsure if that will actually work? does it? i need some sort of confirmation...please...

I have tried to start the function by somehow defining $change = array(); but i think my logic is worng because that totally no work :) perhaps syntax...

So i thought that i was wrong in the first place to say $change =''; because that means that $change is a string... right, a string that behaves like an array, and is not accepted by the each().
Then I go and delete $change = '' from the code... Nothing... still php thinks i am declaring a string and not an array of strings...

I thought of using explode in my code, but that doesn't make sense, how could u explode such a complicated string into an associative array?

I hope someone could shed some light on this...

LAtest Update, i have rewritten all the code so that it actually says: $change = array('{something}' => 'somestring');
Albeit still get the error!! Note: I use single quotes very often!

Posted: Fri Jun 06, 2003 9:21 am
by nielsene
Is there other code between between the first snippet and the second that you showed? I turned on all errors and warnings and I can't generate your error. What version of PHP are you using?