# echo '1'.print(2) + 3; shows "511"???

**URL:** <https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238>\
**Category:** Factual Questions\
**Created:** [August 3, 2013, 9:34am UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238 "2013-08-03T09:34:22Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [August 7, 2013, 9:20pm UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/21 "2013-08-07T21:20:47Z")

</div>

> [@Walton\_Firm](#):
>
> C has strong typing: every variable has a clearly defined type, and the compiler makes sure that, under normal conditions, you cannot perform an operation on a variable which is not valid for its type. In most cases, C is more conservative in what it will allow qua explicit casting than e.g. Perl.

Perl doesn’t have casting, it has autoconversion. Casting is getting the runtime to ignore type information and reinterpret the same data. Autoconversion relies on type information and changes the underlying data.

> [@](#):
>
> If somebody wrote a Perl module which allowed you to do casting in Perl

Not possible from within Perl. Perl is strongly typed.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 7, 2013, 9:37pm UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/22 "2013-08-07T21:37:41Z")

</div>

Nobody really [quite agrees on what “strong typing” actually means](http://en.wikipedia.org/wiki/Strong_and_weak_typing). That said, if you go down the list of possible definitions of strong typing, C fails more of them than Perl.

Even so, IMO the very existence of void\* as a construct that requires no special libraries to use and carries no additional compiler warning by default pretty much makes it one of the most weakly typed languages. (Compare to, say, Go which **has** unsafe.Pointer with roughly the same function, but without unsafe imported it will only let you do type conversion and with unsafe imported things like Google App Engine won’t even let you upload it because of the safety dangers. And the unsafe package is actually implemented in the standard library in languages other than pure Go because doing what unsafe does is not possible with just the syntax of the language)

But this is still all definitions.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [August 7, 2013, 9:37pm UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/23 "2013-08-07T21:37:43Z")

</div>

> [@Derleth](#):
>
> Perl and PHP both have strong typing in that programs always fail in defined and controlled ways, as opposed to C, where you can use casts to make programs fail in ways which can’t be controlled because you’ve gone beyond what the language’s runtime can control, and what happens next is up to the OS, the hardware, or both.

We’ve been over this before in a different thread. In short, you have a definition of “storng” vs. “weak” typing which doesn’t match my own. I doubt that either of us can come up with proof that our definition is the one and only correct one.

> [@Derleth](#):
>
> Perl doesn’t have casting, it has autoconversion. Casting is getting the runtime to ignore type information and reinterpret the same data. Autoconversion relies on type information and changes the underlying data.
> 
> Not possible from within Perl. Perl is strongly typed.

The pack and unpack functions allow you to reinterpret data. Any language with sufficiently powerful file IO also allows reinterpretation.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 7, 2013, 9:40pm UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/24 "2013-08-07T21:40:35Z")

</div>

> [@Dr.Strangelove](#):
>
> The pack and unpack functions allow you to reinterpret data. Any language with sufficiently powerful file IO also allows reinterpretation.

If we wanna go there, any language that’s provably Turing Complete will let you do anything. 😛

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [August 7, 2013, 10:26pm UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/25 "2013-08-07T22:26:42Z")

</div>

> [@Jragon](#):
>
> Even so, IMO the very existence of void\* as a construct that requires no special libraries to use and carries no additional compiler warning by default pretty much makes it one of the most weakly typed languages.

Sure, any pointer will autocast to void \*… but you can hardly use that for anything without additional casting (or warnings). You can use it to free memory, which is safe. You can’t do any pointer arithmetic, or pass it to functions that expect a typed pointer, or anything like that, without aditional casting.

I suppose that one reason for disagreements on the nature of typing is that of one’s goals as a programmer. For instance, it is extremely important to me to know about numeric conversions. C/C++ is fairly good in that respect: it always warns if I try to shove a double into a float, or an unsigned int into a signed int, etc. And of course it actually allows these different types in the first place.

Perl/PHP/etc. support very little of this. They freely move between floats, int, and strings, with little or no warning on loss of data. And of course the type system in general is extremely weak in the sense that I can’t even specify the type of number I want.

But they are safe, in the sense that I can’t crash the program or get into too much trouble. Ironically, this matters more for one off scripts where I don’t want to think too much about how to get things done. For serious programs I want more control, and can afford to give up some safety to achieve that.

Taking this line to the logical conclusion, you can imagine a language where absolutely everything is a string. All arithmetic works on strings. Data structures like arrays and hashes are always serialized/deserialized to a string. And so on.

By some narrow definition, this is a strongly typed language. How could it not be, if there’s only one type, and there’s no way to get at the internals of that type? And yet it seems a little odd, since I can easily break a hash table by tweaking its string representation. It’s strongly typed in some weird theoretical sense but not in any practical sense (at least not what _I_ would consider a practical sense).

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [August 7, 2013, 10:43pm UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/26 "2013-08-07T22:43:55Z")

</div>

> [@Derleth](#):
>
> Not possible from within Perl. Perl is strongly typed.

External modules can be written in C (or probably any language) and do whatever they want, including exposing internal representations to the script.

Sure, the module isn’t in Perl, but allowing non-Perl modules _is_ a Perl feature.

---

<div class="post-metadata">

**Author:** ![Walton\_Firm](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@Walton\_Firm](https://boards.straightdope.com/u/Walton_Firm)\
**Post date:** [August 8, 2013, 5:02am UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/27 "2013-08-08T05:02:15Z")

</div>

> [@Derleth](#):
>
> Perl doesn’t have casting, it has autoconversion. Casting is getting the runtime to ignore type information and reinterpret the same data. Autoconversion relies on type information and changes the underlying data.

If casting in C worked as you describe, then casting the number 33 from an integer into a float, would not result in 33.0 but rather some crazy value based on whatever that bit pattern means in IEEE floating-point representation. But that’s not how it works – the value will get properly converted, showing that the compiler is quite aware of the original type. (It won’t change the type of the original variable, since C is statically typed, but it will automatically create a temporary value on the stack if needed.)

In order to reinterpret an integer as a float _without_ conversion, you need to take its address, cast that from a pointer-to-integer to a pointer-to-float, and then access the same memory via the float-pointer. At that point you’re simply telling the compiler “trust me, I know what I’m doing”. But even this is still type-safe in the sense that the compiler knows what the type of each pointer is, knows how to convert between those types, and will not allow unsafe conversions unless you explicitly ask for them.

But when it comes to being aware of the type of every value in the code and not allowing operations between incompatible types unless you go out of your way to assure the compiler that that is what you want to do, C is actually a lot stricter and more nitpicky than Perl, which will happily autoconvert things implicitly at the drop of a hat, even when the result will almost certainly not be what the programmer intended. E.g. _cos(“three”)_ in Perl will silently autoconvert the string into the number 0, while C will give an error because you cannot call ‘cos’ with a string argument without explicitly telling the compiler how you want it to convert that string into a number.

[QUOTE=Jragon]  
Nobody really [quite agrees on what “strong typing” actually means](http://en.wikipedia.org/wiki/Strong_typing).  
[/QUOTE]

“In general, these terms do not have a precise definition. Rather, they tend to be used by advocates or critics of a given programming language, as a means of explaining why a given language is better or worse than alternatives.” Heh. 🙂

But apparently, the original definition from the people who invented the term, is “whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function.” And “in a strongly typed language each data area will have a distinct type and each process will state its communication requirements in terms of these types.”

That seems to describe C/C++ better than Perl, in my opinion, since the latter will almost never complain about calling a function with a value of the wrong type, but will just happily autoconvert that value even if the result probably won’t be what the programmer intended.

---

<div class="post-metadata">

**Author:** ![JohnClay](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JohnClay](https://boards.straightdope.com/u/JohnClay)\
**Post date:** [August 8, 2013, 5:09am UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/28 "2013-08-08T05:09:06Z")

</div>

> [@bump](#):
>
> Sure, it’s a logical comparison operator; what you’re saying is “does 5 equal 5?” with the return value of 1 if true, 0 if false.
> 
> So echo (5==4) would display 0, I’d think.

Like I wrote earlier it actually doesn’t display anything…

---

<div class="post-metadata">

**Author:** ![JohnClay](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JohnClay](https://boards.straightdope.com/u/JohnClay)\
**Post date:** [August 8, 2013, 5:41am UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/29 "2013-08-08T05:41:36Z")

</div>

> [@bump](#):
>
> Sure, it’s a logical comparison operator; what you’re saying is “does 5 equal 5?” with the return value of 1 if true, 0 if false.
> 
> So echo (5==4) would display 0, I’d think.

BTW…

```php
echo 'false:'.false;
echo '<br><br>';
echo 'true:'.true;
echo '<br><br>';

echo 'true == 1: ';
if (true == 1) {
	echo 'yes';
}
else {
	echo 'no';
}
echo '<br><br>';

echo 'true === 1: ';
if (true === 1) {
	echo 'yes';
}
else {
	echo 'no';
}

```

Displays:

```auto

false:

true:1

true == 1: yes

true === 1: no

```

So though “true” displays as “1” they’re not actually the same thing if you use ===

---

<div class="post-metadata">

**Author:** ![JohnClay](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JohnClay](https://boards.straightdope.com/u/JohnClay)\
**Post date:** [August 8, 2013, 6:32am UTC](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238/30 "2013-08-08T06:32:55Z")

</div>

BTW for the first bit:  
print(2) + 3 is the same as

print (2) + (3)…

(and print 2 + 3)

IMHO I don’t think support for brackets was explicitly coded in for print (and echo and include, etc) but it supports brackets like $a = ((5) \* ((2) + (3))) supports brackets…

[Previous page](https://boards.straightdope.com/t/echo-1-print-2-3-shows-511/665238.md?page=1)
