function test($matches)
{
$fx = '';
if(strpos($matches[0],"http://")===false)
{
$fx = "http://";
}
elseif(strpos($matches[0],"http:/")===false)
{
$fx = "http:/";
}
elseif(strpos($matches[0],"http:")===false)
{
$fx = "http:";
}
elseif(strpos($matches[0],"http")===false)
{
$fx = "http";
}
return $fx.$matches[0];
}
or
function test($matches)
{
if(strpos($matches[0],"http://")===false)
{
return "http://".$matches[0];
}
elseif(strpos($matches[0],"http:/")===false)
{
return "http:/".$matches[0];
}
elseif(strpos($matches[0],"http:")===false)
{
return "http:".$matches[0];
}
elseif(strpos($matches[0],"http")===false)
{
return "http".$matches[0];
}
}
I’m trying to write my scripts to them use less memory as possible, but idk sometimes the code look ugly, in my opnion the first script look more pretty and organized, but i think it use more memory of server.
thanks.
As far as which one will use less memory, I would think they are equivalent (both at their worst case).
However, as for their run times, the second one will finish executing a fraction of a nano-second faster than the first because, it will return as soon as it finds one matching condition. Over all the difference in Big-O is constant which we all know doesn’t make a difference: O(3n) verses O(n) at the best case, and O(3n) verse O(3n) at worst case (dependant on the size of $match[0]). But all this is deeply dependant on the complexity of the strpos() function.
For readability, they are also equilavently written and structured.