我需要帮助找出正确的PHP编码的isset函数。
我在正确理解isset()函数需要做什么时遇到了问题。下面是赋值方向,我已经完成了b部分的末尾,使用isset()函数检查查询字符串中是否存在参数。当我构建表单时,表单操作是"GET",因为它需要在查询字符串中,这也是正确的吗?
我仅有的两个变量是Value1和Value2,它们是用户输入的。
SimpleCalculator.php -编写一个脚本,从查询字符串中检索两个值,将它们相加并显示结果。
步骤:
value1和value2的变量。$value1和$value2的局部变量。您需要使用isset()函数来检查这两个参数是否在查询字符串中。到目前为止,这就是我所拥有的:
<html>
<head>
<title>Sample web form</title>
<link href="/sandvig/mis314/assignments/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="pageContainer centerText">
<h2>Simple Calculations </h2>
<hr />
<form method="get" >
Please enter two numbers:<br><br>
Value 1: <input type="text" name="Value1" autofocus> <br><br>
Value 2: <input type="text" name="Value2" autofocus> <br><br>
<input type="submit" value="Add">
</form>
<?php
$value1 = $_GET ['Value1'];
$value2 = $_GET ['Value2'];
//Retrieve name from querystring. Check that parameter
//is in querystring or may get "Undefined index" error
echo 'Value 1 was:' . ($value1);
echo 'Value 2 was:' . ($value2);
echo 'Sum is: ' . ($value1 + $Value2);
?>
</div>
</body>
</html>发布于 2014-04-27 21:42:31
首先,检查本手册关于isset()函数。
它是如何工作的?
$value1 = isset($_GET ['Value1']) ? $_GET ['Value1'] : null;
$value2 = isset($_GET ['Value2']) ? $_GET ['Value2'] : null;现在,如果用户在查询字符串中发送作为Value1参数的内容,那么$value1变量将与该数据一起分配;如果没有分配null。value2也一样。
简单:)
发布于 2014-04-27 21:41:14
如果没有设置值,它不会说明该做什么,但是这是获取值的最佳方法。如果没有设置$value1或$value2,则可以显示错误。在下面的示例中,它们将为null。
$value1 = isset($_GET['Value1']) ? $_GET['Value1'] : null;
$value2 = isset($_GET['Value2']) ? $_GET['Value2'] : null;发布于 2014-04-27 21:41:24
<?php
if(isset($_GET))
{
$value1 = isset($_GET ['Value1']) ? $_GET ['Value1'] : null;
$value2 = isset($_GET ['Value2']) ? $_GET ['Value2'] : null;
//Retrieve name from querystring. Check that parameter
//is in querystring or may get "Undefined index" error
echo 'Value 1 was:' . ($value1);
echo 'Value 2 was:' . ($value2);
echo 'Sum is: ' . ($value1 + $Value2);
}
?>https://stackoverflow.com/questions/23329330
复制相似问题