How to add line breaks in Telegram bot messages using PHP

I’m trying to create a new line in messages sent through my Telegram bot but it’s not working properly. When I use \n or \r\n in my text, instead of getting a line break, I see an underscore character _ in the actual message.

I need to send a message that has multiple lines but can’t figure out how to format it correctly. Here’s what I’m trying to do:

$message = 'Welcome to our channel! We hope you enjoy the content.';
$message .= "\n";
$message .= 'New videos are posted every evening around 7 PM.';

The message should display on separate lines but it shows with underscores instead of proper line breaks. What’s the correct way to format line breaks in Telegram bot messages sent via PHP?

Had the same issue when I started with Telegram bots. The underscore replacement happens because of markdown parsing conflicts. Don’t concatenate strings with line breaks - build your message like this instead:

$message = “Welcome to our channel! We hope you enjoy the content.\nNew videos are posted every evening around 7 PM.”;

Don’t enable markdown parsing unless you need it. With sendMessage, skip the parse_mode parameter or set it to an empty string. Check for special characters in your message too - they often cause weird character replacements.

try usin double quotes for your message. also, check if your sending method has parse_mode set to ‘HTML’ or ‘MarkdownV2’ - that should help with the format issues.

The underscore issue happens when Telegram’s markdown parsing conflicts with your formatting. I’ve hit this exact problem before - use ‘%0A’ instead of ‘\n’ for line breaks. It’s way more reliable. You can also try actual line breaks in your PHP string with heredoc syntax or just hit enter inside double quotes. Another fix is setting ‘parse_mode’ to null or empty string in your sendMessage parameters. This stops Telegram from converting your line breaks into weird characters. Don’t forget to use ‘urlencode()’ if you’re sending via GET request to the Telegram API.