Home  >  Article  >  Backend Development  >  有哪些 不错的PHP代码样例可以参考?

有哪些 不错的PHP代码样例可以参考?

WBOY
WBOYOriginal
2016-06-17 08:32:03992browse

WEB程序员,PHP一定是必备的,即使你没用它开发过大型软件项目,也一定多少了解它的语法。


尽管PHP经常被人诟病,被人贬低,被人当玩笑开,事实证明,PHP是全世界网站开发中使用率最高的编程语言。PHP最大的缺点是太简单,语法不严谨,框架体系很弱,但这也是它最大的优点,一个有点编程背景的普通人,只需要学习PHP半天时间,就可以上手开始开发web应用了。


网上有人总结几种编程语言的特点,我觉得也挺有道理的:

PHP 就是: Quick and Dirty
Java 就是: Beauty and Slowly
Ruby 就是: Quick andBeauty
Python 就是:Quick and Simple

在PHP的流行普及中,网上总结出了很多实用的PHP代码片段,这些代码片段在当你遇到类似的问题时,粘贴过去就可以使用,非常的高效,非常的省时省力。将这些程序员前辈总结出的优秀代码放到自己的知识库中,是一个善于学习的程序员的好习惯。


有哪些好的PHP代码样例值得收藏起来呢?

回复内容:

Zend Framework, 不二之选。 可以研读类似yii2、laravel框架的源码,如果读通一个,个人认为会有质的变化。 第一次尝试知乎的自问自答。希望可以抛砖引玉~

一、黑名单过滤

<code class="language-text">function is_spam($text, $file, $split = ':', $regex = false){ 
    $handle = fopen($file, 'rb'); 
    $contents = fread($handle, filesize($file)); 
    fclose($handle); 
    $lines = explode("n", $contents); 
    $arr = array(); 
    foreach($lines as $line){ 
        list($word, $count) = explode($split, $line); 
        if($regex) 
            $arr[$word] = $count; 
        else 
            $arr[preg_quote($word)] = $count; 
    } 
    preg_match_all("~".implode('|', array_keys($arr))."~", $text, $matches); 
    $temp = array(); 
    foreach($matches[0] as $match){ 
        if(!in_array($match, $temp)){ 
            $temp[$match] = $temp[$match] + 1; 
            if($temp[$match] >= $arr[$word]) 
                return true; 
        } 
    } 
    return false; 
} 

$file = 'spam.txt'; 
$str = 'This string has cat, dog word'; 
if(is_spam($str, $file)) 
    echo 'this is spam'; 
else 
    echo 'this is not spam';
</code>
优秀代码话,推荐你去看看别人写的一些框架,因为接触,会使用的框架不多,但是现在特别喜欢一个款国人开发的thinkphp框架,非常不错,推荐你去看看
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn