PHP字符串与变量的串联 [英] PHP String Concatenation With Variables

查看:68
本文介绍了PHP字符串与变量的串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次这样做,但是我确实必须这样做,所以我一直在使用php并拥有此脚本,该脚本在发送邮件时给我带来麻烦.它只是一个ajax POST请求,向php脚本发送代码和电子邮件.我面临的问题是获取$ message字符串以将第一封电子邮件与变量连接在一起.如果我只是将$ message设置为字符串文字并发送电子邮件,则一切正常.但是,如此处所示,当尝试将一些变量连接到其中时,不会发送电子邮件.另外,将新的电子邮件和代码写入Winners.txt文件时,所有功能均正常运行.我将字符串串联错了吗?我尝试了几种不同的方法.谢谢!

This is my first time doing this but I really had to, so I've been working with php and have this script that is giving me trouble when it goes to mail. All it is is an ajax POST request sending a code and email to the php script. The problem I am facing is getting the $message string for the first email to be concatenated with the variables. If I just set $message to a string literal and send the email, everything works. But when trying to concatenate some variables into it as shown here no email is sent. Also, when writing the new email and code to the winners.txt file, everything functions properly. Am I concatenating the string wrong? I have tried a few different ways. Thanks!

<?php
$myFile = "winners.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$email = $_POST['email'];
$code = $_POST['code'];
$code = (string)$code . '';
$message = "Winner information follows:\r\nEmail: ";
$message .= strval($email);
$message .= "\r\nConfirmation Code: ";
$message .= strval($code);
fwrite($fh, $email);
fwrite($fh, $code);
mail("loganhsnow@gmail.com", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", ', Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: ');
fclose($fh);
?>

推荐答案

首先不需要进行任何转换.因此,您可以摆脱 $ code =(string)$ code.''; 和所有strval()调用.您可以这样设置$ message:

Dont need to do any conversion first of all. So you can get rid of $code = (string)$code . ''; and all the strval() calls. You $message can be set as thus:

$ message =获胜者信息如下:\ r \ n电子邮件:".$ email."\ r \ n确认代码:".$ code;

此外,您似乎还没有在第二封电子邮件中包含该代码.所以整个事情看起来像这样:

Also, it doesnt look like you are including the code in your second email. So the whole thing changed would look like this:

<?php
$myFile = "winners.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$email = $_POST['email'];
$code = $_POST['code'];
$message = "Winner information follows:\r\nEmail: ".$email."\r\nConfirmation Code: ".$code;
fwrite($fh, $email);
fwrite($fh, $code);
mail("loganhsnow@gmail.com", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", 'Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: '.$code);
fclose($fh);
?>

要注意的另一件事是,您可能希望通过一些卫生检查来放入$ email,以确认它是有效的电子邮件地址.

One other thing to note is that you might want to put $email through some sanitizaiton checks to verify it is a valid email address.

这篇关于PHP字符串与变量的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