Recommend
4 
 Thumb up
 Hide
14 Posts

BoardGameGeek» Forums » Everything Else » Chit Chat

Subject: todays programming challenge 20140111 rss

Your Tags: Add tags
Popular Tags: [View All]
Virre Linwendil Annergård
Sweden
Stockholm
flag msg tools
Forza Bajen!
badge
... and still sober
Avatar
mbmbmbmbmb
So lets have some fun for programmers, there might be more of these in the future with other languages, and I might give geekgold for creative and good answers. This first one is rather easy if you know some PHP as it is this. It is based on a real issue that we had at work that relates to how the drupal_set_message function works in, to no big surprise drupal 7. It is easy to solve if you know it

So to the code


function show_message($message) {
if ($message) {

// Of course a real such message would to something a bit harder.
$out_message = '*' . $message;
}
else {
$out_message = 'no message';
}
print $out_message;
}

$value = '0'; // Yes the quotes, it should be a string. It might well be returned from an outside source.
show_message($value);


4 
 Thumb up
1.00
 tip
 Hide
  • [+] Dice rolls
Blorb Plorbst
United States
Bloomington
Indiana
flag msg tools
badge
I think we're all bozos on this bus.
Avatar
mbmbmb
I assume you're asking what the output is and as I'm no PHP expert, I'll assume there's no syntax errors to deal with (looks like the braces are all paired).

Spoiler (click to reveal)
output = *0

The string '0' will not be cast back to an integer so it will resolve to true as a value is present

I assume you would only get 'no message' if a null or zero is passed.



Please coach me if I got any of that wrong.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Virre Linwendil Annergård
Sweden
Stockholm
flag msg tools
Forza Bajen!
badge
... and still sober
Avatar
mbmbmbmbmb
CrankyPants wrote:
I assume you're asking what the output is and as I'm no PHP expert, I'll assume there's no syntax errors to deal with (looks like the braces are all paired).

Spoiler (click to reveal)
output = *0

The string '0' will not be cast back to an integer so it will resolve to true as a value is present

I assume you would only get 'no message' if a null or zero is passed.



Please coach me if I got any of that wrong.

¨
I should have been clearer, I wanted to know both the output and if there is something that will not work as thought.

Also
Spoiler (click to reveal)

The if will type it as the number 0 and php treats NULL as FALSE so this will write will write 'no message'. Thats the real problem, we want the string to be written even if it is 0.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Blorb Plorbst
United States
Bloomington
Indiana
flag msg tools
badge
I think we're all bozos on this bus.
Avatar
mbmbmb
virre wrote:

Also
Spoiler (click to reveal)

The if will type it as the number 0 and php treats NULL as FALSE so this will write will write 'no message'. Thats the real problem, we want the string to be written even if it is 0.


So I completely blew it.
Spoiler (click to reveal)
I thought variable typed themselves based on context. so that $value = '0'; makes $value a string. Am I right so far?
When does it get re-evaluated? in the function call or through the "if" evaluation?
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Virre Linwendil Annergård
Sweden
Stockholm
flag msg tools
Forza Bajen!
badge
... and still sober
Avatar
mbmbmbmbmb
CrankyPants wrote:
virre wrote:

Also
Spoiler (click to reveal)

The if will type it as the number 0 and php treats NULL as FALSE so this will write will write 'no message'. Thats the real problem, we want the string to be written even if it is 0.


So I completely blew it.
Spoiler (click to reveal)
I thought variable typed themselves based on context. so that $value = '0'; makes $value a string. Am I right so far?
When does it get re-evaluated? in the function call or through the "if" evaluation?


Spoiler (click to reveal)

To be honest it was something I learned today. It seems to be a retypeing in the if. Often it is the correct way to handle 0 but I do see it as a problem of the languages dynamic, weak typing.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Scott Lewis
United States
Thornton
Colorado
flag msg tools
NFHS Football & Basketball
badge
Dread Our Coming, Suffer Our Presence, Embrace Our Glory (Solonavi War Cry)
Avatar
mbmbmbmbmb
virre wrote:
Spoiler (click to reveal)

To be honest it was something I learned today. It seems to be a retypeing in the if. Often it is the correct way to handle 0 but I do see it as a problem of the languages dynamic, weak typing.

