Tricky PHP Code - TIPS!

Discussion in 'PHP' started by JREAM, Oct 24, 2010.

  1. #1
    PHP VarVar $$
    $hey = 'Life';
    $b = 'hey';
    echo $$b;
    PHP:
    1. Think of it this way: Make $b's value a variable
    2. So $b is 'hey', so it becomes $hey

    One you have to think about!
    $dog = 'yeah';
    $a = array('dog', 2, 3);
    echo $$a[0];
    PHP:
    1. First get $a[0], that would be 'dog'.
    2. Then, the variable becomes $dog
    3. Then the value output is 'yeah'

    Whats this do?
    FUNCTION TEST()
    {
          ECHO "HELLO";
    }
    test();
    PHP:
    Surpisingly this works, because function names must be unique.

    Gets even more confusing
    
    $DOG = 1;
    $dog = 2;
    
    PHP:
    These are actually DIFFERENT! But above you see the casing for functions does not matter, but for variables it does!

    Know your arrays!
    $array = array(1 => 2, NULL => 3);
    echo count($array);
    PHP:
    It only returns 1 -- because NULL is not a key!

    What about this??
    $ey = array(12 => 5, 1 => 10);
    $ey[] = 'Hey'!;
    PHP:
    What will the key be for the value we input?
    It is NOT going to be 2! Even though there is no 2 value, and 2 comes after 1, any value you add without a key will automatically increment from the highest key value.


    Okay, now youre confused!!!
    $str = 'hey\\a'; 
    echo $str;
    
    PHP:
    What will that ouput?
    It will give you 'hey\a', because EVEN THOUGH we have a single quoted string (That doesnt parse $values), this is a special case that produces ONE \. You'll also find this in XML -> xpath!

    Thats all for now!
     
    JREAM, Oct 24, 2010 IP
  2. ExTeNsIoN

    ExTeNsIoN Member

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    thanks for this :D i didnt know this can be dont in PHP! its too flexible omg :D
     
    ExTeNsIoN, Oct 24, 2010 IP
  3. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Tricky code is not something any programmer worth his salt should aim for.

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
     
    Deacalion, Oct 24, 2010 IP
  4. lukefowell89

    lukefowell89 Peon

    Messages:
    182
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I agree with Deacalion, simple is the best way to go, because when you get a mountain of code, if it looks foreign when you come to look back at it, its gonna be a nightmare to work out bugs. But then again, sometimes you need these complex solutions to be able to achieve the desired result
     
    lukefowell89, Oct 25, 2010 IP
  5. anh2.info

    anh2.info Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I agree with lukefowell89.The best way is simple way
     
    anh2.info, Oct 25, 2010 IP