In the Star Trek episode, “A Piece of the Action”, Kirk and his crew travel to Sigma Iotia II, a planet last visited before the Prime Directive of non-interference existed. Well, they left behind a book, Chicago Mobs of the Twenties, which the Iotians took as a holy guide, to be imitated and followed even if they didn’t quite understand it, a sort of sci-fi cargo-cult. Cue the crew of the Enterprise being threatened with Tommy Guns and guys doing bad Al Capone impressions.
Michael’s co-worker may have fallen into a similar trap. An advanced developer came to him, and gave him a rule: in PHP, since variables may be used without being declared, it’s entirely possible to have an unset variable. Thus, it’s a good practice to check and see if the variable is set before you use it. Normally, we use this to check if, for example, the submitted form contains certain fields.
Like Bela Okmyx, the “Boss” of Sigma Iotia II, this developer may have read the rules, but they certainly didn’t understand them.
$numDIDSthisMonth=0;
if(isset($numDIDSthisMonth)) {
if($numDIDSthisMonth == "") {
$numDIDSthisMonth=0;
}
}
$numTFDIDSthisMonth=0;
if(isset($numTFDIDSthisMonth)) {
if($numTFDIDSthisMonth == "") {
$numTFDIDSthisMonth=0;
}
}
/*
$numDIDSthisMonthToCharge=$_POST['numDIDSthisMonthToCharge'];
if(isset($numDIDSthisMonthToCharge)){
if($numDIDSthisMonthToCharge == ""){
$numDIDSthisMonthToCharge=0;
}
}
*/
$STDNPthisMonth=0;
if(isset($STDNPthisMonth)) {
if($STDNPthisMonth == "") {
$STDNPthisMonth=0;
}
}
$TFNPthisMonth=0;
if(isset($TFNPthisMonth)) {
if($TFNPthisMonth == "") {
$TFNPthisMonth=0;
}
}
$E911thisMonth=0;
if(isset($E911thisMonth)) {
if($E911thisMonth == "") {
$E911thisMonth=0;
}
}
$E411thisMonth=0;
if(isset($E411thisMonth)) {
if($E411thisMonth == "") {
$E411thisMonth=0;
}
}
/*
$PBthisMonth=0;
if(isset($PBthisMonth)) {
if($PBthisMonth == "") {
$PBthisMonth=0;
}
}
*/
$TFthisMonth=0;
if(isset($TFthisMonth)) {
if($TFthisMonth == "") {
$TFthisMonth=0;
}
}
As you can see, in this entire block of variables, we first set the variable, then we check, if the variable isset
, on the off-chance it magically got unset between lines of code, and then we double check to see if it’s an empty string, and if it is, make it zero.
For extra credit, some of these variables are used in the application. Most of them are not actually used anywhere. See if you can guess each ones!