Home » Php » php – Does echo() Accept numbers?

php – Does echo() Accept numbers?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m relatively new to programming and am still unsure of myself when reading language documentation. The description of PHP’s echo from php.net is:

void echo ( string $arg1 [, string $... ] )

Thus it seems to me that the input parameter (s) must be of type string. However, echo works with numbers. For example, the code…

<p><?php echo 3; ?></p>

…successfully prints 3 to the page. Then shouldn’t the parameter type for $arg1 and $… be the pseudo-type mixed (instead of just string) to show that echo will accept strings or numbers? Otherwise, how would I be able to infer from the documentation that number parameters are acceptable?

How to&Answers:

echo will cast its arguments to a string.

Answer:

Then shouldn’t the parameter type for $arg1 and $… be the pseudotype mixed (instead of just string) to show that echo will accept strings or numbers?

Actually in the manual, string is sort of a pseudo-type as well. Not a real pseudo-type like mixed is (see Pseudo-types and variables used in the PHP documentation).

To be more precise, string is loosely typed:

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. [highlight by me] That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

As this is for variables, the same applies to other types of expressions: an expression’s type is determined by the context in which the expression is used.

In your case, the expression 3 is used in the string context of the echo function. PHP has no problem at all to use 3 as string, so you don’t get an error and it’s displayed (as string).

Otherwise, how would I be able to infer from the documentation that number parameters are acceptable?

echo expects string parameters. When you pass a (variable) expression that is a string, a number, a boolean, NULL or a Resource (see Types), all these types are used as strings. So whenever you see string as the type, just use a string expression. It does not mean that you need to explicitly define an expression to be string to make it work, as PHP does not have explicit type definition.

Answer:

Php handles var rather loosely, read more here:

http://php.net/manual/en/language.types.type-juggling.php

Answer:

php can automagic change type (rightly or wrongly depending on which corner you stand) so it cares little in most cases…

Answer:

AFAIK php does cast variables (especially mixed vars) into string

like for example..

$myArray = array(1,2,3);

if you will echo out $myArray it will result into

Array

Answer:

It’s not that echo changes the type of anything.

echo() is a language construct whose parameters will be treated as strings. So, it expects a sting as each parameter passed to it.

PHP at the Language Level, however, doesn’t have any problem mangling with the data types, as it is a dynamic loosely typed language (as Lisp or Perl, and as compared to Python or C’s strict typing).

So, even if (almost)1 everything you say is basically an expression, each of these sentences have a return value.

Every value has an intrinsic type. Let’s say, for example, that 4 is a value of the integer type. This will always be true.
So, if you test for type integrity, isint(4) will always return the value true (of type boolean).

However, the approach in PHP is that it should be the language who cared about promoting to the needed types in runtime, not about the programmer having to think about the types of the variables, which might even be mutable in some use cases (like a generic quick sort function construction).

So whenever you use an expression as part of another, there is a process of type inference by which every sub-expression is promoted to the inferred type (by the part of the super-expression which has been already parsed).

So let’s say we have the expression "13" . 37. In PHP, the . is the string concatenation operator.
"13" is a string-type expression. So then, the right operator of the . is expected to be a string. As it is not, the Zend Engine looks for an easy way of promoting the 37 integer expression to string. It finds one: the string "37", generated by a C printf("%d", value)-like expression.

The same inference process happens with any data type. This can be somewhat disturbing in advanced programming topics, as one must not always just check about the value of an expression, but also the type of it, as the auto-promotion of types may be triggering a truth value that a naïve programmer could have left unchecked by not dealing with expression types.

This is particularly true in environments such as the following:

if     ($anything == 6) { /* Will be triggered also with "6" */ }
elseif (!$anything) { /* will be triggered with the empty string too */ }
elseif (is_string($anything)) { /* will not be triggered with "6" nor "" because of the else */ }
elseif ($anything == true) { /* will be triggered with anything that is not false:
    1, "1", 4, "anything", myObjectInstance(), AND also with the
    string "false", that is promoted to boolean true. (Boolean false is the empty string) */ }

If you are looking for the ASCII representation of a byte, use chr()


1 There are exceptions, like the namespace construct, the declare construct, this is not Lisp 😀