I have multiple variables like $soft0 , $soft1 , $soft2 , $soft3 , $soft4 , $soft5
and also like $fo0 , $fo1 , $fo2 , $fo3 , $fo4 , $fo5
but if i want to apply a if condition here then it showing error. Code:
for ($i=0; $i<=29; $i++) {
$soft$i = str_replace(" ", "+", $fo$i); // this line showing an error
$link_f_u = $html->find('h3', 0);
$keyy = $link_f_u->plaintext;
if ($soft$i==$keyy){
continue;
} else {
publish($soft$i);
}
}
Any ideas to modify this code?
This will fix your error, but I would highly recommend you take a look at arrays: http://php.net/manual/en/language.types.array.php
for($i=0;$i<=29;$i++){
$softVarName = "soft" . $i;
$foVarName = "fo" . $i;
$$softVarName = str_replace(" ", "+", $$foVarName);
$link_f_u = $html->find('h3', 0);
$keyy = $link_f_u->plaintext;
if ($$softVarName==$keyy){
continue;
} else {
publish($$softVarName);
}
}
This should fix the issue.
for($i=0;$i<=29;$i++){
${'soft' . $i} = str_replace(" ", "+", ${'fo' . $i}); // this line showing an error
$link_f_u = $html->find('h3', 0);
$keyy = $link_f_u->plaintext;
if (${'soft' . $i} == $keyy){
continue;
} else {
publish(${'soft' . $i});
}
}
change $soft0 ... $soft10
to arrays looking like $soft[0] .. $soft[10]
. You can then use count()
in your for loop:
for($i=0;$i<=count($soft);$i++){
$soft[$i] = str_replace(" ", "+", $fo[$i]);
$link_f_u = $html->find('h3', 0);
$keyy = $link_f_u->plaintext;
if ($soft[$i]==$keyy){
continue;
} else {
publish($soft[$i]);
}
}
You can also use double dollar sign, however this is messy and can result in errors that are hard to catch.
$soft_name = 'soft'.$i;
$fo_name = 'fo'.$i;
$$soft_name = str_replace(" ", "+", $$fo_name);