Sponsored by Travelzoo
Take Advantage of Ridiculously Low Holiday Airfares view!
travelzoo.com - Flights $52 and up for Thanksgiving, Christmas & New Year. But move on it now.
66 Comments
- smartssa, on 10/12/2007, -3/+29Digg's comment system just raped me. It apparently hates code snippits.
- GTPilot, on 10/22/2007, -3/+18note to beginners: don't be lazy like this coder ... curly braces in your control structures are good mmkay?
- armbar, on 10/12/2007, -5/+15Different tools for different jobs. I personally prefer lightweight code and frameworks, ala CakePHP/Django/et al. Java's pretty much overkill for everything.
- smartssa, on 10/12/2007, -2/+11Absolutely. It may seem like a good idea to omit them "because I'm only doing one thing" but soon after you add more code and you're going to have to add the curly's later (often causing syntax oopsies!).
And this, "$pages = ($total $pages = ($total - robweber, on 10/12/2007, -2/+11"[curly braces] it unnecessarily slows down the execution, and size of the code"
if you really think that the curly braces are going to have that kind of effect on the execution and size of your code I suggest you go back and study how code execution actually works; and what kind of stuff does affect the speed of execution. - smartssa, on 10/12/2007, -0/+8Of course it's faster, but if you want 10 rows and you do Where id > 10 and id < 21 you'll only get 10 rows if all the id's from 11 to 20 actually exist. If anything has been deleted in between your results will vary. LIMIT X, Y guarantees you the number of rows you want.
- Sonic_Molson, on 10/12/2007, -1/+8How?
- armbar, on 10/12/2007, -0/+5PHP_SELF is kind of pointless to use anyway. Relative URLs will work just fine, especially in this article's case.
Here's the security risk:
http://blog.phpdoc.info/archives/13-XSS-Woes.html - boycy, on 10/12/2007, -2/+7garreh yes I agree - but when I say that the braces add an amount of time to code execution that is negligible I mean it is negligible: you can ignore it. I invite you to perform a benchmark - a mini quick-and-dirty one I just did yielded no difference between two scripts consisting of 300 if's - one with braces and one without. At least, if there was a difference, it was smaller than a microsecond. Readability=important. Potential optimisation for sub-microsecond gains in PHP=really pointless.
- boycy, on 10/12/2007, -0/+5Poppycock, I agree strongly with garreh's first sentence - it really isn't hard to add curly braces. I personally detest K&R bracing style so braces where unneeded add 2 lines per statement block - for a single statement that is unnecessary, indentation is a must so it becomes obvious where braces are missing.
robweber garreh's point is technically correct, an extra two (or one) lines, plus indentation as required will increase code size in PHP. It'll also increase parsing time but garreh - this time increase is absolutely negligible, and if we were talking about compiled languages you'd be incorrect. There are far more important things to consider for execution time / code size, braces are purely a concern of readability and defensive coding either way. - jshusta, on 10/12/2007, -1/+5LIMIT X, Y is syntactic sugar for LIMIT Y OFFSET X which you will find supported in many DBMSes.
- readme, on 10/12/2007, -2/+5You can also just use PEAR :: Package :: Pager if you don't mind using external libraries.
http://pear.php.net/package/Pager/redirected - apotropaic, on 10/12/2007, -1/+4@bonesaw -
Yea armbar is correct... you are living in the past. PHP is getting huge and you can find many jobs with it. I use PHP for most of my sites and apps but am working with .NET right now since an existing site I took over already used it. When I see java now-a-days if i can even hit the back button (firefox freezes) I do. - petepete, on 10/12/2007, -1/+4I was about to post an obligatory 'this is much easier in rails' reply but the PEAR solution actually looks pretty good.
- leighhalliday, on 10/12/2007, -0/+2Whether it is excellent or not is up for debate. But what should be said, is that this is a tutorial for mysql only. It takes advantage of the limit clause, which is not standard sql and is not built into Oracle, which is the DBMS I use at work. With Oracle you must either select everything, and only show the records belonging to your current page, or try to use the rownum meta-column and subqueries... it is a pain. You must do something like select a.*, rownum from (select * from table order by column) where rownum > 0 and rownum
- smartssa, on 10/12/2007, -1/+3thanks for the info.
- transeunte, on 10/12/2007, -1/+3"you can't use MySQL and smart in the same comment."
But you can have tehmoth and troll in the same sentence. - transeunte, on 10/12/2007, -0/+2"And if I’m correct that’s a 10.0750691% speed difference between adding curly brackets (for no good reason) and omitting them.
You decide what to do. :)"
Add the braces if it helps reading the code.
And people, stop being so goddamn stubborn. You don't have to be right _ALL_ the time. - ideadude, on 10/12/2007, -1/+3The PA is still unavailable; I'm interested to see what they did.
Here's a tutorial I wrote for creating Digg-like pagination in PHP:
http://www.strangerstudios.com/sandbox/pagination/diggstyle.php
The code could definately be tightened up, but the focus was on the rules behind the pagination. Might be helpful for those wanting to write their own code from scratch. - oriondr, on 10/12/2007, -2/+4Actually it could theoretically effect the speed of the program.. although not noticeably. PHP is compiled on the fly as it is being accessed and curly braces add two extra bytes, six if they are accompanied by new line chars. The compiler still has to iterate through these characters when the script is accessed, and that takes CPU time no matter how insignificant.
However, for programs compiled into binary, curleys will not have any effect on speed. - aboyd, on 10/12/2007, -1/+3@garreh
Am I reading your post correctly? Are you truly suggesting that developers should care about a gain/loss of one ten-thousandth of a second? That's a TENTH of a milllisecond. And you had to have 4000 "if" statements in your code to get it.
If I read your numbers right, all I can say is that you shouldn't have put them up, because they make you look ridiculous. Hopefully I'm reading them wrong. - arjie, on 10/12/2007, -0/+2garreh, 0.00005s difference with 4000 of those. I can see how that matters so much. After all, when siteA delays displaying my page by 0.00005s, I switch to siteB that provides that much-needed advantage in time.
AFAICS, the other commenters were telling you that the advantage for losing so much readability is negligible, and from what you put up that is true. Also, the idea is that you're a poor coder if you don't care about readability (atleast that's what I gathered, I'm no coder). - sideral, on 10/12/2007, -0/+1One very handy feature of Mysql that I ignored for a long time is that it allows to know the row count without having to do the Count(*) query and then the query itself. This can really boosts performance in cases where you have to do a complex query to get the data. Simply add the option:
SQL_CALC_FOUND_ROWS
e.g:
SELECT SQL_CALC_FOUND_ROWS * from table LIMIT 0, 10
then, in another query use the function
FOUND_ROWS()
to get the row count. (it ignores the Limit, you get the total row count. That's exactly what you want)
Not very standard, but very useful. - hazello, on 12/21/2008, -0/+1Not using curly braces if fricking retarded.
If you don't add them, you are a moron, and you write crappy code. That's all I have to say on the subject. - joecritch, on 10/12/2007, -1/+2This is useful for beginners. Thanks.
- shakin, on 10/12/2007, -0/+1It may be faster, but it also limits what results you can page. You have to page all results according to the order in which they were inserted into the database. If you ORDER BY anything else it won't work.
- sirsteveh, on 10/12/2007, -1/+2armbar, unlimited has now been...
martydow2000
billdreww1999
babubiradavolu
johnsmith16801
venkatbabu01
billdrew2006
infinite0111
unlimited22124
in no particular order. What really needs to happen is that people need to NOT CLICK ON HIS LINKS WHEN YOU SEE THEM. WHEN YOU VISIT HIS SITE, THE TERRORISTS^W^WHE WINS. - armbar, on 10/12/2007, -1/+2tehmoth, I'm not sure what you have against MySQL, but it performs just as well as any other enterprise DBMS, and has more features as well.
- aboyd, on 10/12/2007, -0/+1You are insane.
- GTPilot, on 10/12/2007, -1/+2if you're worried about the bytes, then you should be correcting it by using an optimizer or writing your own code to strip out the un-necessary code.
- DontSayFanboy, on 10/12/2007, -1/+2Dugg up for knowing something about other databases. I am working on a project where the client insisted upon MS-SQL and I ran into this behavior as well. Very frustrating to eventually learn that I couldn't re-use any pagination solution out there on the web because nobody bothers to test PHP with anything other than MySQL.
Even the PEAR packages using the supposed database abstraction layers couldn't provide me with paginated results using MS-SQL. It was either write ugly nested queries like you mention, or read the entire result into an array and use Pager to paginate the array. - quipo, on 10/12/2007, -1/+2End user documentation for PEAR::Pager:
http://pear.php.net/manual/en/package.html.pager.php
Some nifty tutorials:
http://www.alberton.info/pear_pager_tutorials.html
HTH - hazello, on 12/21/2008, -0/+1I'm n a crusade against PHP_SELF. It's totally stupid to use that as a form action, seriously. It's just poin tless... try ''.
- Leviathan777, on 10/12/2007, -0/+0Geez, the script name isn't separated from path info in clear variables that precisely mimic what apache (safely) provides? I've seen problems before using PHP and these kinds of variables (and had to write CGI (!!!!) wrappers around PHP to fix the behavior of PHP_SELF and things like that.
In my world, that rather severely limits the kind of tasks for which PHP is the right tool. - knugen, on 10/12/2007, -2/+2I read a comment in MySQL's documentation that using an autoincremented value (like id) like so: WHERE id > X, is better (faster) than LIMIT X, Y
Can anyone confirm?
It would seem faster but maybe SQL is smart? - ideadude, on 10/12/2007, -1/+1This guy put my code into a function (which would be easier to add to existing code):
http://www.mis-algoritmos.com/?p=101
(warning: site es en espanol) - armbar, on 10/12/2007, -1/+1Stop spamming, unlimited. Besides, who said anything about Ajax?
- zaqintosh, on 10/12/2007, -1/+1I've probably implemented this a few times without giving it much thought. Nice to see people sharing some of the mundane algorithms that often get overlooked.
- Barryke, on 10/12/2007, -1/+1you just made my day .. was looking for that :)
- jprice259, on 04/21/2009, -0/+0OMG, everyone who keeps complaining about the curly braces... get a damn life and stop critiquing other people's code... They provide the code and all you guys do is complain as if their code has beginner errors in it. It's called SHORT HAND, (same with css shorhand) read up on it... Experienced / senior developers tend to code this way since they are constantly busy taking on extra work loads from other "retard programmers" like the ones here complaining.
Other langs like VB hardly use any curly braces! Good luck trying to learn new languages where you expect these "curly braces" ...
Also, if u knew anything... other frameworks have built in objects for pagination... Like the zend framework, use that... o wait, you probably critique the frameworks too... "omg this class extends another ... i hate it b/c i don't know what that means!" ... retards...
To the author: Good work! Love your short hand! There are haters here because they are NOOBS and don't know shiznat... - clueless, on 10/12/2007, -1/+1thank you so much
been on the look for such a thing... - leighhalliday, on 10/12/2007, -1/+1ahhhh it cut off my code, plus I screwed it up anyway and did not alias my sub-select in the from portion of the query... either way, you get the point that it must be done differently for other DBMS
- garreh, on 10/12/2007, -2/+24000 if (1) ;
Average (t1): 0.000435
Average (t2): 0.000508
Average (t3): 0.000436
Average (t4): 0.000464
Average (t5): 0.000433
Average (total): 0.0004552
4000 if (1) { ; }
Average (t1): 0.000493
Average (t2): 0.000652
Average (t3): 0.000494
Average (t4): 0.000437
Average (t5): 0.000455
Average (total): 0.0005062
And if I’m correct that’s a 10.0750691% speed difference between adding curly brackets (for no good reason) and omitting them.
You decide what to do. :) - garreh, on 10/12/2007, -2/+1@aboyd it was to prove a point, you know, to end all doubt that it DOES slow down your code, irrelevant of amount of speed. To put those results into context, its 10% speed difference.
If you can read, I said in my previous post that it will possibly affect very large scaled projects, aka, ones with 4000 if code statements across all the scripts. And don't forget, that the test was for one user. If you have many, many concurrent users, that 10% suddenly becomes quite a lot. Although this only takes into consideration if the project is only made up of ifs statements, its merely to demonstrate the difference in speed.
Someone made a bold statement that it wouldn't affect speed. I done tests. It did. Simple as.
And I think it doesn't make me look ridiculous. In fact, those who digg comments such as robweber's clearly don't know the facts, and I believe they look ridiculous.
The moral is, don't purposely slow down your code because of a silly rumour that "not adding brackets makes you a bad coder." - its absolutely ludicrous.
But, each to there own. Interpret the results as you will. :) - banditaras, on 10/12/2007, -1/+0Actually I've written a paginate_by_sql myself for a complex query, for a medium size application. Although I love rails and I use it as my main web framework, it's not 1-2-3 as the marketing yada yada screencasts want to say it is.
If you're building a blog, yes it's the coolest thing on earth, but for larger apps, you have to be very careful not to end up with the same mess as with PHP.
My 2 cents :) - mike503, on 10/12/2007, -1/+0what a ***** tutorial, i have a pagination function which addresses also issues a query with the LIMIT stuff in mysql, works great. i'd post it but it's not very reusable. but this tutorial... nah. sucks.
- byrnereese, on 10/12/2007, -1/+0There is also a Flickr-esque pagination control for PHP, which works remarkably like Digg as well:
http://www.majordojo.com/php/phppaginator.php - armbar, on 10/12/2007, -5/+3@bonesaw,
My "real" job is .NET, which isn't overkill for what we do, but for my side job I use whatever I want, and I tend to lean towards leaner frameworks. I know what you're saying, but there are plenty of ways to get paid for being a PHP/Python/RoR/Whatever developer.
http://jobs.37signals.com/
http://www.authenticjobs.com/
There are also plenty of PHP jobs on Monster, CareerBuilder, etc. Nobody's forcing you to work for "the man" either. - knugen, on 10/12/2007, -2/+0Aaand what if someone accesses page.php directly?
- usernameunknown, on 10/22/2007, -3/+1Short code and very easy to implement.
-
Show 51 - 66 of 66 discussions



What is Digg?