2

I need to join/concatenate strings from a return of a function call, and from a variable without using the traditional concatenation ..

In this scenario, it should be displaying a url string.

Below is my actual code.

CODE:

$test = "{ config('app.url') }/{ $username }";
die(print_r($test));

Current Result:

{ config('app.url') }/testuser

Expected Result:

http://localhost:8000/testuser
1
  • Why are you refusing concatenation? Commented Jan 16, 2019 at 5:56

3 Answers 3

2

You may read more about complex (curly) syntax in a quoted string, however you may achieve what you want with that code:

$test = "{${config('app.url')}}/{$username}";
die(print_r($test));

But I personally prefer:

$appUrl = config('app.url');
$test = "{$appUrl}/{$username}";
die(print_r($test));

Does that work for you?

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

2 Comments

@mickmackusa, good to know that, but I was the first one to answer.
Thank you so much for the help. But the first snippet of code in your comment is not working too. I used the one you prefer.
1

It's not possible. You can only parse variables inside string. The only way is assigning function result to variable:

$url = config('app.url');
$test = "{$url}/{$username}";

You can read more about Variable parsing in strings

Comments

1

You can try the following way

<?php

$_ = function ( $v ) { return $v; };

function config($url)
{
    return $url;
}

$username = 'u_name';
echo "{$_( config('app.url') )}/{$username}";

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.