Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show a php variable inside html tags?

I’m using phpmailer to send otp via email, in the Body part of the email, I’m trying to show a php variable $otp inside $otp tag but it just Shows $otp instead of the value

//value of $otp
$ot=rand(100000,999999);
$otp=strval($ot);

of the variable. I tried with the below code.

    $mail->Body    = 
    '<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;">$otp</h2>
    ';

but this only returns $otp in mail and not it’s value

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you whant to display a php variable in your string you need to close the string with ' and then use a . after the dot you can write php.

In your case you use the single quote so it will look like 'string' . $variable. 'string';

$mail->Body = '<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;">'
    . $otp . '</h2>';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading