Home > Backend Development > PHP Tutorial > 表单 post过来的是字符串类型么?

表单 post过来的是字符串类型么?

WBOY
Release: 2016-06-23 14:05:48
Original
1295 people have browsed it

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>
Copy after login


运行结果:
Bob's Auto PartsOrder ResultsOrder processed at 17:33 , 15th April 2013one of your input is type string.Your order is as follow: 1 tires. 2 bottles of oil3 spark plugsItems ordered: 6 Subtotal: $132.00Total tax: $13.20
Copy after login


回复讨论(解决方案)

python的字符串连接符和算数相加都是+号,如果你 '1'+'2', python没法明白你的意思,到底相加还是相连?它可不能自作主张,只好严格按类型运算。
但PHP的字符串连接符是 . 号,如果你用 '1'+'2'; 它当然明白你是想相加,而不是相连。因此结果自然是3,而不是'12'。甚至你用 '1'+'ssss' 也会得到数字1

1. 是字符串类型。
2. 有很多语言有自动Cast的功能的。

呵呵呵 刚开始接触弱类型语言的时候都有这类想发,习惯就好了...

python是弱类型,是不需要声明字段属性的,python虽然是弱类型,但是不会将字段类型默认识别。

传递是什么类型就还是什么类型

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template