假数组切片运算符:使其更短
是否有一些创新的方法可以使“打印”更短而不会造成太多混乱?而你最喜欢哪一个“印花”呢?
define('_','_');
function _j($a, $b) {
return $a._.$b;
}
// Output 0_0
print (0)._.(0);
print _j(0,0);
更新
我想要做的是将 Python/Ruby 中的切片语法转换为 PHP 例如。
a[1:3]
a[1,3]
a[1..3]
要将其放入 PHP,您需要像这样引用 $a["1:3"]
($a 是一个具有 ArrayAccess 接口的类),所以我在想是否还有其他方法,$a[(0)._.(0)]
这太长了。
Is there some innovative way to make the "print" shorter without too much confusion? And which of the "print" do you like most?
define('_','_');
function _j($a, $b) {
return $a._.$b;
}
// Output 0_0
print (0)._.(0);
print _j(0,0);
Update
What I want to do is to have slice syntax that are in Python/Ruby into PHP
eg.
a[1:3]
a[1,3]
a[1..3]
to make it into PHP you need to quote like this $a["1:3"]
($a is a class with ArrayAccess interface) so I was thinking if there is some otherways, $a[(0)._.(0)]
This is too long.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您打算消除混淆,那么您确实不应该编写这样的代码,因为这离混淆还差一步。
If you intend to remove confusion, you really shouldn't be writing such code because it's a step short of obfuscation.
你想让我做什么?连接字符串?使用
implode
:不短,但绝对不那么混乱,更具可读性,并且最好地传达 意图
既然问题有足够的信息,就可以编辑
:您有一个实现 ArrayAccess 接口的类。
为什么不使用十进制数来实现切片运算符?
然后您应该能够将数字转换为字符串并按句点分割。您还可以使用
floor
来获取这两个部分。然后委托给 array_slice:但请注意,浮点精度可能存在问题。
使用引号有什么问题吗?多出两个角色也不算太糟糕。
what do you want to do? concatenate strings? use
implode
:not shorter, but definitely less confusing, more readable and it best conveys the intention
edit now that the question has enough information:
you have a class which implements the ArrayAccess interface.
why not use decimal numbers to achieve your slicing operator?
you should then be able to convert the number to a string and split on the period. you could also use
floor
to get both parts. then delegate toarray_slice
:be aware though, there might be problems with floating point precision.
what's wrong with using quotes though? two extra characters is not too bad.
由于它是 PHP,因此您可以通过使用 echo 而不是 print 来使其稍微短一点。
Since it's PHP, you could make it a tiny bit shorter by using echo instead of print.
直接的方法效果很好。
比较
我不完全确定你的目标是什么。
The straight forward approach works well.
Compare
I'm not entirely sure what your goal is here.
这是最短且不混乱的方法:p
This is the shortest and non confusing way :p