Editing
PHP Skills Tests
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Is `print()` a function? === `print()` is a language construct. <ref>[http://php.net/manual/en/function.print.php print()], PHP documentation</ref> <syntaxhighlight lang="php"> echo ((function_exists("print"))?("y"):("n")); /* <<< output is "n" */ echo ((function_exists("printf"))?("y"):("n")); /* <<< output is "y" */ echo ((function_exists("echo"))?("y"):("n")); /* <<< output is "n" */ </syntaxhighlight> * Both `echo` and `print` are constructs, not functions. This makes parentheses deceiving. * Both `echo` and `print` will output everything that follows to the right. * `echo` does not return a value. * `print` ''always'' returns `1`, never the value to the right of it. * Also, the string concatenation operator (`.`) has high precedence in PHP. But that's a red herring in the following example. <syntaxhighlight lang="php"> echo '1'.print(2)+3; // result: 511 /** * `echo` in this example prints everything that comes after it. It's necessary to evaluate everything to the right of 'echo' first. * `print` also outputs everything to the right, regardless of parentheses. * (1) Add (2) + 3 results in the value 5 * (2) print 5 outputs 5 to stdout, and returns the value `1`. * (3) Concatenate '1' with the return value of 'print' results in "11" * (4) 'echo' the output after what's already been sent to stdout, so "11" follows the "5". */ </syntaxhighlight>
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information