1. User Avatar

    Not Clear

    While I have little doubt that you know what you are talking about, either you are not writing clearly or I don’t see you telling us what to do INSTEAD of the bad coding mistakes you see.  That means, this little article provides no help to me whatsoever when it absolutely could.

  2. User Avatar

    I’m… sorry, but your comment just has me absolutely baffled.  I honestly don’t know how to respond to it.

    The only thing I can assume is that you’re tripping up on the tl;dr of the article.  And yes, it’s extremely tl;dr, because that’s just how I write.  However, I’ve re-read this twice now since seeing your comment, and I can’t really see how this is ‘not clear’.  Maybe that’s my own failing (I’m notoriously bad at being able to assess the quality of my own writing), but considering that part of my professional career is having to write clearly, and my ability to do so is something that I have been praised on since high school…  hrm.

    I suppose I’m just confused because the vast majority of the words here are, in fact, telling people how and why.  It’s not lowest-common-denominator stuff, but then again it’s not supposed to be.  Nor is it really a step-by-step tutorial; it’s discussing three things that, I suppose, have more to do with the theory of programming rather than just the “now write x, now write y” of your average PHP tutorial.  The thing on multiple MySQL connections especially is reasonably ‘advanced’ as far as these things go (it’s about as complex resource management as you’re going to get in PHP).  Maybe the title is slightly misleading in that regard.

    Perhaps it would help if I knew what parts you were struggling on?  I suppose, to break it down, the first part (What It Is(n’t)) is discussing the difference between an interpreted language like PHP, a markup language like HTML, and a compiled language like C/++.

    The second part (Common Mistake #1: MySQL) is discussing how PHP handles connections to MySQL databases, and why your scripts will most likely die if you’re trying to run two scripts side-by-side that keep their data in separate databases.  I see this error a lot at CodeGrrl – usually people trying to run WordPress and a fanlisting script at the same time – and AFAIK I’m the only person there yet (or at least was at the time of initial writing) who’s ever answered why it happens rather than just providing the stock-standard solution (put both scripts in the same db).  If you’ve never encountered this error before, this section will make much more sense if you try the example code.  There’s no simple ‘fix’ to this, incidentally, because (oddly enough) there is no simple fix to this.  You either understand the problem or you don’t.

    The third section, Common Mistake #2: Redeclaring Variables, is talking about some general bad-form coding that I see everywhere.  In a nutshell, it is telling you that instead of doing this sort of thing:

    $myName = $array['myName'];

    print "$myName";

    You should suck it up, learn to code properly, and do this:

    print "$array[myName]";

    And then rabbits on for a bit about why doing the former will make all the cool kids laugh about you behind your back.  Like the previous two sections, it touches on the notion of resource/memory management in programming; something that is vital in ‘hard’ programming languages like C/++, but is often abused terribly in ‘soft’ languages like PHP.

    And finally, the fourth section (Tips and Tricks: Associative Arrays) is just telling people not to be scared of associative arrays.

    Does that clear things up a little?

    [b]Editblush.png/b] Incidentally, I fixed up the dead sk.fan link.  My bad for dropping a script in the middle of writing it…

  3. User Avatar

    Yes, that does clear it up. Forgot to mention that the part I wasn’t getting was the redeclaring variables.  Now that I understand what you were aiming to say, the reason I declare the variables like that (the way you don’t suggest) is simply because when I type it all out multiple times, I end up forgetting an apostrophe or something and I get super annoyed. I found that doing it that way helps with that.

    Do you know how much difference it makes for resource or memory to do it the “proper way”?  I don’t really care what people think of me based on how I code so if it doesn’t really make that much of a difference, I’m not going to change.  Considering I know how to do it, I just choose to do it the first way to lessen on my mistakes when I type fast.

  4. User Avatar

    Arrays Are Luff!

    Do you know how much difference it makes for resource or memory to do it the “proper way”?

    Not in explicit numbers, but you can work it out using common sense.  In a nutshell, if you have five variables, and then you dump the value of those five variables into another five variables then you have doubled the memory allocation your script requires (because you’re telling it to store twice the data).

    The question is does this actually matter?  With any compiled application the answer would be, “Hell yes!”… but PHP isn’t a compiled language.  It’s interpreted, and realistically the kinds of scripts that you or I are likely to be commonly writing there is absolutely no noticeable difference to the user no matter what you do.  If you notice, down the bottom of the page there is a timer; even on the bulkiest of sk.log pages (and the code here is far from optimised) it will rarely crack 2 seconds execution time.  It’s pretty exceptional for it to hit 1 second.  Your users simply cannot tell (because human brains are slow) the difference between a page that loads in 0.1 seconds and one that loads in 0.2 seconds.  So from a user perspective, it’s moot.

    Does it makes a difference to the machine you’re running the script on?  Again, probably not; you’re talking such tiny scripts using such tiny amounts of processing power, that you are just never going to kill the PHP interpreter.  Ever.1  And PHP does its own garbage collection (it destroys all variables at the end of every script execution), so you’re not running the risk of memory leaks or whatever.  About the only time I could think that this sort of behaviour might affect the execution of a script is if you were redeclaring a variable that was holding a huge amount of data (e.g. a whole image file encoded in binary)… but even then, I’d be surprised.

    I mean, you can also simply call an unset() on the array you’ve just not used after you’ve cannibalised all the values out of it, leaving you back at square one in the memory allocation stakes.

    Having said all that, there is a big But…, and it’s got to do with Programming Theory 101.

    I’m going to make an assumption here and guess that you’ve never learnt ‘hard’ programming in an institutionalised environment (i.e. school), so just bear with me for a moment while I rabbit on a bit, because there’s some background…

    Anyway, there’s this anecdotal story that every CompSci student learns in their first C++ class, and it’s about a control structure called goto.  goto is, I suppose, like the # symbol for a named internal link in an HTML page, only for programming languages.  You use it like this:

    #include <iostream.h>

    void main() {

      int i = 0;
      START:
      i++;
      cout << "Counter is at: " << i;
      goto START;

      return;
    }

    What this is doing, is setting a variable called i, naming a ‘start point’, incrementing the counter, printing the value of the counter, then using goto to return to our start point.  It’s a very, very basic control structure that is replaced in more mature languages by while() and for().

    But not in BASIC; goto is the control structure in BASIC.  Once Upon a Time, BASIC was written as My First Programming Language; a gentle introduction into CompSci for beginners.  Some of those people who’d started on BASIC then moved onto harder languages like C… and started running into problems.  Because BASIC was, well, basic and what works for ten-line exercise doesn’t always work for a several thousand line application.  goto was one of those things; notorious for reducing code into unreadable spaghetti.  Now, no C teacher in their right mind tells their students to use goto, but people had picked up the ‘habit’ in BASIC programming and carried it over to C.

    This is why I don’t like the whole thing about redeclaring variables.  It doesn’t matter too much in a language like PHP, but it has the same ‘feel’ to it as goto does, and what seems harmless now has the potential to get someone into real trouble in a tighter language, especially when you start getting into pointers and the difference between a bitwise and logical copy.  To people who’ve come from a formal programming background, it looks inexcusably sloppy.  And – as you’ve said yourself – lazy, and lazy coding is bad coding; full stop, end of story.  (I should know; I’m a master of it.)

    I dunno, maybe this is a Real Programmer™ thing; I can see how someone who doesn’t come from a formalised CompSci background would consider all this pointlessly snotty and elitist, but…  I really did have to learn this stuff the hard way.  And it is important; quality code is Srs Bizness.  Start getting lazy in one area and, well, it creeps.

    It’s not a habit I’d ever teach to others.

    1. Not quite true; I think I’ve done it once, but I was at the time using PHP to connect to a db2 database to retrieve massive amounts of data which I was then displaying graphically. ^
  5. User Avatar

    Just to be clear, I didn’t say it was lazy. I said it reduced my errors and in all actuality, I’m typing more so I don’t refer to it as lazy coding.  However, I get what you’re saying.  I’m a bit of a perfectionist so I kind of want to do it the proper way, as you call it.  I’ll just have to watch my coding better and I’m pretty good at spotting a missing apostrophe and whatnot. I have problems later on when I try to print the variable sometimes and I think that is why I stopped trying to do it that way.  My laptop is caput right now or else I’d attempt it again and let you know what I mean.

    P.S. You are right, I didn’t learn any of my programming in a school environment. I am all self-taught (minus a HTML Programming course in HS that I took after I already knew HTML and it didn’t even teach about DOCTYPES and such).

    Anyway, thanks for explaining to me about this.  I needed it “dumbed down” I guess because you were being a bit too technical for me.  

Add Comment
auto insert line breaks
use log.code
use smilies
Verification
  • v-s.net v0.6 and all content (unless noted) © Dee.
  • sk.log v0.6 spat this out in 1.605 seconds.
  • 65 / 211,523
artistic-twobyfour