Jeff
Jeff

Reputation: 467

PHP Shorthand if/else if - Basic

I've got a piece of existing code that I'm having problems understanding.

I generally don't like shorthand because it requires config changes, and is harder for me to read. For this reason I'm not particularly familiar with it. The existing code was written by someone who loves shorthand.

When I encountered this:

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';

I read it as a simple if, and else if string. I converted it to:

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}

I thought that was pretty straightforward, however I'm getting different results in practice. What's the difference between the two snippets above?

Upvotes: 2

Views: 6916

Answers (6)

anubhava
anubhava

Reputation: 786091

I think you first need php switch case to simplify above code.

Although I must mention that I didn't find any code differences 2 versions of your code. It is jsut that switch case makes it more readable than many if, else if, else if, statements.

Upvotes: 1

Powerlord
Powerlord

Reputation: 88846

This isn't actually shorthand syntax. This is just an if/else if/else if where each section only has one statement and thus doesn't need a {} set.

It's a little clearer when formatted with line breaks:

if($type == 'a')
    $type = 'Type A';
else if($type == 'b')
    $type = 'Type B';
else if($type == 'c')
    $type = 'Type C';

Upvotes: 1

designspaceship
designspaceship

Reputation: 11

is $type returning an undesirable? if it is, I would :

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
} else {
 $type = 'Other Type';
}

But I'm in total agreement with the above guys, actually you should be translating it as:

switch ($type) {
  case 'a':
    $type = 'Type A';
    break;

  case 'b':
    $type = 'Type B';
    break;

  case 'c':
    $type = 'Type C';
    break;

  default:
    $type = 'Other Type';
    break;
}

That way you can always see what the undesirable data will be, depending on the circumstances I'd always set a default, esp in dev mode.

Upvotes: 1

John Carter
John Carter

Reputation: 55369

They're absolutely identical, the difference must be elsewhere.

Is that a copy/paste of the before and after code?

I agree with anubhava though, I'd tend to convert that to a switch case for clarity:

switch ($type) {
  case 'a':
    $type = 'Type A';
    break;

  case 'b':
    $type = 'Type B';
    break;

  case 'c':
    $type = 'Type C';
    break;

  default:
    break;
}

Upvotes: 4

Nanne
Nanne

Reputation: 64429

They should be identical. I'll make a test file, but I don't think it will bring change to that fact...

Woah, made a testfile:

<?php
$type = 'a';

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';
echo $type . "\n";


$type = 'b';

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';
echo $type . "\n";


$type = 'c';

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';
echo $type . "\n";


$type = 'a';

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}
echo $type . "\n";

$type = 'b';

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}

echo $type . "\n";
$type = 'c';

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}
echo $type . "\n";

and the results where indeed the same.

Type A
Type B
Type C
Type A
Type B
Type C

Upvotes: 1

MCannon
MCannon

Reputation: 4041

they are identical, the error is elsewhere.

Upvotes: 0

Related Questions