user5858
user5858

Reputation: 1221

Will this snippet eat lot of php memory on apache?

I want to add some 6000 lines of code in a php code for my website, running on LAMP. It's on a shared hosting $6/month.

My question is will it eatup lot of php memory?

if($id==1)
{
sprintf($url,....);
}
else
if($id==2)
.....

and so on till $id equals 6000

Upvotes: 0

Views: 109

Answers (3)

sarnold
sarnold

Reputation: 104050

Getting to $url 6000 will take much longer than getting to $url 1. Not 6000 times longer, but much longer.

I'm not so sure about the wisdom of this design, but using an array would probably allow for much faster access time:

$urls = array(1 => "http://www.foo.bar/", 2 => "http://blubber.blorp/");
sprintf($urls[$id],...);

If all the URLs start with http://, then you could move that into your sprintf() call to save on memory. But 6000 strings times 100 bytes per (lets assume long URLs) is still just 600,000 bytes, and some overhead it's probably chewing less than one megabyte of memory total.

Upvotes: 3

webbiedave
webbiedave

Reputation: 48897

Sure it will "eat up" memory. The parser has to load 6000 if statements, urls, etc...

Since you're on LAMP, take advantage of the M by throwing the urls into a MySQL database and select the appropriate record based on the $id

Upvotes: 1

jcomeau_ictx
jcomeau_ictx

Reputation: 38432

It will eat up too much memory and CPU, even if "too much" is only a few kb; you can code it better than this.

Upvotes: 1

Related Questions