Warning, poor coding...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Warning, poor coding...

Post 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
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You may need a

Code: Select all

reset($change);
right before the while loop.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

For the above:
Warning: Variable passed to reset() is not an array or object
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Is the code you posted above "complete" or did you snip out some intervening stuff?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

What's the output of a var_dump?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

The opening brace '{' is only "special" when adjacent to a dollar sign ($). So that shouldn't cause a problem here.
Last edited by nielsene on Tue Jun 03, 2003 9:08 am, edited 1 time in total.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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 '
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Takuma wrote:Notice that I have used " instead of '
Why?

Mac
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post by discobean »

I done the same as nielsene and got the same result $change seemed ok.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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!
Last edited by mikusan on Fri Jun 06, 2003 9:23 am, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
Post Reply