3
Good evening folks! I’m trying to use the method str_replace()
to replace a term within a variable, but is having no effect.
foreach ($paginas as $pagina => $codigo) {
if(!strcasecmp($atual, $pagina)) {
str_replace("nav-link", "nav-link active", $codigo);
}
//Ao imprimir aqui, a substituição não surte efeito
echo $codigo;
}
I thought it would be because the variable $codigo
, which is the variable that foreach uses to assign the current value does not accept modifications, but maybe I’m wrong, so I came to ask them.
AH, correct. Now I went to read about the method on the PHP website and saw that the function returns a string or array with the values replaced. As I came from Java, I thought the method worked as replace(), with no return. Thanks for exclaiming.
– BrunnoFdc
It is a tip to always follow the PHP manual, because you are used to Java (which is a little more consistent in terms of nomenclature) will have many surprises, especially regarding the orders of the parameters (which vary greatly in PHP, often in the same function family).
– Bacco
It is. I had read about this method in the PHP manual, but had not put attention on the return part.
– BrunnoFdc