2

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?

4
  • What results are you getting? Commented Mar 17, 2011 at 21:04
  • The two code snippets are identical. Commented Mar 17, 2011 at 21:08
  • btw, if your code above is a good representation of your actual code, you may want to look into a switch statement Commented Mar 17, 2011 at 21:09
  • I am getting $type set to the incorrect value, thanks everyone for the help, I must have an error elsewhere. Commented Mar 17, 2011 at 21:16

6 Answers 6

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification and recommendation, and I agree I should probably change that out to a switch.
1

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

2 Comments

I hope no-one saw that before edit ;). The code is the same :)
Excellent, thank you! I must have an error somewhere else causing my strange results.
1

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.

Comments

1

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';

Comments

1

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.

1 Comment

Thank you for the recommendation.
0

they are identical, the error is elsewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.