Php isset和$\你会感到困惑
我已经创建了一个私人消息系统,但我有一个恼人的问题$\u GET 首先,这里是用于发送邮件的表单的“sendmail.php”页面的完整代码Php isset和$\你会感到困惑,php,Php,我已经创建了一个私人消息系统,但我有一个恼人的问题$\u GET 首先,这里是用于发送邮件的表单的“sendmail.php”页面的完整代码 <?php include("header.php"); include("connect.php"); $mailid = $_GET['username']; $replyname = $_GET['user']; ?> <form action="send.php" method="post" name="sendpm">
<?php
include("header.php");
include("connect.php");
$mailid = $_GET['username'];
$replyname = $_GET['user'];
?>
<form action="send.php" method="post" name="sendpm">
<table>
<tr>
<td>
<font color='orange'>To</font>
</td>
<?php IF ($mailid != ''){ ?>
<td>
<input type="text" name="touser" id="touser" value="<?php echo $mailid;?>"/>
</td>
<?php }elseif($replyname != ''){ ?>
<td>
<input type="text" name="touser" id="touser" value="<?php echo $replyname;?>"/>
</td>
<?php }else{ ?>
<td>
<input type="text" name="touser" id="touser" value="<?php echo $replyname;?>"/>
</td>
<?php } ?>
</tr>
<tr>
<td>
<font color='orange'>Subject</font>
</td>
<td>
<input type="text" name="subject" id="subject" />
</td>
</tr>
<tr>
<td>
<font color='orange'>Message</font>
</td>
<td>
<textarea name="message" cols="60" rows="10" id="message"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Send PM" />
</td>
</tr>
</table>
</form>
<?php
include("footer.php");
?>
根据它的来源,我可以得到两种不同的结果。“mailid”是指您是否从个人资料中获得指导。“回复名称”是指您对收到的邮件单击“回复”
现在,如果我尝试从头开始发送邮件,我会被告知“用户名”和“用户”未定义(因为我没有来自包含它们的链接)。如果我来自一个链接,那么另一个链接将是未定义的,反之亦然
我可以通过添加isset
来“解决”这个问题,但出于某种原因,它会一直显示一个“1”,而不是应该显示的用户名(如果我跟踪一个没有isset的链接,就会显示)
这是非常恼人的,我一直在试图解决它的年龄
我刚刚意识到这是真是假。所以“1”表示这是真的,所以我把它改为
IF(isset($_GET['username'])){
$mailid = $_GET['username'];
}
IF(isset($_GET['user'])){
$replyname = $_GET['user'];
}
我认为这很好,但仍然显示1?尝试-
$mailid = isset($_GET['username']) ? $_GET['username'] : false;
$replyname = isset($_GET['user']) ? $_GET['user'] : false;
isset
返回boolean
如果您还检查了!空($var)或尝试使用
我自己修好了(花了2个小时,没办法修好,发到这里,马上就可以看到了..哈哈)
这是因为isset只检查它是否正确
所以它基本上说“如果username为true,变量就变成username”我在代码中没有看到
isset
,所以不清楚如何使用它
通常您会这样做(我更喜欢空的
而不是设置的
):
$mailIdOrName=”“;
如果(!empty($\u GET['username'])){
$mailIdOrName=$\u GET['username'];
}elseif(!empty($\u GET['user'])){
$mailIdOrName=$\u GET['user'];
}
/*以后你将不再需要if elseif else*/
我想看看你是如何使用isset的。如果你正在尝试这样的事情
$mailid = isset($_GET['username']);
$replyname = isset($_GET['user']);
然后它将返回布尔值
试试这个
<?php
include("header.php");
include("connect.php");
?>
<form action="send.php" method="post" name="sendpm">
<table>
<tr>
<td>
<font color='orange'>To</font>
</td>
<td>
<input type="text" name="touser" id="touser" value="<?php echo isset($_GET['username'])?$_GET['username']:$_GET['user']; ?>"/>
</td>
</tr>
<tr>
<td>
<font color='orange'>Subject</font>
</td>
<td>
<input type="text" name="subject" id="subject" />
</td>
</tr>
<tr>
<td>
<font color='orange'>Message</font>
</td>
<td>
<textarea name="message" cols="60" rows="10" id="message"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Send PM" />
</td>
</tr>
</table>
</form>
到
你在哪里回音?仅仅因为你不知道如何使用函数并不意味着它不工作properly@LoganMurphy有必要吗?一个函数显然总是能正常工作的。这个问题表明php本身存在一个bug,而不是你的代码……或者至少它没有看到我的“答案”,我使用isset来查看它是否存在。如果你能帮上更多的忙,看看大家都忽略了这一点,哈哈,是的,你是对的。Isset返回布尔值,而不是值。只有一个建议使用三元运算符(?:),这比“if-else”快得多。此外,您还可以像我上面所做的那样用它来缩短代码。谢谢,我使用了这么多ifs和ELSE,这将有助于加载:)
IF(isset($_GET['username'])){
$mailid = $_GET['username'];
}
IF(isset($_GET['user'])){
$replyname = $_GET['user'];
}
$mailIdOrName = "";
if (!empty($_GET['username'])) {
$mailIdOrName = $_GET['username'];
} elseif (!empty($_GET['user'])) {
$mailIdOrName = $_GET['user'];
}
/* Later you will not need if-elseif-else */
<input type="text" name="touser" id="touser" value="<?php echo $mailIdOrName;?>"/>
$mailid = isset($_GET['username']);
$replyname = isset($_GET['user']);
<?php
include("header.php");
include("connect.php");
?>
<form action="send.php" method="post" name="sendpm">
<table>
<tr>
<td>
<font color='orange'>To</font>
</td>
<td>
<input type="text" name="touser" id="touser" value="<?php echo isset($_GET['username'])?$_GET['username']:$_GET['user']; ?>"/>
</td>
</tr>
<tr>
<td>
<font color='orange'>Subject</font>
</td>
<td>
<input type="text" name="subject" id="subject" />
</td>
</tr>
<tr>
<td>
<font color='orange'>Message</font>
</td>
<td>
<textarea name="message" cols="60" rows="10" id="message"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Send PM" />
</td>
</tr>
</table>
</form>