Comments (5)
This is expected! (Though I can totally see why you wouldn't expect it).
As a REPL, PsySH evals your code as soon as it can. It doesn't wait for input to be "done". It also doesn't require semicolons if the input is otherwise complete. So when you type:
foreach ($data as $pos)
{
echo $pos . "\n";
}
… you're really saying:
foreach ($data as $pos);
{
echo $pos . "\n";
}
The difference is subtle :)
The reason it outputs the final $pos
is that the loop iteration variable leaks out of the loop scope and the final value is available after the loop completes. To top it all off, your echo is surrounded by brackets but you can just … put brackets wherever you want in PHP. In this case they do nothing interesting at all. So your whole thing is equivalent to:
$data = [
"1111",
"2222",
"3333",
"4444",
];
$pos = end($data);
echo $pos . "\n";
To avoid this, you can:
- Change your style, and always put opening brackets on the same line while using the REPL. This is my personal preference, as it avoids other, similar sorts of issues.
- Force line continuation by adding a
\
at the end of the line (much like in Bash or other shells, which also execute as soon as they have valid input). - Or disable automatic semicolon insertion, via the
requireSemicolons
config option. This means PsySH will never execute anything until you've added all the semicolons, which can be a bit annoying, but it also prevents your issue here completely.
from tinker.
Note that this behavior only applies to line-by-line input. Inside another scope that already keeps the input buffer open (e.g. inside a function or class definition, or a namespace block) it will act like you expected, because treating the foreach
on its own line as a complete statement and executing it won't work inside another block.
It also doesn't apply to pasted input, input from STDIN, or input from the edit
command.
from tinker.
@bobthecow thanks a lot for that thorough explanation 👍
from tinker.
I don't think this is a tinker issue but rather a psysh one. Please try their repo or a support channel, thanks.
from tinker.
Oooh! I just thought of a fourth way!
If you only temporarily want to get the requireSemicolons
type experience, you can open a block by starting with a {
on its own line. Then all input will be buffered until you get to the closing }
.
This would work great for method chaining interfaces.
from tinker.
Related Issues (20)
- Tinker lost colors HOT 18
- How is idea of making laravel tinker that detect the updates in code instantly? HOT 3
- Add ability to refresh Tinker session from within Tinker? HOT 3
- Contextual binding doesn't appear to work in tinker HOT 4
- Add migrate:install to white listed commands HOT 1
- assert always returns true HOT 2
- Can't open Tinker because of a bad $options argument to Symfony HOT 2
- Commands inside tinker incorrectly parsed HOT 1
- Tinker broken in laravel 9.0.0 HOT 8
- Issue registering StorageLinkCommand HOT 2
- PHP Error: Call to a member function make() on null HOT 5
- Closures not working when using the --execute argument. HOT 1
- stream_wrapper_register(): Protocol hoa:// is already defined in psy/psysh/src/Readline/Hoa/ProtocolWrapper.php:473 HOT 1
- Recoverable fatal error: Object of class Illuminate\Foundation\Application could not be converted to string in Command line code on line 8 HOT 1
- Parse error: Syntax error, unexpected ':' in vendor\psy\psysh\src\Exception\ParseErrorException.php on line 40. HOT 1
- The destructor is not executed HOT 2
- Output cleared and missing colors after upgrading to Laravel 9 HOT 4
- First time i open tinker, i have to dispatch a job twice to get it pushed to queue HOT 1
- Silent crash when initializing a PDO connection to PostgreSQL HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinker.