GWW
GWW

Reputation: 44093

PHP's automatic string conversion

I couldn't find any useful explanation as to why PHP's automatic string casting is a useful feature? To me it seems like it can cause more bugs and issues than it solves. Not to mention the extra CPU cycles it takes to check each string if it needs to be converted. I can't think of any examples where this functionality is beneficial but perhaps I'm not being imaginative enough or I'm to used to using method calls like atoi or python's built in int() method.

Hopefully someone on here has an idea behind the rationale for this.

Upvotes: 0

Views: 143

Answers (2)

user229044
user229044

Reputation: 239290

What do you mean by "string casting"? Are you talking about how numeric and string types can change dynamically based on how they're used, without explicit casts?

It's a useful feature if you don't consider strong typing important. I believe this and many of PHP's features were original implemented to make it easy to learn. It's hardly the only language that juggles types this way, and there's nothing at all wrong with it. Wikipedia lists many languages which a similar type system: Erlang, Groovy, JavaScript, Lisp, Lua, Objective-C, Perl, PHP, Prolog, Python, Ruby, Smalltalk and Tcl

CPU cycles and performance were the last thing on the developers mind, and maybe one PHP-based site in a million actually needs to worry about those extra cycles.

You say you're used to using atoi; how is that more useful than just treating a variable like its contents are numeric and letting the language figure out the rest?

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

PHP wasn't built around concepts such as "logic" or "design"; it just sort of "growed" into something intended to be easy to use. As such, some of the features either don't make sense, or have a "hacked-in" feel. This is one of them.

Upvotes: 4

Related Questions