IF something do this ELSE do that

Did that title make any sense to you? Basically it’s saying;

IF (something is true) do “this”
ELSE (something is false) so do “that”

Let’s put that into as clear english as I can, using a simple example;

IF (you want to learn how to make a good looking osCommerce Store) buy an eBook
ELSE (just use osCommerce out of the box)

If/else is simply a true/false expression. Let’s take it to PHP Code and find something in osCommerce that would be useful to use it on…

How about IF a customer is logged in, show a link to “log off” ELSE (he is not logged in) so show a “log in” link…

This is really quite simple example, and would be written like this (in standard osCommerce coding conventions):

[php]
“>
[/php]

The above (or something very like it) can be partially found in /includes/header.php – it’s basically going in and out of PHP parsing 5 or 6 times just to create 1 line of (changing) code.

Now, I prefer to write things out so that they are simpler to understand and easier to parse, so I’d do that same code something like this:

[php]‘ . HEADER_TITLE_LOGIN . ‘‘;
$logoff = ‘‘ . HEADER_TITLE_LOGOFF . ‘‘;
echo (tep_session_is_registered(‘customer_id’)) ? $logoff : $login ;
?>[/php]

As you can see we go into PHP once, and stay there to parse the whole thing, then out of PHP mode. I believe that this is definitely easier to understand when written down and is probably quicker to parse (but literally only by milliseconds). But milliseconds can add up!

In my code, we are assigning two new variables $login and $logoff. Then using a TERNARY operator based on tep_session_is_registered to determine which of $login or $logoff to show. Easy, eh?

Leave a Reply

Your email address will not be published. Required fields are marked *