Overcoming needle haystack confusion in PHP

。_饼干妹妹 提交于 2019-12-21 07:25:28

问题


What is the most practical way of overcoming needle haystack confusion in PHP?

Here $needle is the first argument

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Here $needle is the second argument

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

回答1:


it might make sense if you think of this as pre-fix representation of an in-fix operations.

is "bat" in array ("cat", "rat", "bat", "fat")
is $needle in_array $haystack
in_array($needle, $haystack)


does "supercalifragistic" string contain string "percal"
does $haystack strstr $needle
strstr($haystack, $needle)



回答2:


I made a cheat sheet for all php commands containing needle haystack parameters.

String functions are haystack/needle:

          strpos $haystack, $needle
         stripos $haystack, $needle
          strstr $haystack, $needle
          strchr $haystack, $needle
         stristr $haystack, $needle
         strrchr $haystack, $needle
        strripos $haystack, $needle
         strrpos $haystack, $needle
    substr_count $haystack, $needle

strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
stripos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
(same as strstr) strchr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
stristr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
strrchr ( string $haystack , mixed $needle ) : string
strripos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
strrpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) : int

Array functions are needle/haystack (except array_filter):

    array_search $needle, $haystack
        in_array $needle, $haystack

in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool
array_search ( mixed $needle  , array $haystack  [, bool $strict  ] )

array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array




回答3:


I don't think that's important. It's the same with the Linux bash. For example tar uses arguments archive files but ln uses arguments target linkname. But my example isn't a programming language so here is another explanation: What was the first in the world counting or numbering? For what you need it's always 2 arguments. The same is true for my example with tar and ln.



来源:https://stackoverflow.com/questions/11260589/overcoming-needle-haystack-confusion-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!