Adding the username into a URL to redirect to a specific page

I recommend switching to a code editor with PHP syntax highlighting, such as PHPStorm/Sublime/Atom/etc, which would reveal the problem on this line of code, as shown in the highlighting on your question once the code block is formatted:

$url="https://myurl.com/user/‘.$username;

The closing quote, is actually an apostrophe not ". As a result the closing quote is actually just before template_redirect