Home  >  Article  >  Backend Development  >  PHP entry variable numbers_PHP tutorial

PHP entry variable numbers_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:34860browse

When introducing variables, I made it clear that PHP has integer and floating point (decimal) numeric types. However, in my experience, both types can be classified under general numbers (for the most part).

The following is a list of valid numeric type variables in PHP:

8 
3.14 
10980843985 
-4.2398508 
4.4e2 

Note: These values ​​are never enclosed in quotes (if they were, they would be strings containing numeric values, as shown in the concatenating string example), nor could they be separated by thousands using commas character (example thousands separator: 20,943).

Two functions commonly used to process numbers:
round() is used to round decimals to the nearest integer; or to round decimals to a specified number of digits.
Number_format() is used to convert a number into a more common representation, using commas as thousands separators; it can also set the specified number of decimal places.

<?php
	$n = 3.14; 
	$n = round ($n); //把小数四舍五入结果为:3 

	$n = 3.142857; 
	$n = round ($n, 3); //把小数四舍五入到指定的位数:3.143 
	
	$n = 20943; 
	$n = number_format ($n); //用逗号作为千位分隔符结果为:20,943
	
	$n = 20943; 
	$n = number_format ($n, 2); //用逗号作为千位分隔符同时设置小数点的指定位数:20,943.00 
?>

Those who are interested can print the results themselves, but one thing to note is that each result must be separated, otherwise the various printed results will be connected together. Is this a little test? ? ?

================================================== =================================

Let’s do a practical example below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>单引号</title>
</head>
<body> 
<?php 
  
// 设置必要要的变量: 
$quantity = 30; // 出售30件产品. 
$price = 119.95; // 单价. 
$taxrate = .05; // 5%的发票税. 
  
// 计算总额: 
$total = $quantity * $price; 
$total = $total + ($total * $taxrate); 
  
// 格式化总额: 
$total = number_format ($total, 2); 
  
// 打印结果: 
echo '<p>你所出售的 <b>' . $quantity . '</b> 件产品,成本单价为 <b>$' . $price . '</b> . 加上发票税,总额为 <b>$' . $total . '</b>.</p>'; 
  
?> 
</body> 
</html>

To print out a combination of HTML, dollar signs, and variables, . At the same time, there is another printing method (double quotes), which is about the difference between single quotes and double quotes for getting started with PHP.

Many mathematical operators also have corresponding assignment operators, allowing for shorthand assignment statements. The following line of statement:

$total = $total + ($total * $taxrate); 
可以重写为: 
$total += ($total * $taxrate);

================================================== ==============================

Let’s take a look at the operator knowledge points related to numbers (just take a look at it yourself and understand it. In fact, it is the same as when we studied mathematics in school and understand the algorithm. For interested students, you can Baidu Google Specific usage)

Operator: Operators are symbols used to perform certain operations on arrays and variables.

 

 

    

Note: Identity means that true will be returned only when the operands on both sides are equal and the data types are also the same;

For example: 0==0" This returns true because the operands are equal;

0==="0" This returns false because the data types are different.

Operator

5. Ternary operator

Condition ? value if true : value if false

Example:

6,

$a=@(57/0);

The divisor cannot be

span>

Operator precedence and associativity:

Generally speaking, operators have a set of priorities, which is the order in which they are executed.

The

operator is also associative, that is, the execution order of operators with the same priority. This order is usually left to right, right to left or irrelevant.

The table of operator precedence is given below. The top operator has the lowest priority, and the priority increases from top to bottom in the table.

To avoid priority confusion, you can use parentheses to avoid priorities.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/743386.htmlTechArticleWhen introducing variables, I clearly stated that PHP has integer and floating point (decimal) numeric types. However, based on my experience, both types can be classified under general numbers (in extreme...
Statement:
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