So who here knows PHP?
-
Trying to understand some PHP code I ran across. I've read PHP before, (never had to write it, thankfully,) but this is using some syntax I'm not familiar with.
$myArr = array(); //repeated several times $newVal = new Foo(bar, baz); $myArr[] = $newVal;
Making an educated guess here, does
$myArr[] =
mean "append new value to the end ofmyArr
? Because that's the only thing that makes any sense, but my brain looks at that and goes .
-
@masonwheeler that's right. That's one way to append to an array in PHP. I forget the other (s) but they're a bit more normal looking.
-
That's impressively wacky.
-
If = means append, then what's the syntax for assigning something to an array object?
-
@stillwater said in So who here knows PHP?:
If = means append, then what's the syntax for assigning something to an array object?
Presumably
$myArr = $newArray;
- leaving off the indexer brackets.
-
@stillwater The
=
doesn't mean append. It's just simple assignment. When you're assigning a value to a specific position in an array, the square braces are supposed to hold the key at which the item should be stored. If you omit the key, then the default is to add the item at the end of the array, so it's exactly the same as pushing the item onto the array.
-
@masonwheeler said in So who here knows PHP?:
Trying to understand some PHP code I ran across
. I've read PHP before, (never had to write it, thankfully,) but this is using some syntax I'm not familiar with.$myArr = array(); //repeated several times $newVal = new Foo(bar, baz); $myArr[] = $newVal;
Making an educated guess here, does
$myArr[] =
mean "append new value to the end ofmyArr
? Because that's the only thing that makes any sense, but my brain looks at that and goes .FTFY but not actually because that tag didn't do what I wanted it to
-
@masonwheeler said in So who here knows PHP?:
Making an educated guess here, does
$myArr[] =
mean "append new value to the end ofmyArr
?As I understand it, PHP arrays are dictionaries in which you can omit any keys you like, in which case items are referenced by index. Using
$myArr[] = "foo"
will add it with an index equal to the largest integer index currently in the array, +1.
-
@Gurth said in So who here knows PHP?:
@masonwheeler said in So who here knows PHP?:
Making an educated guess here, does
$myArr[] =
mean "append new value to the end ofmyArr
?As I understand it, PHP arrays are dictionaries in which you can omit any keys you like, in which case items are referenced by index. Using
$myArr[] = "foo"
will add it with an index equal to the largest integer index currently in the array, +1.This, plus:
- Array keys can be integers or strings . Other numerics and booleans are cast to integers.
- Objects can't be used as keys; it's an error.
null
and not-set values are coerced to the empty string though. - Integer-looking keys, like "34", will be cast to integers (only trimmed decimal integer literals though)
- Explicit integer keys can be negative, but generated keys start at 0.
<?php $arr = array( -7 => "Key is integer -7", "Omitted, autogenerated key is zero", // You can omit keys even when you specify others. Fun "4" => "Key is integer 4", "Autogenerated key is 5", 3.14 => "Key is truncated to integer 3", TRUE => "Boolean is coerced to integer 1", "1004 " => "Key is string \"1004 \"", " 1004" => "Key is string \" 1004\"", "1004" => "Key is integer 1004", 0x0A => "Key is integer 0x0A, aka 10", "0x0A" => "Key is string \"0x0A\"", );
-
@Gurth said in So who here knows PHP?:
As I understand it, PHP arrays are dictionaries in which you can omit any keys you like, in which case items are referenced by index.
I'm pretty sure that the
[]
syntax specifically only works for setting values.Using
$myArr[] = "foo"
will add it with an index equal to the largest integer index currently in the array, +1.Slight correction: the key will be equal to the largest non-negative integer key plus one, or 0 if the array has no keys matching that description.
Note also that using
unset
to delete the last item(s) from the array will leave a sparse array, but won't change the maximum integer key. So if you delete the last item and then use[]
to push a new item, there will be a gap in the keys.
-
@anotherusername said in So who here knows PHP?:
I'm pretty sure that the
[]
syntax specifically only works for setting values.Yeah, I should have mentioned I meant when setting values, not for retrieving them. Trying that gives:
php > echo $a[]; Fatal error: Cannot use [] for reading in php shell code on line 1
As a further , why doesn’t invoking the PHP interpreter from the shell with no arguments, cause it to go into interactive mode? I had to do
php -h
to find I neededphp -a
, else it’s just something you can type into but which seems to do nothing except respond to^C
.
-
@Gurth said in So who here knows PHP?:
something you can type into but which seems to do nothing except respond to
^C
.Since it's not interactive, you have to type ^D or ^Z depending on platform to signal the end of the document.
-
@heterodox Oh, that works too. But in any case that wasn’t my complaint :)
-
@Gurth said in So who here knows PHP?:
why doesn’t invoking the PHP interpreter from the shell with no arguments, cause it to go into interactive mode?
I honestly don't remember if this worked or not, and I'm not going to get PHP on me again just for testing, but have you tried typing
<?php
before typing other code?
-
-
-
I assume piping cat to it or feeding it a heredoc or herestring also works. Just not interactively.
Again, .
-
-
-
@TimeBandit That isn't piping a cat to any destination, though.
-
@masonwheeler Ok then, this will get it to some destination
-
@TimeBandit Poor kitty!
-
@masonwheeler Don't worry, he's got 9 lives
-
@TimeBandit said in So who here knows PHP?:
@masonwheeler Don't worry, he's got 9 lives
But that's gonna take about 12 of them, especially the landing.
-
@Benjamin-Hall said in So who here knows PHP?:
especially the landing
I doubt that there's gonna be anything left to land
-
@TimeBandit When something's fired out of one of those, land is usually down - right - up - left - down - right - ...
...uh, land is usually straight ahead.
-
-
@masonwheeler said in So who here knows PHP?:
@TimeBandit That isn't piping a cat to any destination, though.
Depends on what's in the pipe...
-
-
@masonwheeler said in So who here knows PHP?:
I think he was pointing out spin-stabilized ammunition.
INB4 "But that's a smooth-bore artillery piece" or "It doesn't have enough rifling to do that"
-
-
@TimeBandit said in So who here knows PHP?:
# echo "<?php echo \"Testing\";" | php
But this works
It's probably reading it in a buffered way.
Why the PHP command doesn't just check whether stdin is a tty, I don't know.
-
@ben_lubar said in So who here knows PHP?:
Why the PHP command doesn't just check whether stdin is a tty, I don't know.
Probably a rare enough occurrence that it's not worth their while.
-
@ben_lubar said in So who here knows PHP?:
@TimeBandit said in So who here knows PHP?:
# echo "<?php echo \"Testing\";" | php
But this works
It's probably reading it in a buffered way.
Why the PHP command doesn't just check whether stdin is a tty, I don't know.
It wouldn't be beyond PHP to check how many bytes are available in the stdin buffer, find none because the TTY interaction has only just started, then simply sit there because there's nothing to do.
Anyway, you should use
php -a
if it was included at compile time.
-
Interactive Shell and Interactive Mode are not the same thing, despite the similar names and functionality.
If you type 'php -a' and get a response of 'Interactive Shell' followed by a 'php>' prompt, you have interactive shell available (PHP was compiled with readline support). If instead you get a response of 'Interactive mode enabled', you DO NOT have interactive shell available and this article does not apply to you.
You can also check 'php -m' and see if readline is listed in the output - if not, you don't have interactive shell.
Interactive mode is essentially like running php with stdin as the file input. You just type code, and when you're done (Ctrl-D), php executes whatever you typed as if it were a normal PHP (PHTML) file - hence you start in interactive mode with '<?php' in order to execute code.
Interactive shell evaluates every expression as you complete it (with ; or }), reports errors without terminating execution, and supports standard shell functionality via readline (history, tab completion, etc). It's an enhanced version of interactive mode that is ONLY available if you have the required libraries, and is an actual PHP shell that interprets everything you type as PHP code - using '<?php' will cause a parse error.
So with just
php
you can indeed type code (that must begin<?php
) and it will execute when you hit^D
. Withphp -a
it executes each line as you pressReturn
.And I think what happened earlier is that in my quick testing with an
echo
command, I lost track of the output because it doesn’t put a newline after it, meaning my prompt got tacked straight onto the back of it.
-
@Gurth Just think of all the things you might have learned instead of this. Aren't you lucky.
-
-
@pie_flavor said in So who here knows PHP?:
@Gurth said in So who here knows PHP?:
^D
Or
^Z
on Windows.You need to push enter after you push
^Z
on Windows.
-
@ben_lubar said in So who here knows PHP?:
@pie_flavor said in So who here knows PHP?:
@Gurth said in So who here knows PHP?:
^D
Or
^Z
on Windows.You need to push enter after you push
^Z
on Windows.I think this might apply to any sufficiently retarded terminal.