1. 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.

  2. 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. ^
  3. 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.837 seconds.
  • 62 / 216,271
artistic-twobyfour