4

more out of interested...

$_GET['unique'] = blahblahblah=this_is_what_im_interested_in

I know I can get the second element like this:

$words = explode('=', $_GET['unique']);
echo $words[1];

Is there a way to get this in a single line? - that would then 'hopefully' allow me to add that to function/object call:

$common->resetPasswordReply(... in here I would put it....);

like

$common->resetPasswordReply(explode('=', $_GET['unique'])[1]);

I'm just interested to see if this is possible.

3
  • In recent enough versions of PHP, sure. That particular... weakness of the PHP parser was only fixed within the past half-year or so. Commented Jun 12, 2013 at 4:55
  • 1
    another funny way is using array_shift twice like $common->resetPasswordReply(array_shift(array_shift(explode('=', $_GET['unique'])))); Commented Jun 12, 2013 at 4:56
  • 1
    Your last line of code should already work, had you tried it before you would not need to even ask Commented Jun 12, 2013 at 5:04

3 Answers 3

8

PHP supports the indexing on functions if they are returning arrays/objects; so the following would work too:

echo explode('=', $_GET['unique'])[1];

EDIT

This is termed as array dereferencing and has been covered in PHP documentations:

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

As of PHP 5.5 it is possible to array dereference an array literal.

Sign up to request clarification or add additional context in comments.

1 Comment

This is true as of 5.4, older versions don't support it.
1

This should do it for you.

substr($str, strpos($str, "=")+1);

Comments

1

Strangely enough you can 'almost' do this with list(), but you can't use it in a function call. I only post it as you say 'more out of interest' :-

$_GET['unique'] = "blahblahblah=this_is_what_im_interested_in";

list(, $second) = explode('=', $_GET['unique']);

var_dump($second);

Output:-

string 'this_is_what_im_interested_in' (length=29)

You can see good examples of how flexible list() is in the first set of examples on the manual page.

I think it is worth pointing out that although your example will work:-

$common->resetPasswordReply(explode('=', $_GET['unique'])[1]);

it does kind of obfuscate your code and it is not obvious what you are passing into the function. Whereas something like the following is much more readable:-

list(, $replyText) = explode('=', $_GET['unique']);
$common->resetPasswordReply($replyText));

Think about coming back to your code in 6 months time and trying to debug it. Make it as self documenting as possible. Also, don't forget that, as you are taking user input here, it will need to be sanitised at some point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.