Question:
How long is a piece of string?
APPELLANT53
2007-08-09 15:57:19 UTC
How long is a piece of string?
31 answers:
Yarnlady_needsyarn
2007-08-10 13:36:46 UTC

echo "This is a constant string";

?>


echo "This is an interpolated $string";

?>

INIT STRING ~0

ADD_STRING ~0 ~0 'This'

ADD_STRING ~0 ~0 ' '

ADD_STRING ~0 ~0 'is'

ADD_STRING ~0 ~0 ' '

ADD_STRING ~0 ~0 'an'

ADD_STRING ~0 ~0 ' '

ADD_STRING ~0 ~0 'interpolated'

ADD_STRING ~0 ~0 ' '

ADD_VAR ~0 ~0 !0

ECHO ~0

Where !0 represents the compiled variable named $string. Looking at these opcodes: INIT_STRING allocates an IS_STRING variable of one byte (to hold the terminating NULL). Then it's realloc'd to five bytes by the first ADD_STRING ('This' plus the terminating NULL). Next it's realloc'd to six bytes in order to add a space, then again to eight bytes for 'is', then nine to add a space, and so on until the temporary string has the contents of the interpolated variable copied into its contents before being used by the echo statement and finally discarded. Now let's rewrite that line to avoid interpolation and use concatenation instead:








Does this come out to a simple, single opcode? Nope, because the rules necessary to catch a heredoc's end token demand the same careful examination as double-quoted variable substitution and you wind up (in this case) with SEVENTY-EIGHT opcodes! One INIT_STRING, 76 ADD_STRINGs. and a final ECHO. That means a malloc, 76 reallocs, and a free which will be executed every time that code snippet comes along. Even the original contents take up more memory because they're stored in 76 distinct zval/IS_STRING structures.







Why does this happen? Because there are about a dozen ways that a variable can be hidden inside an interpolated string. Similarly, when looking for a heredoc end-token, the token can be an arbitrary length, containing any of the label characters, and may or may not sit on a line by itself. Put simply, it's too difficult to encompass in one regular expression.

If you're using APC, you'll find just such an optimizer built in, but not enabled by default. To turn it on, you'll need to set apc.optimization=on in your php.ini. In addition to stitching these run-on opcodes back together, it'll also add run-time speed-ups like pre-resolving persistent constants to their actual values, folding static scalar expressions (like 1 + 1) to their fixed results (e.g. 2), and simpler stuff like avoiding the use of JMP when the target is the next opcode, or boolean casts when the original expression is known to be a boolean value. (It should be noted that these speed-ups also break some of the runtime-manipulation features of runkit, but that was stuff you....probably should have been doing anyway)







Can't use an optimizer because your webhost doesn't know how to set php.ini options? You can still avoid 90% of the INIT_STRING/ADD_STRING dilema by simply using single quotes and concatenation (or commas when dealing with echo statements). It's a simple trick and one which shouldn't harm maintainability too much, but on a large, complicated script, you just might see an extra request or two per second.





I found this answer by typing the question into my search box.
lauer
2016-11-12 02:38:59 UTC
a sprint bit string is going right into a pub an sits down. The bartender says you will possibly desire to bypass away we dont serve string here. The string is going out to the line and asks a guy to tie him in a knot. the guy does it yet as he ties the knot the ends of the string become frayed. The string says thats ok and walks back into the pub a sits down. The bartender says, are not you that piece of string I kicked out of here till now. The string says no i'm a frayed knot.
Becka
2007-08-09 16:01:49 UTC
How is 'How long is a piece of string?’ a pseudo-question? It might be vague, but every explanation always is, and it seems absurd to say that vague answers are not truth-evaluable according to non-vague criteria. IE: if you were to specify the answer in centimetres by default, but refuse to do it in millimetres or micrometres, you should not be faulted, and your answer would be satisfying. If we can't even answer those questions, then no mundane questions can be answered either (unless you were to specify the appropriate conditions of an answer within the question). =}
painterlady
2007-08-09 16:09:13 UTC
It all depends how long you want it, just curious what are you doing with the string?.
firebobby
2007-08-09 16:01:18 UTC
Is this a metric or imperial piece of string?

If metric,then yard for metre it has to be longer.
Kev E
2007-08-09 16:01:35 UTC
4 inches

That's my string theory
anonymous
2007-08-09 16:02:11 UTC
No, How Long is a China man
LEANNE
2007-08-09 20:09:03 UTC
Normaly a little bit shorter than the job you need it for.
Honeypai
2007-08-09 16:09:23 UTC
It's whats left of the part it was cut from.
Diminished
2007-08-09 16:04:57 UTC
Its as long as you want it to be.
anonymous
2007-08-09 16:00:47 UTC
from one end to the other and all in between.
Wayne ahrRg
2007-08-10 06:27:10 UTC
Shorter than the two peices it was cut from.
anonymous
2007-08-09 16:00:08 UTC
If you tie one end to the other then you have an endless loop.
princess t
2007-08-09 16:03:35 UTC
As long as you want it to be...

or

if you find/buy it it then it's as long as whatever it measures...

lol
Briu1970
2007-08-09 16:08:11 UTC
Unknown quantity - unable to answer.
anonymous
2007-08-09 16:07:43 UTC
from the middle to the end twice
anonymous
2007-08-09 16:04:31 UTC
as long as you want
anonymous
2007-08-09 16:00:41 UTC
Twice half it's length.



No why did I say that! People at school used to say that and it fucked me off.
anonymous
2007-08-09 16:00:19 UTC
8 shillings and six pence
JillPinky
2007-08-09 16:03:55 UTC
Never long enough.
angel
2007-08-09 16:00:20 UTC
as long as you intent it to be?

one of life's mysteries!
Misty Blue
2007-08-09 17:21:04 UTC
Same as how short it is.
anonymous
2007-08-09 16:05:57 UTC
long enough for us all to share
anonymous
2007-08-09 16:11:29 UTC
Just a minute.
18/M/Scotland
2007-08-09 16:00:45 UTC
not too short and not too long..



however much you cut it ma boy :D
anonymous
2007-08-09 15:59:25 UTC
aPIECE OF STRING IT HAS NO END TO ANSWER THIS I DO INTEND LOL XX
schmagum
2007-08-09 21:15:10 UTC
Almost as long as my ****. Almost.
....
2007-08-09 16:05:01 UTC
as long as it is....
Rozzaxw1
2007-08-09 15:59:48 UTC
depends how long you cut it to......duhhhh
anonymous
2007-08-09 15:59:22 UTC
yes it is
hortense h
2007-08-09 15:59:41 UTC
2.5m


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...