I noticed that PHP's internal functions never use strings for pre-defined or limited values, only constants.
我注意到PHP的内部函数从不使用字符串作为预定义值或有限值,只使用常量。
例如:
pad_type:
pad_type:
Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
可选参数pad_type可以是STR_PAD_RIGHT、STR_PAD_LEFT或STR_PAD_BOTH。如果没有指定pad_type,则假定为STR_PAD_RIGHT。
What's the reason for not using a string as parameter here?
这里不使用字符串作为参数的原因是什么?
str_pad($test, 10, 0, 'left')
seems a lot simpler than str_pad( $test, 10, 0, STR_PAD_LEFT)
str_pad($test, 10,0, 'left')似乎比str_pad简单得多($test, 10,0, STR_PAD_LEFT)
(This is more of a meta question. I hope it's OK to ask here.)
这更像是一个元问题。我希望可以在这里问一下。
3 个解决方案
#1
5
It is easier to make mistakes when typing a string. Using an undefined constant will throw a warning. It's not just a PHP thing. Regular API functions (i.e. of an OS) usually use numeric constants as well for parameters like this.
输入字符串时更容易出错。使用一个未定义的常量将抛出一个警告。它不仅仅是PHP的东西。常规的API函数(例如操作系统)通常也会使用数值常量作为参数。
#2
1
They use int
.. and it more efficient that way because of Case Sensitivity , Spelling Mistakes , Parsing strings , Better for IDE , Error etc.
他们使用int . .由于区分大小写、拼写错误、解析字符串、IDE更好、错误等原因,这种方法更有效。
If you don't like constant you can just use int value
如果你不喜欢常量,你可以用int值
str_pad($test, 10, 0, 0) == str_pad( $test, 10, 0, STR_PAD_LEFT)
#3
1
Notice that the type of $pad_type
in the parameters is actually int
. Passing ints is a lot faster than passing (then comparing) strings. So instead of passing a number into the function to tell it what to do, you pass the corresponding constant, making your code clearer. And if the PHP developers ever want to change it to pass strings instead of ints, they can do it without breaking any of your code, as long as you used the constants.
注意,参数中的$pad_type实际上是int类型,传递ints要比传递(然后比较)字符串快得多。因此,与其向函数中传递一个数字来告诉它该做什么,不如传递相应的常数,使代码更清晰。如果PHP开发人员想要将其更改为传递字符串而不是int,他们可以在不破坏任何代码的情况下这样做,只要使用常量。
#1
5
It is easier to make mistakes when typing a string. Using an undefined constant will throw a warning. It's not just a PHP thing. Regular API functions (i.e. of an OS) usually use numeric constants as well for parameters like this.
输入字符串时更容易出错。使用一个未定义的常量将抛出一个警告。它不仅仅是PHP的东西。常规的API函数(例如操作系统)通常也会使用数值常量作为参数。
#2
1
They use int
.. and it more efficient that way because of Case Sensitivity , Spelling Mistakes , Parsing strings , Better for IDE , Error etc.
他们使用int . .由于区分大小写、拼写错误、解析字符串、IDE更好、错误等原因,这种方法更有效。
If you don't like constant you can just use int value
如果你不喜欢常量,你可以用int值
str_pad($test, 10, 0, 0) == str_pad( $test, 10, 0, STR_PAD_LEFT)
#3
1
Notice that the type of $pad_type
in the parameters is actually int
. Passing ints is a lot faster than passing (then comparing) strings. So instead of passing a number into the function to tell it what to do, you pass the corresponding constant, making your code clearer. And if the PHP developers ever want to change it to pass strings instead of ints, they can do it without breaking any of your code, as long as you used the constants.
注意,参数中的$pad_type实际上是int类型,传递ints要比传递(然后比较)字符串快得多。因此,与其向函数中传递一个数字来告诉它该做什么,不如传递相应的常数,使代码更清晰。如果PHP开发人员想要将其更改为传递字符串而不是int,他们可以在不破坏任何代码的情况下这样做,只要使用常量。