Category: Technical

Anything that uses 1s and 0s

  • Form Names

    Given the following page:

    <form>
      <input type="radio" name="BT.123" value="active" />
      <input type="radio" name="BT.123" value="inactive" />
      <input type="submit" value="Submit" />
    </form>
    
    <?php
      echo "GET: ", $_GET['BT.123'];
      echo "POST: ", $_POST['BT.123'];
      echo "REQUEST: ", $_REQUEST['BT.123'];
    ?>
    

    Assume the “inactive” button is checked and the form is submitted. What is the output?
    Answer: Because the “.” is converted to “_”, only the string literals (and some warnings, if using E_STRICT) are output.

    GET: 
    POST:
    REQUEST:
    

    In other words, use $_REQUEST[‘BT_123’] instead.

  • Unique Index

    If I run the following, what will happen?

    CREATE TABLE myTable (someField int, UNIQUE (someField));
    INSERT INTO myTable VALUES (null);
    INSERT INTO myTable VALUES (NULL);
    

    Answer: Two rows containing NULL will be inserted. NULL’s might be unique, so the unique index allows multiple.

  • Post increment

    <?php
    $a = 4;
    echo $a+++$a++;
    ?>
    

    What is the output?
    Answer: 9. The first $a++ looks up 4, then increments $a. The second $a++ looks up 5, then increments $a. 4 + 5 = 9. $a now contains 6.

  • Truncation

    Assume the following has completed correctly:

    CREATE TABLE test (
    charField varchar(5)
    );

    What is the end difference between the following two statements?

    INSERT INTO test (charField) VALUES ("123456");
    INSERT INTO test (charField) VALUES ("12345 ");

    Answer: In both cases, the inserted value is trimmed to ‘12345’. For the first, a warning is issued. For the second, only whitespace is trimmed, so no warning.

  • Concatenate

    What, if anything, is the minimum change (fewest inserted/deleted/modified characters) required to make the output The result is bar:


    <?php
    class A {
      private $foo = "bar";
      public function __toString() {
        return $this->foo;
      }
    }

    $myObject = new A();
    echo "The result is " . $myObject;
    ?>

    Answer:
    echo "The result is " , $myObject;

  • User Experience

    I had a positive user experience at the MySQL website the other day while registering for a webinar. The site knew I was logged in, and filled out as much of the form for me as it could. When I added the missing pieces of information, that next page noticed and provided a single-click where I could add the data to my profile. Added functionality, but completely unobtrusive – that’s the user experience I like.

  • What are ‘gotchas’?

    At work, we play a little game called “Tech Team Quiz”.

    Whenever we stumble across a bit of code that behaves oddly, or gives unexpected results that turn out to be documented clearly in the manual, or just contain some devious little error that took a long time to hunt down, we post it as a challenge for the others. For the purpose of this blog, I’ve dubbed them “gotchas”.

    Read carefully – sometimes it’s bad syntax, sometimes it’s behavior that changed between recent versions (for PHP, you can assume version 5.0; for MySQL, it could be anything from 4.0 to 5.1).