Jump to content

Global Variables issue


weballergy

Recommended Posts

Hi all,

I got this code and something realy queer happens:
whem i refer to $DirtyWords as global var and send it into foreach loop i got message that
it is not a valid parameter.when i use for loop it forks like a charm
any ideas why?

[code]
$DirtyWords=array('w1','w2','w3','w4');


$text="w2 this w3 'man w4 them";

CleanLng($text);

echo $text;

function CleanLng(&$string)
{
global  $DirtyWords;//-->refer to as a global var

$SizeDW=sizeof($DirtyWords);

$string=explode(" ",$string);

$size=sizeof($string);

for($i=0;$i<$size;++$i)
{
/*
foreach ($DirtyWords as $Dword)-->***doesnt work***
{
if($string[$i]==$Dword)
$string[$i]="****";
}
*/
for ($j=0;$j<$SizeDW;++$j)//->***works fine***
{ if($string[$i]==$DirtyWords[$j])
{
$string[$i]="****";
break;
}
}
}


$string=implode(" ",$string);

}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/30813-global-variables-issue/
Share on other sites

works fine for me, id dint make any changes. you might want to look into using regex to replace the dirty words though

<?php
$DirtyWords=array('w1','w2','w3','w4');


$text="w2 this w3 'man w4 them";

CleanLng($text);

echo $text;

function CleanLng(&$string)
{
global  $DirtyWords;//-->refer to as a global var

$SizeDW=sizeof($DirtyWords);

$string=explode(" ",$string);

$size=sizeof($string);

for($i=0;$i<$size;++$i)
{

foreach ($DirtyWords as $Dword)
{
if($string[$i]==$Dword)
$string[$i]="****";
}

/*for ($j=0;$j<$SizeDW;++$j)//->***works fine***
{ if($string[$i]==$DirtyWords[$j])
{
$string[$i]="****";
break;
}
}*/
}


$string=implode(" ",$string);

}
?>

Archived

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

×
×
  • 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.