Why is there no “iforelse” statement?

Filed under:Computing, PHP — posted by Nic "RedWord" Smith on October 19, 02007 @ 9:35 PM

I wrote the following code today:

if (isset($predictions[$key])) {
$probability = $predictions[$key];
}
else {
$probability = 0.02275;
}

After doing this, I realized that a probability can’t be greater than 1.0, nor less than 0.0; for what I’m working on, I want a probability exclusive of exactly 1.0 or 0.0, but I can’t be certain that $predictions[$key] actually meets this requirement. There are a couple of ways to deal with this, and the most straightforward way is to add more conditions to the if statement, like so:

if (isset($predictions[$key])
&& ($predictions[$key] > 0.0)
&& ($predictions[$key] < 1.0)) {
$probability = $predictions[$key];
}
else {
$probability = 0.02275;
}

This works, but it doesn’t seem like quite the right place to put these conditions as far as human readability goes. What would make more sense to check for these conditions after $probability has been assigned, like so:

if (isset($predictions[$key])) {
$probability = $predictions[$key];
}
else {
$probability = 0.02275;
}
if (($predictions[$key] < 0.0)
|| ($predictions[$key] > 1.0)) {
$probability = 0.02275;
}

But this is less than ideal, because it involves repeating the assignment of 2.3% to $probability rather awkwardly. What we want is to attach the conditions to the else block of code. Alas, there’s no way to do this! An elseif statement would be executed iff the original if condition fails, which is not what we want! Using an independent if and eliminating the else block means we have to duplicate the original if condition, to see if we need to assign 2.3% to probability because $predictions[$key] does not exist! What would be best is a new statement altogether, which would run if a previous if statement failed, or if some other conditions were met “iforelse”:

if (isset($predictions[$key])) {
$probability = $predictions[$key];
}
iforelse(($predictions[$key] < 0.0)
|| ($predictions[$key] > 1.0)) {
$probability = 0.02275;
}

AFAIK, no computer language has any such statement.

Zend and Eclipse

Filed under:Computing, PHP — posted by Nic "RedWord" Smith on October 10, 02007 @ 12:36 PM

I just got an email from Zend telling me about their new move to embrace Eclipse:

Zend is launching a beta of the next generation of the Zend Studio family – Zend Studio for Eclipse.
This beta release (code named “Neon”) is based on proven Zend Studio technology and the Eclipse PHP Developers Tools (PDT) project. Zend Studio for Eclipse is the world’s most powerful PHP IDE – providing professional PHP development capabilities combined with the Eclipse multi-language support and plug-in extension technology.

When I tried Zend studio, I gave up on it after the trial period. As an IDE, it’s beautiful, complete, and the slowest software ever written. Java doesn’t need to be so slow — Eclipse runs almost as fast as a native application on my machine. I have missed some of the more complex features, so I’ll definitely be checking this new product out when I get a chance.



image: detail of installation by Bronwyn Lace