New to PHP. Converting a ColdFusion program to PHP

Discussion in 'PHP' started by NewPHP, Jun 29, 2012.

  1. #1
    I just learned PHP 5 days ago. I've been a programmer for many many years so languages are nothing new or special.

    I don't claim to know all of PHP, obviously, just enough to get by. I converted a 3,000 line ColdFusion program to PHP by mostly mechanical editing (changing "<CFSET " to "$", CF's "StructNew()" to an appropriate PHP class), etc. and then checking the logic. Believe it or not, my program is basically working.

    But I ran into two issues and I wonder if anyone can clarify that I missed something.

    1. In ColdFusion, unless directed otherwise, all variables are global to all functions. Not in PHP. I spent more effort trying to find variables in functions that needed to be made global that I overlooked.... Why doesn't PHP choke on undefined variables at runtime and stop processing? .. that's what I was hoping for.

    2. When I missed a dollar sign and had the exact function return statement

    return result;

    instead of the correct

    return $result;

    ... I found out (the hard way) that the returned result was actually a text string of length 6, because it treated my variable 'result' as a text string. Again, why not choke on the undefined entity?

    Can PHP be made to force all variables to be global? Can it be forced to abort when something is undefined?

    Thanks in advance.
     
    NewPHP, Jun 29, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    Why on earth would you want all variables to be Global? Do you have any idea how bad of an idea that is???? In fact...since you don't have to scope and declare variables in PHP it's a mess because you can basically use the variables all over the place...even in places that don't make sense!

    PHP is very forgiving... It's not strict. So it can be messy unless you are very careful. I spent 25 minutes debugging a class that would of took 30 seconds in Perl.

    If your concern is with PHPs poor method of handling variables you just have to be more careful and strict with your programming practices. Unfortunately, there isn't a "strict" pragma....that I know of.
     
    NetStar, Jun 29, 2012 IP
  3. NewPHP

    NewPHP Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, NetStar, I think you rested my case for me.

    By the way, I'd suggest you not issue opinions on coding practice of others, as that was not only not the question but the 'given'... I have more than enough experience to know when it may or may not be good practice or not. I didn't retire at 47 because I wasn't sharp enough to get paid enough to quit working when I wanted to.

    And also, "would of took" is correctly stated as "would have taken". But thanks again. You did answer my question... congrats.
     
    NewPHP, Jun 29, 2012 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Your age is absolutely irrelevant. So is your retirement. Forcing every variable to be Global is NOT good programming in ANY situation.

    If every variable was declared as Global your program would lack locality. A global can be modified ANYWHERE in any part of the program making it difficult to debug. It's terrible practice and a great way to pollute the namespace.

    If you think declaring every variable global in a program is ever necessary, then you aren't as sharp as you believe you are and you need to retrace the basics and standards of programming. Global Variables are usually synonymous for bad practice...in any language.

    ps. "would of took".. "would of taken".. blah blah blah... Irrelevant. You are seeking help with the PHP language. I'm not posting for help with the English language. Why bother to post if you aren't willing to accept a view from someone with years of experience in the PHP language when in fact you asked for one...
     
    Last edited: Jun 29, 2012
    NetStar, Jun 29, 2012 IP