intval和floatval之间的区别是什么?

huangapple go评论59阅读模式
英文:

Whats the difference between intval and floatval?

问题

我测试这段代码时看不出任何差异

$stock_threshold = get_post_meta( $product->get_id(), '_out_of_stock_threshold', true );
$new_stock_quantity = $product->get_stock_quantity() - intval($stock_threshold);
var_dump( $new_stock_quantity );

更新: 我不需要小数位,只需要库存数量为整数。无论哪种方法,都没有错误,只是想知道它们之间的区别,但我认为我需要使用这个是因为数据库中存在负的库存水平,如-35,而数据库不接受减号。

我被迫使用intval()来避免出现数字错误:遇到非数字值。

$stock_threshold = 2
$new_stock_quantity = 8
英文:

I see no difference when testing this code

$stock_threshold = get_post_meta( $product->get_id(), '_out_of_stock_threshold', true );
$new_stock_quantity = $product->get_stock_quantity() - intval($stock_threshold);
var_dump( $new_stock_quantity );

Update : I don't need decimal places, only whole numbers for stock quantity. I have no errors using either, just wanted to know the difference but i assume the reason i need to use this is because of the negative stock levels in the database like - 35 where the DB doesn't like a minus sign.

I've been forced to use intval() to avoid numeric the error : non-numeric value encountered.

$stock_threshold = 2
$new_stock_quantity = 8

答案1

得分: 1

intval: intval() 函数返回一个变量的整数值。

参数值:

  • variable - 必需。指定要检查的变量。
  • base - 可选。指定用于转换的基数。仅当变量为字符串时才会生效。默认基数为10。

-> 成功时返回变量的整数值,失败时返回0。一个空数组将返回0,非空数组将返回1。它接受一个变量作为参数并返回其整数值。如果变量包含以数字值开头的字符串,它将返回该字符串的整数值。如果变量包含浮点数,它将返回该浮点数的整数部分。如果变量包含非数值值,它将返回0。

示例:

echo intval(35) . "<br>";     // 输出: 35

echo intval(3.5) . "<br>";    // 输出: 3

echo intval("35.2") . "<br>";  // 输出: 35

echo intval(array()) . "<br>";  // 输出: 0

echo intval(array("red", "green", "blue")) . "<br>";   // 输出: 1

floatval: floatval() 函数返回一个变量的浮点数值。

参数值:

  • variable - 必需。指定要检查的变量。必须是标量类型。

-> 成功时返回变量的浮点数值,失败时返回0。一个空数组将返回0,非空数组将返回1。它接受一个变量作为参数并返回其浮点数值。如果变量包含以数字值开头的字符串,它将返回该字符串的浮点数值。如果变量包含整数,它将将其转换为浮点数。如果变量包含非数值值,它将返回0。

示例:

echo floatval("235.567889") . "<br>";    // 输出: 235.567889

echo floatval("235.567889Example") . "<br>";   // 输出: 235.567889

echo floatval(35.56) . "<br>";  // 输出: 35.56

echo floatval("Example123") . "<br>";    // 输出: 0
英文:

intval: The intval() function returns the integer value of a variable.

Parameter Values:

variable - Required. Specifies the variable to check
base - Optional. Specifies the base to use for the conversion. Only has an effect if the variable is a string. The default base is 10

-> The integer value of the variable on success, 0 on failure. An empty array will return 0, and a non-empty array will return 1. It takes a variable as an argument and returns its integer value. If the variable contains a string that starts with a numeric value, it will return the integer value of that string. If the variable contains a float, it will return the integer portion of that float. If the variable contains a non-numeric value, it will return 0.

Examples:

echo intval(35) . &quot;&lt;br&gt;&quot;;     // output: 35

echo intval(3.5) . &quot;&lt;br&gt;&quot;;    // output: 3

echo intval(&quot;35.2&quot;) . &quot;&lt;br&gt;&quot;;  // output: 35

echo intval(array()) . &quot;&lt;br&gt;&quot;;  // output: 0

echo intval(array(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;)) . &quot;&lt;br&gt;&quot;;   // output: 1

floatval: The floatval() function returns the float value of a variable.

Parameter Values:

variable - Required. Specifies the variable to check. Must be a scalar type

-> The float value of the variable on success, 0 on failure. An empty array will return 0, and a non-empty array will return 1. It takes a variable as an argument and returns its floating-point value. If the variable contains a string that starts with a numeric value, it will return the floating-point value of that string. If the variable contains an integer, it will convert it to a float. If the variable contains a non-numeric value, it will return 0.

Examples:

echo floatval(&quot;235.567889&quot;) . &quot;&lt;br&gt;&quot;;    // Output: 235.567889

echo floatval(&quot;235.567889Example&quot;) . &quot;&lt;br&gt;&quot;;   // Output: 235.567889

echo floatval(35.56) . &quot;&lt;br&gt;&quot;;  // Output: 35.56

echo floatval(&quot;Example123&quot;) . &quot;&lt;br&gt;&quot;;    // Output: 0

huangapple
  • 本文由 发表于 2023年2月23日 23:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定