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!
=== String concatenation and string operators === <syntaxhighlight lang="php"> echo (2).(3*(print 3)); // "323" </syntaxhighlight> * This example also plays on the obscure fact that the `print` construct always returns the value `1`. * `echo` evaluates everything to the right. * First, `print 3` outputs `"3"`. * Then 3 is multiplied by the return value of `print`, i.e. `1`, for a result of `3`; * `2` is concatenated to `3` for `"23"` * `echo` outputs `"23"` after the `"3"` that has already been output, for a result of `"323"`. ---- '''Q: What will be the output of the following code?''' <syntaxhighlight lang="php"> $foo = 5 + "10 things"; print $foo; // 15 </syntaxhighlight> {| ! a. | 510 things |- ! b. | 5 10 things |- ! c. | 15 things |- ! d. | 15 |- ! e. | None of the above |} '''A: (d) 15''' * One trick to this question is that there is no string concatenation operator (`.`) involved. * Still one thing to keep in mind is that the dot operator has the same precedence as `+` and `-`. * Non-empty non-numeric strings are converted to `0` when they are part of a mathematical operation. <ref>[http://php.net/manual/en/language.operators.string.php String Operators], PHP documenation</ref> * But a string is numeric if the numeric part comes at the beginning. ** `"10 things" = 10` ** `"things 10" = 0` ** `"10things" = 10` ** `"10 x 4" = 10` * `"10 things"` is converted to `10`, then added to `5`. * The final result is `15`.
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