Re: [PHP] Getting mysql_query results into an array
On Tue, 2007-02-13 at 18:22 -0600, Richard Lynch wrote: > #2 > loop through mysql result set to build $array > perform some kind of calculation upon $array > > In this case, it's USUALLY much more efficient to write an SQL query > to perform the calculation. > > Databases are highly optimized for this kind of thing, and very > efficient at it. > PHP is a generalized programming language, and not so efficient for > this kind of task. > > #2 does occasionally have an exception to the rule, where the SQL > query is nasty and the PHP is fast and easy, but that's awfully rare. I hear this all the time that the database should do as much as possible "it's highly optimized" yadda yadda. I'm going to go out on a limb and say that in a heavily loaded system, shoving all the work into the primary bottleneck is a bad idea. 100 workers make light work, and that would be the inherent power in horizontal scalability. Databases do not scale well horizontally, machines loaded with Apache and PHP do, especially when each machine doesn't expect the database server to do all the work. In fact, I'd wager queries involving joins are another bane on horizontal scalability since now the tables are forced to reside on the same machine. I could be completely wrong, I've never worked on an extremely loaded server, but I get a tick every time someone says "shove all the work into the query", and that usually happens when something doesn't feel right :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Getting mysql_query results into an array
# [EMAIL PROTECTED] / 2007-02-13 17:43:10 -0700: > Richard Lynch wrote: > >The most efficient way is "Don't do that." :-) > > > >Simply loop through the results and do whatever you want to do with > >them, and don't put them into an array at all. > > This makes perfect sense. > > However, I am currently writing an abstraction > layer for a project that will later be ported from > MySQL to another database (and I haven't even > been told what that database will be, but probably > MS SQL Server), so I was thinking if some sort of > "helper functions" might be useful, but I think > simple wrappers are probably the way to go. Use iterators. -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Getting mysql_query results into an array
Richard Lynch wrote: The most efficient way is "Don't do that." :-) Simply loop through the results and do whatever you want to do with them, and don't put them into an array at all. This makes perfect sense. However, I am currently writing an abstraction layer for a project that will later be ported from MySQL to another database (and I haven't even been told what that database will be, but probably MS SQL Server), so I was thinking if some sort of "helper functions" might be useful, but I think simple wrappers are probably the way to go. -- Skip Evans Big Sky Penguin, LLC 61 W Broadway Butte, Montana 59701 406-782-2240 http://bigskypenguin.com =-=-=-=-=-=-=-=-=-= Check out PHPenguin, a lightweight and versatile PHP/MySQL development framework. http://phpenguin.bigskypenguin.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Filtering _REQUEST.. Why is this bad?
i agree, no argument there. Data coming from user should always be considered malicious. I'm just pointing out one reason why not use _REQUEST. but there are intance _REQUEST variable can be useful (just be very careful). Regarding _GET and _POST using same name, there are instance this can be useful, not at the same time in one page but rather interchangely. example in page submit you get data from $_GET['sameName'] and on the next page submit you get it from $_POST['sameName'] this is for dynamic purpose. There are situation, that for example data on _GET needed to be passed but you need to pass your page using POST.(like i said be careful and not over use because of lazyness) (i hope i'm being clear) On 2/14/07, Richard Lynch <[EMAIL PROTECTED]> wrote: On Mon, February 12, 2007 8:41 pm, J R wrote: > it is not adviced to do filtering on _REQUEST getting data in general > from > it actually. It is much better to specify where your data is coming > from ( > e.g. _POST or _GET). This is because variable _REQUST contains all the > data > from the cookies, get and post. and if ever you have the same variable > name > on two or more of those variable you might get the wrong one. > > and as we all know there is a security risk with cookies. users can > easily > replace you data for example in post using cookies. Or they could replace all the POST data using POST... A Bad Guy would have to be incredibly naive, unskilled, and downright dumb to be caught by your script differentiating between GET/POST/COOKIE as the source of the data. Spoofing a POST is a matter of saving the HTML locally and filling in whatever you want for extra INPUT and the values you like. If you intentionally have 2 (or more) inputs to your script of the same name, one each from GET/POST/COOKIE, I'd have to say that's a pretty confusing design from the get-go. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- GMail Rocks!!!
Re: [PHP] Getting mysql_query results into an array
On Tue, February 13, 2007 5:34 pm, Skip Evans wrote: > I read on php.net about resources, the type > returned from mysql_query(), and was trying locate > the best way to get the result set back from a > query into an array. > > Is simply looping through the result set with > mysql_fetch_assoc() the common way to do this? > > As great as PHP is with arrays I was wondering if > there is some simply, more efficient way, but as > yet have not located one. The most efficient way is "Don't do that." :-) Simply loop through the results and do whatever you want to do with them, and don't put them into an array at all. This question has appeared before, and usually breaks down to one of these scenarios: #1 loop through mysql result set to build $array loop through $array to output results In this case, it's clearly more efficient to do: loop through mysql result set to output results #2 loop through mysql result set to build $array perform some kind of calculation upon $array In this case, it's USUALLY much more efficient to write an SQL query to perform the calculation. Databases are highly optimized for this kind of thing, and very efficient at it. PHP is a generalized programming language, and not so efficient for this kind of task. #2 does occasionally have an exception to the rule, where the SQL query is nasty and the PHP is fast and easy, but that's awfully rare. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Getting mysql_query results into an array
Hey all, I read on php.net about resources, the type returned from mysql_query(), and was trying locate the best way to get the result set back from a query into an array. Is simply looping through the result set with mysql_fetch_assoc() the common way to do this? As great as PHP is with arrays I was wondering if there is some simply, more efficient way, but as yet have not located one. Thanks, Skip -- Skip Evans Big Sky Penguin, LLC 61 W Broadway Butte, Montana 59701 406-782-2240 http://bigskypenguin.com =-=-=-=-=-=-=-=-=-= Check out PHPenguin, a lightweight and versatile PHP/MySQL development framework. http://phpenguin.bigskypenguin.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP or Bridge (card game)
On Sat, February 10, 2007 3:19 pm, pub wrote: > To all PHP experts, > > Do any of you also know how to play bridge? > If yes, which do you think is harder to learn, PHP or bridge? Bridge is way more harder to learn. Especially all those weird bidding conventions with no rationale. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Filtering _REQUEST.. Why is this bad?
On Mon, February 12, 2007 8:41 pm, J R wrote: > it is not adviced to do filtering on _REQUEST getting data in general > from > it actually. It is much better to specify where your data is coming > from ( > e.g. _POST or _GET). This is because variable _REQUST contains all the > data > from the cookies, get and post. and if ever you have the same variable > name > on two or more of those variable you might get the wrong one. > > and as we all know there is a security risk with cookies. users can > easily > replace you data for example in post using cookies. Or they could replace all the POST data using POST... A Bad Guy would have to be incredibly naive, unskilled, and downright dumb to be caught by your script differentiating between GET/POST/COOKIE as the source of the data. Spoofing a POST is a matter of saving the HTML locally and filling in whatever you want for extra INPUT and the values you like. If you intentionally have 2 (or more) inputs to your script of the same name, one each from GET/POST/COOKIE, I'd have to say that's a pretty confusing design from the get-go. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] How to upload files up to 40MB with a html post form?
[snip] 1. My hosting provider has up to 120 seconds apache timeout 2. My hosting provider has up to 10MB to upload files in php.ini Any kind of ideas? [/snip] Get a new provider? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How to upload files up to 40MB with a html post form?
How can I upload files up to 40MB with a html post form? (without a ftp client) Please, remember 1. My hosting provider has up to 120 seconds apache timeout 2. My hosting provider has up to 10MB to upload files in php.ini Any kind of ideas? Regards Mauricio M.
[PHP] Redisplay file name on failed upload
When a file upload fails, not because of a problem with the upload itself, is there anyway of assigning the value captured by when the form is redisplayed? The failure may not be due to a problem with the file being uploaded, but because the user failed to complete a keywords field or some other bit of wanted information. Is it possible to assign form[ upldFile ] = "C:\somepath\somefile" or form[ upldFile ] = $_FILES['userfile']['name'] The script which does the validation is the same one which contains the upload form. I suspect we're completely out of luck - apart from validating with JavaScript. Regards - Miles
Re: [PHP] Redisplay file name on failed upload
Thanks guys - I guess the staging area is the way we'll go. (This is from gmail, I'm not used to it, so if I included all of the prev msgs, please forgive me.) Thanks - Miles
Re: [PHP] Redisplay file name on failed upload
Brad Fuller wrote: >> Is it possible to assign form[ upldFile ] = "C:\somepath\somefile" >> or >> form[ upldFile ] = $_FILES['userfile']['name'] > > The "value" attribute of the tag is not able to be > altered or pre-populated for obvious security reasons. > >> The script which does the validation is the same one which contains the >> upload form. I suspect we're completely out of luck - apart from >> validating >> with JavaScript. > > Yup. well no - you don't have any certainty that the javascript is even being run. you still have to check everything server side and assume the javascript was circumvented. javascript validation is purely a mechanism to aid the user - i.e. it makes the form more responsive for honest users with a javascript *enabled* browser by eliminating superfluous trips to the server. > > -Brad > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Redisplay file name on failed upload
Miles Thompson wrote: > When a file upload fails, not because of a problem with the upload itself, > is there anyway of assigning the value captured by > > > > when the form is redisplayed? > > The failure may not be due to a problem with the file being uploaded, but > because the user failed to complete a keywords field or some other bit of > wanted information. > > Is it possible to assign form[ upldFile ] = "C:\somepath\somefile" > or > form[ upldFile ] = $_FILES['userfile']['name'] > > The script which does the validation is the same one which contains the > upload form. I suspect we're completely out of luck - apart from validating > with JavaScript. it's a security measure that you are not able to set the value of file upload element, neither by filling the value attribute literally nor by using javascript. this is because your page is not allowed access to the local filesystem ... if you are able to do it then your looking at a browser bug. what you can do is place the full local path the the file in question above/below the file upload element to help the user. a better alternative would be to temporarily store the file in the staging area and redisplay the form without the upload element (and inform the user the file is already on the server) and have the user fill everything else correctly - once you recieve the rest of the data correctly you can pluck the relevant file from the staging area. another tactic would be to use a multi-page form and have the upload on the last page, only showing the last page when everything else has been submitted correctly. > > Regards - Miles > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Redisplay file name on failed upload
> Is it possible to assign form[ upldFile ] = "C:\somepath\somefile" > or > form[ upldFile ] = $_FILES['userfile']['name'] The "value" attribute of the tag is not able to be altered or pre-populated for obvious security reasons. > The script which does the validation is the same one which contains the > upload form. I suspect we're completely out of luck - apart from > validating > with JavaScript. Yup. -Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] round to nearest 500?
I don't buy "zero doesn't count". But again, this is getting into serious math. It should be good enough to say 0-4 = 0, 5-9 = 10, but if you don't keep strict high precision throughout the whole process and round at every step, things are going to be off no matter what. It's just a matter of being off as little as possible (especially when it comes to money.. people are a little touchy about that). Don't malign zero though.. :) See also: Zero: The Biography of a Dangerous Idea (Paperback) by Charles Seife -TG = = = Original message = = = I wasn't aware of the accounting trick before today, but I believe I can explain it: If your numbers are statistically random, then the above solution will lead to an even distribution of rounding up and rounding down. The reason is simple: 0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 - it already *is* 8.) 1-4: You round down -> 4 of 9 times you round down. 5-9: You round up -> 5 of 9 times you round up. So you round up 11.1% more often than you round down. As a result, if you round up when it's odd, and down when it's even, you eliminate the 11.1% difference in when you'd round up then round down. That said, if someone were aware of the above rounding trick, it wouldn't take someone very much effort to come up with things like "fee structures" or "pricing structures" that could take advantage of that scheme to force rounding errors to remain permanently in the person's favor. I certainly hope that PHP continues to use the standard technique, and not the "accounting trick" above. :-) jon ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] round to nearest 500?
- Original Message - From: "Jon Anderson" <[EMAIL PROTECTED]> [EMAIL PROTECTED] wrote: hah yeah, always worth a little skepticism, but it seemed to make some kind of sense. If you always round up or always round down, that's obviously not right and you end up losing potentially a lot of money or over-estimating the money involved. Founding up for 5 through 9 and down for 0 through 4 seems like it makes some kind of sense, but apparently it doesn't work out that way. I'm sure someone out there knows what I'm talking about (it might be the first time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least leads to fewer anomalies. Someone feel like writing a quick script that generates random numbers and does the rounding based on these two ideas (doing it the 'hard way') and see how much variation there is after like 10,000 iterations? If I have time later, I'll do it. Now I'm even more curious. I wasn't aware of the accounting trick before today, but I believe I can explain it: If your numbers are statistically random, then the above solution will lead to an even distribution of rounding up and rounding down. The reason is simple: 0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 - it already *is* 8.) 1-4: You round down -> 4 of 9 times you round down. 5-9: You round up -> 5 of 9 times you round up. That is not quite correct, there is no such 4 ninths agains 5 ninths. You round down in the interval from 0 to0.4 and you round up from 0.5 to 0.999. If you substract the .5 from 0.9, you get 0.499 so it is about the same interval for both. If there is any difference, and actually there is, is because any number is, in reality, truncated, and not rounded, at some point. Depending on the number of bits of the mantissa, it might be a long way off, but eventually, it will happen, and that one is truncation, nor rounding, and if you repeat any calculation involving fractional numbers, it will eventually add up to something noticeable. Actually, there is the further problem that computers actually use binary, not decimal, so they cannot represent decimal numbers exactly. To offer an example of what I'm talking about, 1/3, which results in a never ending 0.33 is exactly 0.1 in base 3 arithmetic! Conversely, many 'round' numbers in our decimal notation are not 'round' in binary and viceversa. So it is all the piling of lots of rounding errors in real-life number representations that produce the error, not the mathematics of rounding, which actually work in an abstract world of infinitely precise number representations (in other words, infinite bits to represent any numbers). Satyam So you round up 11.1% more often than you round down. As a result, if you round up when it's odd, and down when it's even, you eliminate the 11.1% difference in when you'd round up then round down. That said, if someone were aware of the above rounding trick, it wouldn't take someone very much effort to come up with things like "fee structures" or "pricing structures" that could take advantage of that scheme to force rounding errors to remain permanently in the person's favor. I certainly hope that PHP continues to use the standard technique, and not the "accounting trick" above. :-) jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.17.37/682 - Release Date: 12/02/2007 13:23 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] round to nearest 500?
Ahh.. good call. http://en.wikipedia.org/wiki/Rounding Apprently it's called "banker's rounding" or "statistician's rounding" and is a little more complicated than just looking at the odd/even of the digit being arounded. This is starting to get into some heavy math theory and scary stuff that I'll leave up to people much smarter than me as far as statistical analysis goes. I'm still suprised nobody's mentioned Superman 3/Office Space in all this. -TG = = = Original message = = = I think there's a fairly good article on it on wikipedia... but it's been a while since I read it and I don't have a link for you... sorry. On Feb 13, 2007, at 12:32 PM, <[EMAIL PROTECTED]> wrote: > hah yeah, always worth a little skepticism, but it seemed to make > some kind of sense. If you always round up or always round down, > that's obviously not right and you end up losing potentially a lot > of money or over-estimating the money involved. > > Founding up for 5 through 9 and down for 0 through 4 seems like it > makes some kind of sense, but apparently it doesn't work out that way. > > I'm sure someone out there knows what I'm talking about (it might > be the first time, but I know I'm not making this up hah), but > rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is > supposed to be more accurate or at least leads to fewer anomalies. > > Someone feel like writing a quick script that generates random > numbers and does the rounding based on these two ideas (doing it > the 'hard way') and see how much variation there is after like > 10,000 iterations? If I have time later, I'll do it. Now I'm even > more curious. > > -TG > > = = = Original message = = = > > >> Supposedly this is an accounting trick that >> ultimatley works out in the end for proper rounding of money >> values. > > Yeah works out for who? Bet it doesn't for the guy paying :P ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] round to nearest 500?
Ok, screw work.. it's snowing out anyway (not that that makes a real difference to doing PHP work inside), curiosity got the better of me. btw.. the "banker" rounding code here was pulled from the round() manual page. It's not what I read before, but it's the same concept: function bankers_round ($moneyfloat = null) { $money_str = sprintf("%01.3f", round($moneyfloat, 3)); // convert to rounded (to the nearest thousandth) string $thous_pos = strlen($money_str)-1;// Thousandth string position $hundt_pos = strlen($money_str)-2;// Hundredth string position if ($money_str[$thous_pos] === "5") $money_str[$thous_pos] = ((int)$money_str[$hundt_pos] & 1) ? "9" : "0"; // use a bitwise test, its faster than modulus // The above statement assists round() by setting the thousandth position to 9 or zero if the hundredth is even or odd (respectively) return round($money_str, 2); // Return rounded value } for ($i = 0; $i < 1; $i++) { $rand = rand() / 1; $round += round($rand, 2); $banker += bankers_round($rand); $total += $rand; } echo "Total: $total\n"; echo "round() rounding: $round\n"; echo "Banker Rounding: $banker\n"; Results for one run: Total: 1082648588.0678 round() rounding: 1082648588.52 Banker Rounding: 1082648588.21 Banker is closer in this case. Total should round to around ..88.07 Next example... Total: 1087871577.5462 round() rounding: 1087871578.04 Banker Rounding: 1087871577.83 Banker again closer.. should be around ..77.55 Let's do 100,000 iterations now: Total: 10769106454.867 round() rounding: 10769106456.21 Banker Rounding: 10769106456.15 Hmm....54.xx is a bit different than ..56.xx. Technically the banker one is still closer, but you can see how after a while the numbers start to drift apart if you don't (can't?) maintain the precision to keep exact values. I definitely don't want to be an accountant when I grow up. -TG = = = Original message = = = hah yeah, always worth a little skepticism, but it seemed to make some kind of sense. If you always round up or always round down, that's obviously not right and you end up losing potentially a lot of money or over-estimating the money involved. Founding up for 5 through 9 and down for 0 through 4 seems like it makes some kind of sense, but apparently it doesn't work out that way. I'm sure someone out there knows what I'm talking about (it might be the first time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least leads to fewer anomalies. Someone feel like writing a quick script that generates random numbers and does the rounding based on these two ideas (doing it the 'hard way') and see how much variation there is after like 10,000 iterations? If I have time later, I'll do it. Now I'm even more curious. -TG = = = Original message = = = > Supposedly this is an accounting trick that > ultimatley works out in the end for proper rounding of money > values. Yeah works out for who? Bet it doesn't for the guy paying :P ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] round to nearest 500?
[EMAIL PROTECTED] wrote: hah yeah, always worth a little skepticism, but it seemed to make some kind of sense. If you always round up or always round down, that's obviously not right and you end up losing potentially a lot of money or over-estimating the money involved. Founding up for 5 through 9 and down for 0 through 4 seems like it makes some kind of sense, but apparently it doesn't work out that way. I'm sure someone out there knows what I'm talking about (it might be the first time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least leads to fewer anomalies. Someone feel like writing a quick script that generates random numbers and does the rounding based on these two ideas (doing it the 'hard way') and see how much variation there is after like 10,000 iterations? If I have time later, I'll do it. Now I'm even more curious. I wasn't aware of the accounting trick before today, but I believe I can explain it: If your numbers are statistically random, then the above solution will lead to an even distribution of rounding up and rounding down. The reason is simple: 0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 - it already *is* 8.) 1-4: You round down -> 4 of 9 times you round down. 5-9: You round up -> 5 of 9 times you round up. So you round up 11.1% more often than you round down. As a result, if you round up when it's odd, and down when it's even, you eliminate the 11.1% difference in when you'd round up then round down. That said, if someone were aware of the above rounding trick, it wouldn't take someone very much effort to come up with things like "fee structures" or "pricing structures" that could take advantage of that scheme to force rounding errors to remain permanently in the person's favor. I certainly hope that PHP continues to use the standard technique, and not the "accounting trick" above. :-) jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] failed to upload files into directory named "bin"
Dear all, I was just creating two upload pages,both will upload files into directory named "bin". ./bin/ <-- directory where the files uploaded ./admin/upload.php<-- I wrote path as "../bin/" ./userupload.php<-- I wrote path as "bin/" I was confused because ./userupload.php failed to upload while ./admin/upload.php run as expected. Was it because the directory name "bin"? This problem solved when I rename the directory into another name. FYI, I'm using AppServ with PHP 4.4.1 on Windows environtment. Does anyone have any explanation? -- thanks, Yeni Setiawan
RE: [PHP] round to nearest 500?
hah yeah, always worth a little skepticism, but it seemed to make some kind of sense. If you always round up or always round down, that's obviously not right and you end up losing potentially a lot of money or over-estimating the money involved. Founding up for 5 through 9 and down for 0 through 4 seems like it makes some kind of sense, but apparently it doesn't work out that way. I'm sure someone out there knows what I'm talking about (it might be the first time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least leads to fewer anomalies. Someone feel like writing a quick script that generates random numbers and does the rounding based on these two ideas (doing it the 'hard way') and see how much variation there is after like 10,000 iterations? If I have time later, I'll do it. Now I'm even more curious. -TG = = = Original message = = = > Supposedly this is an accounting trick that > ultimatley works out in the end for proper rounding of money > values. Yeah works out for who? Bet it doesn't for the guy paying :P ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] round to nearest 500?
> Supposedly this is an accounting trick that > ultimatley works out in the end for proper rounding of money > values. Yeah works out for who? Bet it doesn't for the guy paying :P -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] round to nearest 500?
For downward rounding, you'd always want to use floor() and use ceil() for rounding up. round() rounds up on a 5, down on a 4 and below. Example: echo round(141.074, 2); // 141.07 echo round(141.065, 2); // 141.07 I thought round() (or maybe it was a rounding function in another language or something like Excel) did the supposed accounting trick of rounding up and down depending on the next "significant digit" or whatever you'd call it. For example, 141.075 might round to 141.08 (because 7 is odd) and 141.065 might round to 141.06 (because 6 is even). Or vice versa. Supposedly this is an accounting trick that ultimatley works out in the end for proper rounding of money values. Guess our PHP 4.3.4 here doesn't do that though. -TG = = = Original message = = = At 7:57 PM +0100 2/12/07, Marc Weber wrote: >On Mon, 12 Feb 2007 18:02:41 +0100, <[EMAIL PROTECTED]> wrote: > >>Is there an easy way in php to round to the nearest 500? >Yeah >$rouned = round($val/500) * 500; I've always questioned the round() function. I believe it has a downward bias, am I wrong? tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] print() or echo
echo is slightly faster than print and it takes multiple arguments so instead of: echo '' . $test . ''; you can do echo '' , $test , ''; which should be faster, and I say 'should' just because as print should be slower because it has to go into the trouble of setting up a return value, so avoiding doing a concatenation first and then output the whole thing by just streaming each of the pieces to the output straight away should be faster, though string operations are so optimized as to be neglig well, you can't tell the difference. Satyam - Original Message - From: "Danial Rahmanzadeh" <[EMAIL PROTECTED]> To: Sent: Tuesday, February 13, 2007 4:49 PM Subject: [PHP] print() or echo is it true that echo is a bit faster than print()? in general, when we don't need a return value, which one is better to choose? Cheers, Danial Rahmanzadeh No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.17.37/682 - Release Date: 12/02/2007 13:23 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how do I just escape double quotes within a string?
You could use addslashes(): http://us3.php.net/manual/en/function.addslashes.php Or, the code you mentioned below, could be rewritten like: str_replace("\"","\\"",$code); or str_replace('"','\"',$code); And if you're doing it for a MySQL query call, then you want to use mysql-real-escape-string() instead of all that mess above: http://www.php.net/manual/en/function.mysql-real-escape-string.php What's happening with the example you gave is that you're having a conflict with using the same type of string encloser (what's the proper word for single and double quotes in this case? hah). """ = "" with a dangling ". Error. "\"" = a string containing just a double quote. Everything inside "" is interpreted. Everything inside '' is taken literally. '"' = a string containing a double quote ''' = '' with a dangling ' on the end '\'' = a string containing ' (I think.. I think this is the only exception to no-interpretation-within-single-quotes) Some more information on single and double quotes here: http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.single -TG = = = Original message = = = If I use add slashes, it strips everything, I just want to replace all the double quotes with slash double quote but this, of course, throws errors: str_replace(""","\"",$code); Thanks! ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how do I just escape double quotes within a string?
On 2/13/07, blackwater dev <[EMAIL PROTECTED]> wrote: If I use add slashes, it strips everything, I just want to replace all the double quotes with slash double quote but this, of course, throws errors: str_replace(""","\"",$code); Thanks! Try this $string = 'Hello "person." How are you?'; var_dump($string); var_dump(str_replace('"', '\"', $string)); Notice you can use single and double quotes in the same string. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] round to nearest 500?
At 7:57 PM +0100 2/12/07, Marc Weber wrote: On Mon, 12 Feb 2007 18:02:41 +0100, <[EMAIL PROTECTED]> wrote: Is there an easy way in php to round to the nearest 500? Yeah $rouned = round($val/500) * 500; I've always questioned the round() function. I believe it has a downward bias, am I wrong? tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] print() or echo
"negligible".. blarg spelling. :) = = = Original message = = = As referenced in the manual ( http://us2.php.net/manual/en/function.echo.php ), check out this url: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 Short story, there is a difference, but the speed difference is negligable. If anyone cares, I prefer echo too. Not sure why. Shorter to type, old habits (coming more from a scripting background than a "real programming" background even though I've done some of that too), who knows? Part of it may be the desire to have my code do what it needs to do and nothing more. Print returns a value, which I don't use, so why would I want it to do that? That's a little anal retentive, but every little bit helps I suppose, even if it's a negligable difference. -TG = = = Original message = = = On Tue, 2007-02-13 at 19:19 +0330, Danial Rahmanzadeh wrote: > is it true that echo is a bit faster than print()? in general, when we don't > need a return value, which one is better to choose? Yes, echo is faster than print. I would suggest echo over print since it is shorter and faster :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] how do I just escape double quotes within a string?
If I use add slashes, it strips everything, I just want to replace all the double quotes with slash double quote but this, of course, throws errors: str_replace(""","\"",$code); Thanks!
Re: [PHP] print() or echo
As referenced in the manual ( http://us2.php.net/manual/en/function.echo.php ), check out this url: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 Short story, there is a difference, but the speed difference is negligable. If anyone cares, I prefer echo too. Not sure why. Shorter to type, old habits (coming more from a scripting background than a "real programming" background even though I've done some of that too), who knows? Part of it may be the desire to have my code do what it needs to do and nothing more. Print returns a value, which I don't use, so why would I want it to do that? That's a little anal retentive, but every little bit helps I suppose, even if it's a negligable difference. -TG = = = Original message = = = On Tue, 2007-02-13 at 19:19 +0330, Danial Rahmanzadeh wrote: > is it true that echo is a bit faster than print()? in general, when we don't > need a return value, which one is better to choose? Yes, echo is faster than print. I would suggest echo over print since it is shorter and faster :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] print() or echo
On Tue, 2007-02-13 at 19:19 +0330, Danial Rahmanzadeh wrote: > is it true that echo is a bit faster than print()? in general, when we don't > need a return value, which one is better to choose? Yes, echo is faster than print. I would suggest echo over print since it is shorter and faster :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] print() or echo
is it true that echo is a bit faster than print()? in general, when we don't need a return value, which one is better to choose? Cheers, Danial Rahmanzadeh
Re: [PHP] illegal characters
You may check same discussion at http://bugs.php.net/bug.php?id=31184&edit=1 On Mon, 4 Dec 2006, M.Ozan Hazer wrote: Hi all, I'm getting these errors: [04-Dec-2006 18:21:56] PHP Warning: Unknown: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0 [04-Dec-2006 18:21:56] PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0 [04-Dec-2006 18:22:09] PHP Warning: session_start() [function.session-start]: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /web/fotokritik.com/lib/main.php on line 24 [04-Dec-2006 18:22:20] PHP Warning: Unknown: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0 [04-Dec-2006 18:22:20] PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0 The project was running for a long time without any problems, today these errors show up... I installed ZendPlatform trial and uninstalled maybe related but I don't knwo where to start. I checked session.save_path and other options. Seems to be normal. Do you have any ideas? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] illegal characters
Hi all, I'm getting these errors: [04-Dec-2006 18:21:56] PHP Warning: Unknown: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0 [04-Dec-2006 18:21:56] PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0 [04-Dec-2006 18:22:09] PHP Warning: session_start() [function.session-start]: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /web/fotokritik.com/lib/main.php on line 24 [04-Dec-2006 18:22:20] PHP Warning: Unknown: The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0 [04-Dec-2006 18:22:20] PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0 The project was running for a long time without any problems, today these errors show up... I installed ZendPlatform trial and uninstalled maybe related but I don't knwo where to start. I checked session.save_path and other options. Seems to be normal. Do you have any ideas? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Where would you look for documentation about interface RecursiveIterator? RecursiveIteratorAggregate - suggestion
# [EMAIL PROTECTED] / 2007-02-13 11:54:41 +0100: > > http://de.php.net/~helly/php/ext/spl/interfaceRecursiveIterator.html > > This piece of code > > > $array = array(1, 2 => array(21, 22 => array(221, 222), 23 => array(231)), > 3); > > $dir = new RecursiveIteratorIterator(new ArrayIterator($array)); untested: $dir = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Where would you look for documentation about interface RecursiveIterator? RecursiveIteratorAggregate - suggestion
On Tue, Feb 13, 2007 at 11:54:41AM +0100, Marc Weber wrote: > I've implemented a simple walk function which seems to be even easier using php. So consider this thread beeing no longer a problem :) Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Array to Object
Eli wrote: Hi, Having this array: $arr = array( 'my var'=>'My Value' ); Notice the space in 'my var'. Converted to object: $obj = (object)$arr; How can I access $arr['my var'] in $obj ? -thanks! print $obj->{'my var'}; $obj->{'my var'} = 'My New Value'; print $obj->{'my var'}; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array to Object
On Tue, Feb 13, 2007 at 12:02:10PM +0200, Eli wrote: > Hi, > > Having this array: > $arr = array( > 'my var'=>'My Value' > ); > Notice the space in 'my var'. > > Converted to object: > $obj = (object)$arr; > > How can I access $arr['my var'] in $obj ? This works but there may be much better ways. $n='my var'; echo ($obj->$n); Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array to Object
Hi, Having this array: $arr = array( 'my var'=>'My Value' ); Notice the space in 'my var'. Converted to object: $obj = (object)$arr; How can I access $arr['my var'] in $obj ? -thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Where would you look for documentation about interface RecursiveIterator? RecursiveIteratorAggregate - suggestion
http://de.php.net/~helly/php/ext/spl/interfaceRecursiveIterator.html This piece of code $array = array(1, 2 => array(21, 22 => array(221, 222), 23 => array(231)), 3); $dir = new RecursiveIteratorIterator(new ArrayIterator($array)); foreach ($dir as $file) { print "$file\n"; } ?> results in Fatal error: Uncaught exception 'InvalidArgumentException' with message 'An instance of *RecursiveIterator* or IteratorAggregate creating it is required' in /tmp/t3.php:4 Stack trace: /tmp/t3.php|4| RecursiveIteratorIterator->__construct(Object(ArrayIterator)) #1 {main} thrown in /tmp/t3.php on line 4 So this interface exists. Why am I asking? I've written a small html combinator library where each tag is represented as object. Now I want to iterate over them using RecursiveIteratorIterator. To be able to iterate over subElements I have to provide subIterator by exposing the interface RecursiveIterator, right? I'd like to also implement IteratorAggregate instead of Iterator because subtags are stored in arrays. This doesn't work because RecursiveIterator does inherit from Iterator. Thus I have to implement current, next, etc as well just beeing dummy functions calling an internal iterator iterating over those elements? Or is this much easier and there is a RecursiveIteratorAggregate interface? Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] static functions and array_map - why not allowed?
Marc Weber wrote: > > Why can't I use static functions in array_map? > > Example: > > class Dummy > { > static public function T($a) > { > echo "T called with $a\n"; > return $a+2; > } > } > > function t($a) > { > echo "t called with $a\n"; > return $a*2; > } > > echo 'invoking Dummy::T works fine : ', Dummy::T(3),"\n"; > var_dump(array_map('t',array(1,2))); > var_dump(array_map('Dummy::T',array(1,2))); do it like this: var_dump(array_map(array("Dummy","T"), array(1,2))); > ?> > > Marc > > --PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] static functions and array_map - why not allowed?
Why can't I use static functions in array_map? Example: Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Iterators
What did I miss here? In case somebody else wants to know. I've found some examples in php sources ( ext/spl/tests/array_009.phpt ) This is the way to accomplish this: $array = array(1, 2 => array(21, 22 => array(221, 222), 23 => array(231)), 3); $dir = new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::LEAVES_ONLY); foreach ($dir as $file) { print "$file\n"; } Would be really nice to have some hints in documentation. Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php