Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Is PHP a sloppy language?

Ok, I learned the basics of programming with Pascal and C, all strongly typed languages. A weekly typed language like PHP is driving me crazy.

I give few examples:

$testvar = "0"; if ($testvar) { echo "the string is true" . "\n"; } else { echo "the string is false" . "\n"; }

The result is false: PHP converted the String "0" to boolean false

$testvar = "1"; if ($testvar) { echo "the string is true" . "\n"; } else { echo "the string is false" . "\n"; }

predictably the result is true

$testvar = "false"; if ($testvar) { echo "the string is true" . "\n"; } else { echo "the string is false" . "\n"; }

The result here is true!!! Why did PHP failed to covert the string to boolean?

Another example

$a = "1000" + 2; echo $a . "\n";

The result is 1002

$a = "1000.2" + 2; echo $a . "\n";

The result is 1002.2

Let's suppose that we use a comma to separate 3 decimals

$a = "1,000.2" + 2; echo $a . "\n";

The result is 3!!!

Now, let's suppose that we are in a non Anglo-Saxon country where we use the "," to separate fractional numbers.

$a = "1000,2" + 2; echo $a . "\n";

The result is 1002!!!

My take is that type conversion in PHP are far from being predictable, additionally the language isn't giving you any warnings. What is the logic for type conversion in PHP? Is there a documentation for type conversions?

Cheers Luca

5 Answers

Not too sure what you mean by a "sloppy" language. $testvar = "false"; if ($testvar) { echo "the string is true" . "\n"; } else { echo "the string is false" . "\n"; }. This statement will return true because a string literal "false" is not the same thing as $testvar = false; Each char in a string represents an ASCII char and is stores a an array of char. false = 0, but "false" = some number greater than 0. Any number greater than 0 will return true. I hope that makes sense.

Jason, Using the same logic $testvar = "0"; if ($testvar) { echo "the string is true" . "\n"; } else { echo "the string is false" . "\n"; } should return true as the ascii code for the char "0" is 0x30 (or 48 in decimal) different than 0. Instead it returns false. Where is the logic here?

I see your point. That is kinda funky.

when you are put the number it does NOT need put double quote . put just only number without the double or single quote. the double quote is for string.

also when you are set up the variable string which is always true statement. if not exist variable it will run the else.

Hi Brian,

I don't think you understand my point. In the examples I was typing, I tested the automatic type conversion of the PHP language. When you try to do operations with variables or constants of different types (in my case a string and an integer), different languages will react in different ways. A language like Pascal or Ruby will throw an error, forcing you to use the appropriate type conversion function. A language like PHP or JavaScript will automatically convert types and do the operation nevertheless. for example:

z:= '7' + 8; PASCAL will give you an error during compilation

var z = "7" + 8; the JavaScript result will be "78" (convert the integer to string and concatenate the two strings)

$z = "7" + 8; the PHP result will be 15 (convert the string to integer and calculate the sum of two integers)

My point is that PHP type conversion is a bit unpredictable. I am sure with time and practice I will get the hang of it.

Cheers Luca

By the way a string of "0" will be converted to a false boolean

very interesting!