Does echo () use accept numbers?

I am relatively new to programming and still don't know when I am reading the documentation in the language. Description of php echo from php.net:

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

Thus, it seems to me that the input parameter should be of type string. However, echo works with numbers. For example, the code ...

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

... successfully prints 3 per page. Then should the parameter type for $ arg1 and $ ... be the mixed pseudo-type (and not just a string) to indicate that the echo will accept strings or numbers? Otherwise, how can I conclude from the documentation that the number parameters are acceptable?

+7
source share
6 answers
+8
source

Then should the parameter type for $ arg1 and $ ... be a mixed pseudo-type (and not just a string) to show that the echo will accept strings or numbers?

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

To be more precise, string freely typed :

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

Since this is for variables, this also applies to other types of expressions : the type of expression is determined by the context in which the expression is used .

In your case, expression 3 used in the context of the string of the echo function. PHP has no problem to use 3 as a string, so you won't get an error, and it displays (as a string).

Otherwise, how could I conclude from the documentation that the number parameters are acceptable?

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

+3
source

Php handles var rather weakly, read here:

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

+2
source

php can automatically change the type (right or wrong, depending on which corner you are standing on), so in most cases it takes little care ...

+1
source

It is not that echo changes the type of anything.

echo() is a language construct whose parameters will be processed as strings. Thus, he expects a bite when each parameter is passed to him.

PHP at the language level, however, has no problems with manipulating data types, since it is a dynamic, freely typed language (like Lisp or Perl, and compared to the strict Python or C type).

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

Each value has its own type. Say, for example, that 4 is an integer value. This will always be true. So, if you check the integrity of the type, isint(4) will always return true (like boolean ).

However, the approach in PHP is that it should be a language that takes care of progressing to the necessary types at runtime, and not that the programmer should think about the types of variables that can even be changed in some use cases (e.g. , general quick sort function).

Thus, whenever you use an expression as part of another, there is a type inference process by which each subexpression advances to the deduced type (part of a partial expression that has already been parsed).

So let's say we have the expression "13" . 37 "13" . 37 . In PHP . is a string concatenation operator. "13" is an expression of type string . So, the right operator . must be a string. Since this is not the case, the Zend Engine is looking for an easy way to push the 37 integer expression into a string. He finds one: the string "37" generated by the expression C printf("%d", value) .

The same thing happens with any data type. This can be somewhat troubling in advanced programming topics, since it is not always necessary to simply check the value of an expression, but also its type, since automatic type promotion can lead to a truth value that a naive programmer might have left uncontrollable without dealing with expression types .

This is especially 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 an ASCII byte representation, use chr ()


1 There are exceptions, such as the namespace construct, the declare construct, this is not Lisp: -D

0
source

AFAIK php translate variables (especially mixed vars) into a string

such as..

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

if you select $ myArray, this will result in

Array

0
source

All Articles