Analyzing Arguments Sent to a Function

Here is a random trick in PHP that I stumbled upon today. I was trying to split a string into a number pieces and I was passing an equation of variables into substr. I wasn't getting the result that I was expecting, so I began to look at what data I was passing into it one by one. Then I realized that I could just look at the whole thing by doing something like this.

Take this sample piece of code.

$x = substr($y, ($a + 8), ($b - $a + 8));

I did some calculations to find $a and $b and I was trying to use those numbers to extract a particular piece of the string $x. Instead having to echo $x, $y, $a, and $b individually, I just changed the line to this.

echo "$x = substr($y, ($a + 8), ($b - $a + 8));";

And it very plainly revealed to me what the second and third arguments (start and length) were evaluating to.

= substr(the random string I was extracting from, (22 + 8), (35 - 22 + 8));

This kind of seems like a pretty basic trick to use, but it was just something I never really thought about trying before and it turned out to work pretty well.

Leave a Reply

{
}