1.表单post过来的是字符串类型么?
2.如果是字符串类型,那么下面这段怎么解释,为何不经过类型转换就可以直接相加?Python可不是这样的哦<html>
<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
// create constant names
define ('TIREPRICE',100);
define ('OILPRICE',10);
define ('SPARKPRICE',4);
?>
<head>
<title>Bob's Auto Parts - Order Result</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
//echo '<p>Order processed.</p>';
echo '<p>Order processed at '.date('H:i , jS F Y').'</p>'; // overcome not integer number
if (is_string($tireqty) == True or is_string($oilqty) == True or is_string($sparkqty) == True)
{
echo "one of your input is type string.<br/>";
echo '<p>Your order is as follow: </p>';
echo $tireqty. ' tires. <br/>';
echo $oilqty. ' bottles of oil<br/>';
echo $sparkqty. ' spark plugs<br/>';
// calc total items ordered
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
if ($totalqty == 0)
{
echo 'You did not order anything on the previous page!<br/>';
}
else
{
echo "Items ordered: $totalqty <br/>";
// calc total amount
$toatlamount = 0;
$toatlamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;
echo 'Subtotal: $'.number_format($toatlamount,2).'<br/>';
// calc total tax
$taxrate = 0.10;
$totaltaxrate = 0;
$totaltaxrate = $toatlamount * 0.10;
echo 'Total tax: $'.number_format($totaltaxrate,2).'<br/>';
}
}
?>
</body>
</html>运行结果:Bob's Auto Parts
Order Results
Order processed at 17:33 , 15th April 2013one of your input is type string.Your order is as follow: 1 tires.
2 bottles of oil
3 spark plugs
Items ordered: 6
Subtotal: $132.00
Total tax: $13.20
PHPString
2.如果是字符串类型,那么下面这段怎么解释,为何不经过类型转换就可以直接相加?Python可不是这样的哦<html>
<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
// create constant names
define ('TIREPRICE',100);
define ('OILPRICE',10);
define ('SPARKPRICE',4);
?>
<head>
<title>Bob's Auto Parts - Order Result</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
//echo '<p>Order processed.</p>';
echo '<p>Order processed at '.date('H:i , jS F Y').'</p>'; // overcome not integer number
if (is_string($tireqty) == True or is_string($oilqty) == True or is_string($sparkqty) == True)
{
echo "one of your input is type string.<br/>";
echo '<p>Your order is as follow: </p>';
echo $tireqty. ' tires. <br/>';
echo $oilqty. ' bottles of oil<br/>';
echo $sparkqty. ' spark plugs<br/>';
// calc total items ordered
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
if ($totalqty == 0)
{
echo 'You did not order anything on the previous page!<br/>';
}
else
{
echo "Items ordered: $totalqty <br/>";
// calc total amount
$toatlamount = 0;
$toatlamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;
echo 'Subtotal: $'.number_format($toatlamount,2).'<br/>';
// calc total tax
$taxrate = 0.10;
$totaltaxrate = 0;
$totaltaxrate = $toatlamount * 0.10;
echo 'Total tax: $'.number_format($totaltaxrate,2).'<br/>';
}
}
?>
</body>
</html>运行结果:Bob's Auto Parts
Order Results
Order processed at 17:33 , 15th April 2013one of your input is type string.Your order is as follow: 1 tires.
2 bottles of oil
3 spark plugs
Items ordered: 6
Subtotal: $132.00
Total tax: $13.20
PHPString
但PHP的字符串连接符是 . 号,如果你用 '1'+'2'; 它当然明白你是想相加,而不是相连。因此结果自然是3,而不是'12'。甚至你用 '1'+'ssss' 也会得到数字1
2. 有很多语言有自动Cast的功能的。