Jump to content

Function returns wrong value?


LLLLLLL
Go to solution Solved by Jacques1,

Recommended Posts

class ufo {
  const MP_SIZE_LARGE  = 600;
  const MP_SIZE_MEDIUM = 400;
  const MP_SIZE_SMALL  = 250;
  
  const LARGE  = 'LARGE';
  const MEDIUM = 'MEDIUM';
  const SMALL  = 'SMALL';
  
  public $mp_size;
  
  public function desired_width() {
      switch ( $this->mp_size ) {
      case ufo::LARGE  : return ufo::MP_SIZE_LARGE;
      case ufo::MEDIUM : return ufo::MP_SIZE_MEDIUM;
      case ufo::SMALL  : return ufo::MP_SIZE_SMALL;
      default: 
        return 0;
    }
  }
}

So check out the class and function above. What should "desired width" return if mp_size is 0? It makes no sense that the value is anything but 0. Instead, it returns 600. In fact, it will return 400 if I put the "medium" case first. Why would it do that? 

 

The function returns expected values if I put strval() around the mp_size. Can someone explain that? This makes absolutely no sense to me. I can't see how the order of the cases could possibly matter, and I also can't see how any case would be "true" when 0 doesn't equal "LARGE".

Link to comment
Share on other sites

  • Solution

You're trying to compare a number (0) with a string (ufo::LARGE). In a strongly typed language, your code wouldn't even run. In a weakly typed language like PHP, the values get converted.

 

And the integer value of "LARGE" is in fact 0:

<?php

var_dump((int) 'LARGE');
var_dump(0 == 'LARGE');

If you don't want this to happen, don't compare apples and oranges. I wouldn't even allow invalid values for the $mp_size attribute. Write a proper setter which rejects invalid values.

Link to comment
Share on other sites

I'm aware of how strongly typed languages work, and I would write different code. Sometimes weakly-typed languages have advantages. (mp_size has other reasons to be ints)

 

You're saying that ...

 

0 == (int)"LARGE"

 

... when I would have thought that 

 

"0" != "LARGE"

 

So I guess it's a matter of what gets converted to what, and I was thinking about it the opposite way of what actually occurs. Thanks.

Link to comment
Share on other sites

PHP type comparison tables

 

Sometimes weakly-typed languages have advantages. (mp_size has other reasons to be ints)

 

The attribute isn't limited to strings and integers, though. It can be set to absolutely anything. While this may be acceptable in small non-professional applications where bugs aren't a problem, I definitely wouldn't use public attributes for anything critical.
 

Link to comment
Share on other sites

Yes yes, fine. Why does every PHPFreaks answer have to be some show of chest-thumping? Gosh, I thanked you for your answer. I'm well aware that "the attribute isn't limited to strings and ints", but hey, if you feel better about yourself with your guru-like knowledge-dropping, then congrats.

 

Again, thank you for your initial response.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.