0

I've this config.php file that contains this example array:

return [
  myarray => [
    'foo' => 'bar_'.str_random(5).'.log'
  ]
]

I'd like to assign the str_random(5) into a $var variable and use it all in one step; for example, something like (this example doesn't work):

return [
  myarray = [
    'foo' => 'bar_'.(($var=str_random(5)) && $var).'.log'
  ]
]

The output should be:

echo $var;
uoEUu

print_r($myarray);
[
  'foo' => 'bar_uoEUu.log'
]

Thank you


EDIT

Ok, I found this solution, but It doesn't like me:

return [
  myarray = [
    'foo' => 'bar_'.$var=str_random(5).''.$var.'.log'
  ]
]

any cleaner suggestions?

3
  • 1
    Why? You could set $var before creating the array and just use it in the array. Commented Jul 17, 2019 at 12:16
  • This content is in a config file; and this array is returned: return [ myarray = [ 'foo' => 'bar_'.(($var=str_random(5)) && $var).'.log' ] ] Commented Jul 17, 2019 at 12:18
  • There is no reason I can see why you'd want to do this all in one step, and depending on what str_random is / does, the && $var may be unneeded, and is the bit that means that $var is a boolean, so you're concat-ing a bool to a string which appears to not be what you want. Commented Jul 17, 2019 at 12:21

3 Answers 3

1

The solution is actually much simpler than you think:

$myarray = [
  'foo' => 'bar_'.($var=str_random(5)).'.log'
];

The reason this works is that any assignment can be used as an expression whose value is the value assigned. This isn't often necessary, but has a few uses, like setting multiple variables to the same value:

$foo = $bar = 42;

In this case, you're concatenating a fixed string, an expression (which happens to be an assignment), and another fixed string; the extra parentheses just make sure that the assignment expression is evaluated before the concatenation.

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

Comments

1

IMO, you are overthinking the problem. If you need to use $var after assigning it to foo you can just do the following:

$var = str_random(5);
$myarray = [
  'foo' => "bar_$var.log"
];

1 Comment

As clarified in comments, this is a simplified example, which comes in the middle of a larger block of code in a config file. While unusual, I can imagine scenarios where this is useful.
1

Just wrap it in brackets:

$myarray = [
  'foo' => 'bar_'.($var=uniqid()).'.log'
]

var_dump($var, $myarray);

Which gives you :

string(13) "5d2f1f1e01338" 
array(1) { ["foo"]=> string(34) "bar_5d2f1f1e01338.log" }

Check it out here: https://3v4l.org/JfZf6

3 Comments

This doesn't quite match the desired output, as the random string gets inserted twice.
His code wanted 2? (based on his updated question) 'bar_'.($var=uniqid()).''.$var.'.log' the only bit i changed was uniqid(), as the function he uses is a custom one
The desired output shows just one instance of the random string: bar_uoEUu.log. The duplication seems to just be a mistake in trying to get it working.

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.