php 在php中添加带数字的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11026796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:35:58  来源:igfitidea点击:

Adding string with number in php

php

提问by rynhe

$a = "3dollars";
$b = 20;
echo $a += $b;
print($a += $b);

Result:

结果:

23
43

I have a question from this calculation.$a is a string and $b is number.I am adding both and print using echo its print 23 and print using print return 43.How is it

我有一个来自这个计算的问题。$a 是一个字符串,$b 是数字。我正在添加两者并使用 echo 打印其打印 23 并使用打印返回 43 打印。它是怎么回事

回答by xbonez

It casts '3dollars' as a number, getting $a = 3.

它将 '3dollars' 作为一个数字,得到$a = 3.

When you echo, you add 20, to $a, so it prints 23and $a = 23.

当您 echo 时,您将 20, 添加到$a,因此它会打印23$a = 23

Then, when you print, you again add 20, so now $a = 43.

然后,当您打印时,您再次添加 20,所以现在$a = 43.

回答by fyquah95

The right way to add (which is technically concatenating) strings is

添加(从技术上讲是连接)字符串的正确方法是

$a = 7;
$b = "3 dollars";
print ($a . $b);  // 73 dollars

The +operator in php automatically converts string into numbers, which explains why your code carried out arimethic instead of concatenation

+php 中的操作符会自动将字符串转换为数字,这就解释了为什么你的代码进行了算术而不是串联

回答by JC editx

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

PHP 会根据变量的值自动将数据类型与变量相关联。由于严格意义上没有设置数据类型,因此您可以执行将字符串添加到整数之类的操作而不会导致错误。

In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.

在 PHP 7 中,添加了类型声明。这为我们提供了在声明函数时指定预期数据类型的选项,并且通过添加严格声明,如果数据类型不匹配,它将抛出“致命错误”。

To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file. Then it will show fatal error and if you didn't declare this strict then it convert string into integer.

要指定严格,我们需要设置 declare(strict_types=1);。这必须在 PHP 文件的第一行。然后它会显示致命错误,如果你没有声明这个严格,那么它将字符串转换为整数。

回答by ExploreTech

Since You have created a variable for the two, it stores the result of each, so when you added $a to 20 it will echo 23 which stores in the system, them when you print $a which is now 23 in addition to $b which is 20. You will get 43.

由于您为这两个变量创建了一个变量,它存储了每个变量的结果,因此当您将 $a 添加到 20 时,它将回显存储在系统中的 23,当您打印 $a 时,除了 $b 现在是 23这是 20。你会得到 43。

回答by arun singh

PHP treats '3dollars' as a integer 3 because string starting with integer and participating in arithmetic operation, so

PHP将'3dollars'视为整数3,因为字符串以整数开头并参与算术运算,所以

    $a = "3dollars";
    $b = 20;
    echo $a += $b;
    it echo 23;   //$a=$a+$b;

    now $a = 23 + 20;
    print($a += $b);    //$a=$a+$b;
    it print 43;