• Post Reply Bookmark Topic Watch Topic
  • New Topic

String to Numerical Data  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I apologize if this isn't the correct place for this. As I was looking, I couldn't find a spot that seemed to fit this question best.

This question originates from a PHP discussion, but I tried to consider the answer in java, and realized I couldn't come up with it in Java, so I became curious to that as well.

A group of my classmates and I were discussing strings. We were asked, "What circumstances would you want to convert a text string to numerical data?" but we couldn't think of any answers outside of counting characters within the string for various applications. Again, sorry if this isn't necessary the best place to post this, we were just curious. Our PHP discussion made me think of Java and I couldn't think of an answer for it.
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a user types a string into a GUI field, or passes a URL parameter from HTML, it may be that the string in question was supposed to represent a number. Like the user's age or salary or lucky number or... you name it. So it's necessary to convert that string to a numeric value.

I'm sure that sort of thing happens in PHP as well, but I don't know PHP. Perhaps it's an automatic coercion?
 
Sheriff
Posts: 4930
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm sure that sort of thing happens in PHP as well, but I don't know PHP. Perhaps it's an automatic coercion?


Yes, in php you don't need to specify primitives data types, php decides it for you. Not even primitives, strings, arrays as well. This is the part many people think they like it about php not to worry about data type. Personally I found it confusing, as you never know, you look at the variable you think it represents an integer for instance at one point, but actually you could have an array of strings in it at the other point, so it can be difficult to debug.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!