Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does echo() Accept numbers?

Tags:

php

echo

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?

like image 636
Bill Avatar asked Aug 05 '11 15:08

Bill


People also ask

What is the use of echo () function?

The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

How do you write echo?

echo is a statement, which is used to display the output. echo can be used with or without parentheses: echo(), and echo. echo does not return any value. We can pass multiple strings separated by a comma (,) in echo.

What is echo in PHP with example?

They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print .

What does Echo Echo 127 mean in Linux?

echo $? will return the exit status of last command. You got 127 that is the exit status of last executed command exited with some error (most probably). Commands on successful completion exit with an exit status of 0 (most probably). The last command gave output 0 since the echo $v on the line previous finished without an error.

How to include a string in an echo command in Linux?

Use > or >> to include the string in an echo command in a file, instead of displaying it as output: If the specified text file doesn’t already exist, this command will create it. Use the cat command to display the content of the file:

Why does the last command of echo give output 0?

The last command gave output 0 since the echo $v on the line previous finished without an error. v=4 echo $v echo $? 4 (from echo $v) 0 (from echo $?)

Why does echo $$ in Bash return a number like 7190?

Why does running echo $$ in bash return a number like 7190, while running echo $ only returns a $? Convention. $$: Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell (see the link to the manual below).


2 Answers

echo will cast its arguments to a string.

like image 57
nickf Avatar answered Nov 05 '22 05:11

nickf


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.

like image 24
hakre Avatar answered Nov 05 '22 05:11

hakre