Programming

PHP (or Perl) one line if/then/else statements

August 8th, 2009

If you're toggling something between two states in PHP or Perl it's often handy to use an if/then/else one liner.

In pseudocode this goes like this:

<if this evaluates to TRUE> then <parse this> else <parse this>

All you need to do is replace the "then" with a question mark and the "else" with a colon:

<if this evaluates to TRUE> ? <parse this> : <parse this>

For example:

print  $trueOrFalse ? "you're telling the truth" : "you're lying";

Ignore the print command, it's not part of the if/then/else statement, it's just here to do something with the outcome of that statement.

The expression immediately to the left of the question mark is evaluated. The expression between the question mark and the colon is parsed if the expression evaluates to TRUE, otherwise the expression immediately to the right of the colon is parsed. So in the above example, either "you're telling the truth" or "you're lying" is printed, depending on whether $trueOrFalse is ... you guessed it ... TRUE or FALSE.

But perhaps a more common situation is toggling the value assigned to a variable. For example, toggling between TRUE and FALSE:

$trueOrFalse = $trueOrFalse ? FALSE : TRUE;

Here's a practical example of the use of if/then/else one liners. There's two in this chunk of PHP. The scroll box list below the code is the kind of thing this PHP produces.

<div style="overflow:auto; height:100px; width:300px; border:3px groove #DDD; padding:0">
<?php
    $alternateLine = FALSE;
    while($presidentsArray) {
        print "<div style=\"background-color:";
        print $alternateLine ? "#F5F8F9" : "white";
        print "; padding-bottom: 1px\"> &nbsp; &nbsp; <a href=\"someURL\" title=\"This link goes nowhere\">" .
          $presidentsArray['name'] . "</a></div>";
        $alternateLine = $alternateLine ? FALSE : TRUE;
    }
?>
</div>

Leave a comment

 

A handy PHP date() checker

July 30th, 2009

A handy site to remember if you’re writing PHP is http://php-date.com/. It provides everything you need to know about the date() function in PHP and has an interactive form for testing your formatting until you get your dates/times just the way you want them.

php-date-function

Potentially similar posts

Leave a comment



“Programmers love hierarchy … normal people hate that”

March 15th, 2009    4 Comments

treeviewSomething I’ve been preaching for years, to any software developer who’ll listen, is: don’t use a tree view control in the user interface if your users are not highly technical and there’s another way of allowing the user to do the thing they actually want to do (which there usually is if you put some thought into it).

So I was delighted to hear Jeff Atwood and Joel Spolsky’s views on the Stack Overflow Podcast #45:

 


Atwood:   … programmers love hierarchy, to a degree that they don't even understand how different they are than the public in this regard. Like they love putting everything in this little bucket, that goes in this little bucket, which is this sub-bucket of this and this, and normal people hate that. And threading is totally a manifestation of that and it drives me crazy that a lot of programmers can't see that they're like immediately like: "Oh, threading is good. I love threading. What are you talking about?" You know? They can't see it at all.

Spolsky:   Right, right.

Atwood:   It's like myopia.

Spolsky:   Yeh. I mean it's really a function of the size of the group, and one thing I've learned through years and years of usability testing is that anything that smacks of a hierarchy or a tree is not going to be understandable to the average, non-technical user.

Atwood:   Yeh.

Spolsky:   You just have to learn that: if it's a tree, or a hierarchy, like eighty per cent of the regular people are going to get confused and not quite get it.


Hierarchies are great at showing nested relationships, and they make sense to programmers, who are used to them – but most of the time the relationships don’t matter to the user. Usually the user just wants to find something and yet the tree view forces them to “drill down”, clicking down into a hierarchy that becomes increasingly complex as they click.

My request to all programmers placed in the position of having to design a user interface: avoid hierarchies unless you truly believe the end users really need the hierarchical information.

Leave a comment



Stack Overflow virgin

February 26th, 2009

stackoverflow-logo-250 I posted my first question on Stack Overflow today and already it’s had a couple of replies. I can see how Stack Overflow could become a little addictive because it has elements of a game built into it. For starters, you build up reputation points, which you get from other people by providing answers, but you need some reputation points before you can start giving points to others, and you can’t comment on other people’s answers until you’re above a certain rep level.

Have a listen to Hanselminutes Show 134 to hear Jeff Atwood, CEO of Stack Overflow, talking about the concept of the site and what they’ve done to make it an appealing place for software developers to hang out. The bit that really struck a chord with me was when he described Stack Overflow as sort of an antidote to Experts’ Exchange, the latter being a site that really rubs me up the wrong way because of its underhand tactics. There are so many times I’ve searched for something technical on Google and found a hit that looks like it might provide the answer I was looking for but I don’t notice it’s at Experts’ Exchange until I get there and discover the details are obscured because the site is run as a private club, which I refuse to join.

I like Stack Overflow. The only thing I don’t like about it is that I think its search facility is very weak right now. If you want to find stuff it’s best to use Google, like this:

http://www.google.com/search?q=site:stackoverflow.com/questions YOUR QUERY

For example:

http://www.google.com/search?q=site:stackoverflow.com/questions online help

The other aspect of it is that, if you’re not a programmer – or even if you are – it can be an intimidating place for the newcomer. You have to brace yourself and be prepared to be told you’re an idiot and should go away and never darken the doors of Stack Overflow again. But, in some ways, that’s not altogether a bad thing. It’s intended to be a games room for professional programmers – it’s not designed for just anybody to go and find an answer to any old thing. But, unlike Experts’ Exchange, everyone’s allowed to come in and wander around and listen in on the conversations. However, if you try answering a question you’re not qualified to answer, or you start asking questions that should have been asked elsewhere, then you can expect the regulars to give you a hard time.

Leave a comment



God damned exception

February 16th, 2009    1 Comment

I was late leaving work this evening and I was rushing to close down my applications so that I could shut down my laptop. I closed a Word document and immediately pulled the cable to my second monitor. The following error message popped up:

god-damned-exception-Word

This isn’t a Photoshop job, it’s a real error message, presumably tucked away in some remote corner of Microsoft Word.


Update:

Turns out it's nothing to do with Word (more's the pity). It's a "feature" of Notepad++, which is my text editor of choice right now. I must have been closing down Notepad++ at the same time as Word.

Leave a comment



^ back to top ^

Page 1 of 3123»