Spoiler (click to reveal)
Javascript has similar retyping issues. I think it can be a gotcha when using integers or strings as "true/false" values. One practice I've tried to use when dealing with this in Javascript is that when I have a comparison that isn't directly a boolean to make sure there is an equality check of some sort, rather than assuming that type-conversion will work like I expect, because it always seems to bite me at just the wrong time
4 
 Thumb up
1.00
 tip
 Hide
  • [+] Dice rolls
Virre Linwendil Annergård
Sweden
Stockholm
flag msg tools
Forza Bajen!
badge
... and still sober
Avatar
mbmbmbmbmb
sigmazero13 wrote:
virre wrote:
Spoiler (click to reveal)

To be honest it was something I learned today. It seems to be a retypeing in the if. Often it is the correct way to handle 0 but I do see it as a problem of the languages dynamic, weak typing.

Spoiler (click to reveal)
Javascript has similar retyping issues. I think it can be a gotcha when using integers or strings as "true/false" values. One practice I've tried to use when dealing with this in Javascript is that when I have a comparison that isn't directly a boolean to make sure there is an equality check of some sort, rather than assuming that type-conversion will work like I expect, because it always seems to bite me at just the wrong time


Spoiler (click to reveal)

The only thing I tested that seemed to work was a

count((string)$variable) > 0)

but I might have missed something.
As far as I remember, as a string with the value 0 apperently is empty TRUE too...
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Matt B
United States
Cincinnati
Ohio
flag msg tools
Avatar
mbmbmbmbmb
I know absolutely no PHP... my guess was the same as CrankyPants's.
3 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
CHAPEL
United States
Round Rock
Texas
flag msg tools
badge
"that's a smith and wesson, and you've had your six"
Avatar
mbmbmbmbmb
Spoiler (click to reveal)
Out message is "*0"
3 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Scott Lewis
United States
Thornton
Colorado
flag msg tools
NFHS Football & Basketball
badge
Dread Our Coming, Suffer Our Presence, Embrace Our Glory (Solonavi War Cry)
Avatar
mbmbmbmbmb
Spoiler (click to reveal)
Hmm, you're right; Ah, the odd idiosyncrasies of weakly typed languages.

Another thing you could do potentially is something like this:

($variable."").isEmpty()

A lot of languages with strings have a method like that to tell you if the string is empty, and one quick-and-dirty way to cast to a string in many weakly typed languages is just to append an empty string to it. I'm not 100% sure if this would work in PHP though.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Virre Linwendil Annergård
Sweden
Stockholm
flag msg tools
Forza Bajen!
badge
... and still sober
Avatar
mbmbmbmbmb
MWChapel wrote:
Spoiler (click to reveal)
Out message is "*0"


Spoiler (click to reveal)

Sorry you are incorrect. Try it out (also if it would print that please note version of php. It might be that we are still on php 5.2 at work.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Sean Ahern
United States
Spokane
Washington
flag msg tools
badge
Avatar
mbmbmbmbmb
Spoiler (click to reveal)
I do work in Drupal 7 so I figured it was probably that if statement. is_null() or empty() should work fine. However, is_string() would probably work the best for the example given.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Virre Linwendil Annergård
Sweden
Stockholm
flag msg tools
Forza Bajen!
badge
... and still sober
Avatar
mbmbmbmbmb
BookandGame wrote:
Spoiler (click to reveal)
I do work in Drupal 7 so I figured it was probably that if statement. is_null() or empty() should work fine. However, is_string() would probably work the best for the example given.


Spoiler (click to reveal)

empty() does not work in php 5.2 at least. I am however home so I do not ahve the test code, it might work with a (string)$value typecast for empty.
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Sean Ahern
United States
Spokane
Washington
flag msg tools
badge
Avatar
mbmbmbmbmb
virre wrote:
BookandGame wrote:
Spoiler (click to reveal)
I do work in Drupal 7 so I figured it was probably that if statement. is_null() or empty() should work fine. However, is_string() would probably work the best for the example given.


Spoiler (click to reveal)

empty() does not work in php 5.2 at least. I am however home so I do not ahve the test code, it might work with a (string)$value typecast for empty.


Spoiler (click to reveal)
You're right, I was thinking of isset().
2 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Front Page | Welcome | Contact | Privacy Policy | Terms of Service | Advertise | Support BGG | Feeds RSS
Geekdo, BoardGameGeek, the Geekdo logo, and the BoardGameGeek logo are trademarks of BoardGameGeek, LLC.