I have a few number values that I need to store in one string/array and then use it at a later time.

The idea is:

$a = 1;
$b = 2;
$c = 3;

$alpha = $a + " " + $b + " " + $c;

echo $alpha;

The output of alpha in turn would be "6" I want it to be "1 2 3".

Also after it is stored possible to break it up again and then retrieve the values of "1 2 3" and store them back to new variables of $a1, $b1, $c1.

Thankyou, Regards X

to use them as string put quotations around them.

$a = '1';

then to break them up again use explode() function

if you are just trying to store some data just seperate the values using commas by i think using implode(do use it much) and use explode(",",$str); to get them out again.

<?php
$a = 1;
$b = 2;
$c = 3;
$alpha = $a." ".$b." ".$c;
echo $alpha;
//prints 1 2 3
echo "<br />";
list($a1,$b1,$c1) = explode (" ",$alpha);
echo $a1; //prints 1
echo $b1; //prints 2
echo $c1; //prints 3
?>

. is the concatenation operator not + !

commented: Spot on as usual ;) +1

Nav to the rescue once again my hero! :)

Umm another thing will that work if the variables are double digits example 11, 22, 33 just the same for singular digits? Thanks

as long as they are spaced by the " " character, yes.

and if the number of values is unknown, you could also assign them directly to an array like so

$alpha = "11 22 33 44 55 66";
$array = explode(" ", $alpha);

/*
Then you will have
$array[0] = "11";
$array[1] = "22";
$array[2] = "33";
$array[3] = "44";
$array[4] = "55";
$array[5] = "66";
*/

//and you can loop through them like this
foreach($array as $value)
{
	echo $value . "<br />";
}
commented: Thanks for the helpful advice +1

wow, i feel like an idiot. how did i miss the + thing. guess i am use to javascript.

actually, after i read my post again, i have no idea what i was thinking. ignore my post.

You think you are.

Who do you think first used the +.

Haha ya just been working with other coding conventions and forgot that in php it was . instead of +.

Thanks guys working it out now and keep you all informed :)

Ok it worked but it didnt.

My small example did but when I changed it to my requirements I got this error:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 10 bytes)

Any idea on how I can fix this?
or will I need to program the above in java to work?
If so will it even work in java if it dosent work in php?

Thanks, Regards X

what exactly are you doing to cause that error? just exploding variables and such.??

Using alot of numbers? (which I think I declare too many/alot as an array?)

Try changing

memory_limit = 8M ; Maximum amount of memory a script may consume (8MB)

in php.ini .
Btw, I am not really sure if this will work !

134217728 bytes is exactly the same as 128 megabyte. So how much memory does your computer have?
Perhaps you have an endless loop somewhere in your code. Could you post the code that causes this error?

commented: ah! right ! +7

LOL, I just checked my memory it is at 128MB :D (hence 134217728 bytes)

This is just for localhost at the moment so im not too worried if it cant work on the internet, its more for a home requirement for now.

It is just a loop that declares arrays and I think it declares too many but I need it to declare an x amount.

its like:

for($i=0;$i<999999999999;$i++) {
 for($i=0;$i<999999999999;$i++) {
  $alpha[] = --$i . " " . $i . " " . ++$i;
 }
}

Now I dont know how many times each loop goes for, depends on the requirements but it is ALOT so I dont know what to do at the moment?

Also on the 128MB thing what happens if I changed it to 99999.

Will the calculation work at the risk of blowing up my computer? or just freezing IE?

Thanks, Regards X

LOL, I just checked mine it is at 128MB :D

I thought so :D


Now I dont know how many times each loop goes for, depends on the requirements but it is ALOT so I dont know what to do at the moment?

How about storing everything in a file or database instead of in memory ?

Also on the 128MB thing what happens if I changed it to 99999.

Will the calculation work at the risk of blowing up my computer? or just freezing IE?

You computer is fine (with windows 2000 or better. (or linux).

With 99999 the code will still crash:

Each string is 5 bytes (1 byte for each character)
the inner loop loops 99.999 times.
The outher loop also
So memory required (in MB) == ((99.999 * 99.999 * 5) /1024) /1024 = 9536 MB

lol..You can increase the execution time limit, by either changing max_execution_time to 0 (in php.ini ) or use set_time_limit(0); in your script.

Ignore this post !

for($i=0;$i<999999999999;$i++) {
for($i=0;$i<999999999999;$i++) {
$alpha[] = --$i . " " . $i . " " . ++$i;
}
}

This will crash your browser anyway ;)

Well a family friend has told me to construct a program on vary vague requirements.

But it requires at least 5 nested for loops and the iterations come out to something like intializing like 1mil plus arrays but by the end the script it removes like 800,000 of them.

So is this just not possible with php?
Do I have to pass it onto java or learn it myself?

Any possible fixes? Thanks

PS: can I increase "max_execution_time = 30"? Dosent seem to be working when I change the value to like 60/120.

Initializing 1.000.000 arrays shouldn't be a problem (as long as they aren't bigger then 134 bytes), but what you asked:

for (0 to 99.999)
    for (0 to 99.999)

will result in 99.999 * 99.999 loops, which is something like 10.000.000.000 loops.

So the answer is: yes, a million arrays in the memory are possible :)

so what is the max? (just so I can have an idea)

Depends on how big your array is.

Let's say that the string in the array looks like this: 1 2 3 In theory this requires 5 bytes of memory. (3 numbers and two spaces)
(NOTE: I don't know exactly how PHP memory works with memory, so it might also be 6 bytes as in C and C++, where the last byte represents an 'end of string' character)

so you have 128 MB of memory, but you want to keep some of that available for other programs like windows etc. So let's say we can use half of it == 64 MB. That would lead to the following calculation:
(64*1024^2) / 5 = 13.421.772 elements for your array.

So to keep it on the safe side: max = 10 million

commented: Nice factual information! +1

Likewise, I recommend writing to a file or a db and then reading the data back into an array when needed. What do you need a million arrays for? This program sounds like a major resource hog, I would probably try to find another way of doing things.

Hmmm ok ill try some things tomo and get back to yas.

Also a file and database will not work as it will just retrieve the value, but still the large amout of iterations are require and hence the same resources are used.

Its like a once use program nothing major more of an experiement for family.

Can anyone answer the max_execution_time = 30 question?

Also niek_e you sound very knowledgable very nice work :)

Thanks, Goodnight

PS: Its not a hog as such It needs to do alot of calculations.

max_execution_time is used if you have a script which takes more than 30 seconds to execute. When you set max_execution_time as 0, the execution time of the script is removed. That's what I use when I have a process a large csv file. :-/ But I don't think it will work in this case.

Also niek_e you sound very knowledgable very nice work :)

Thanks, but I'm actually an Embedded engineer, which means I like playing with bits and bytes. For PHP questions you shouldn't be paying too much attention to me ;) Nav33n would be the one to listen to in this thread :)

Also a file and database will not work as it will just retrieve the value, but still the large amout of iterations are require and hence the same resources are used.

Not exactly. Writing to a DB and retrieving data will use a lot of CPU time and HD-space, but you wouldn't need an enormous amount of memory. A MB or two would be enough :)

Nav33n would be the one to listen to in this thread

Lol.. no.. I got to know few things after reading your posts ! I wasn't aware of the 'in depth bits and bytes calculation' ! ;) electronics! eh ?

If you write to a file or db and call set_time_limit every x number of iterations and use your memory resources wisely by utilizing disc space, you could go all day.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.