<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP | 酷 壳 - CoolShell</title>
	<atom:link href="https://coolshell.cn/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>https://coolshell.cn</link>
	<description>享受编程和技术所带来的快乐 - Coding Your Ambition</description>
	<lastBuildDate>Mon, 28 Dec 2020 08:22:50 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>
	<item>
		<title>代码执行的效率</title>
		<link>https://coolshell.cn/articles/7886.html</link>
					<comments>https://coolshell.cn/articles/7886.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Fri, 13 Jul 2012 00:18:32 +0000</pubDate>
				<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[编程语言]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=7886</guid>

					<description><![CDATA[<p>在《性能调优攻略》里，我说过，要调优性需要找到程序中的Hotspot，也就是被调用最多的地方，这种地方，只要你能优化一点点，你的性能就会有质的提高。在这里我给大...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/7886.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/7886.html">代码执行的效率</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>在《<a title="性能调优攻略" href="https://coolshell.cn/articles/7490.html" target="_blank">性能调优攻略</a>》里，我说过，要调优性需要找到程序中的Hotspot，也就是被调用最多的地方，这种地方，只要你能优化一点点，你的性能就会有质的提高。在这里我给大家举三个关于代码执行效率的例子（它们都来自于网上）</p>
<h4><strong>第一个例子</strong></h4>
<p><strong> PHP中Getter和Setter的效率</strong>（<a href="http://www.reddit.com/r/programming/comments/wdsgn/today_i_learned_that_creating_getters_setters_in/" target="_blank">来源reddit</a>）</p>
<p>这个例子比较简单，你可以跳过。</p>
<p>考虑下面的PHP代码：我们可看到，使用Getter/Setter的方式，性能要比直接读写成员变量要差一倍以上。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">&lt;?php
	//dog_naive.php

	class dog {
		public $name = &quot;&quot;;
		public function setName($name) {
			$this-&amp;gt;name = $name;
		}
		public function getName() {
			return $this-&amp;gt;name;
		}
	}

	$rover = new dog();
        //通过Getter/Setter方式
	for ($x=0; $x&lt;10; $x++) {
		$t = microtime(true);
		for ($i=0; $i&lt;1000000; $i++) {
			$rover-&gt;setName(&quot;rover&quot;);
			$n = $rover-&gt;getName();
		}
		echo microtime(true) - $t;
		echo &quot;\n&quot;;
	}
        //直接存取变量方式
        for ($x=0; $x&lt;10; $x++) {
		$t = microtime(true);
		for($i=0; $i&lt;1000000; $i++) {
			$rover-&gt;name = &quot;rover&quot;;
			$n = $rover-&gt;name;
		}
		echo microtime(true) - $t;
		echo &quot;\n&quot;;
	}
?&gt;</pre>
<p>这个并没有什么稀，因为有函数调用的开销，函数调用需要压栈出栈，需要传值，有时还要需要中断，要干的事太多了。所以，代码多了，效率自然就慢了。所有的语言都这个德行，这就是为什么C++要引入inline的原因。而且Java在打开优化的时候也可以优化之。但是对于动态语言来说，这个事就变得有点困难了。</p>
<p><span id="more-7886"></span></p>
<p>你可能会以为使用下面的代码（Magic Function）会好一些，但实际其性能更差。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">class dog {
	private $_name = &quot;&quot;;
	function __set($property,$value) {
		if($property == &#039;name&#039;) $this-&gt;_name = $value;
	}
	function __get($property) {
		if($property == &#039;name&#039;) return $this-&gt;_name;
	}
}</pre>
<p>动态语言的效率从来都是一个问题，如果你需要PHP有更好的性能，你可能需要使用<a href="https://github.com/facebook/hiphop-php" target="_blank">FaceBook的HipHop</a>来把PHP编译成C语言。</p>
<h4><strong>第二个例子</strong></h4>
<p><strong>为什么Python程序在函数内执行得更快？</strong>（<a href="http://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function" target="_blank">来源StackOverflow</a>）</p>
<p>考虑下面的代码，一个在函数体内，一个是全局的代码。</p>
<p>函数内的代码执行效率为 1.8s</p>
<pre data-enlighter-language="python" class="EnlighterJSRAW">def main():
    for i in xrange(10**8):
        pass
main()</pre>
<p>函数体外的代码执行效率为 4.5s</p>
<pre data-enlighter-language="python" class="EnlighterJSRAW">for i in xrange(10**8):
    pass</pre>
<p>不用太纠结时间，只是一个示例，我们可以看到效率查得很多。为什么会这样呢？我们使用 <a href="http://docs.python.org/library/dis.html" target="_blank"><code>dis</code> module</a> 反汇编函数体内的bytecode 代码，使用 <a href="http://docs.python.org/library/functions.html#compile" target="_blank"><code>compile</code> builtin</a> 反汇编全局bytecode，我们可以看到下面的反汇编（注意我高亮的地方）</p>
<pre data-enlighter-language="shell" class="EnlighterJSRAW" data-enlighter-highlight="2">
13 FOR_ITER                 6 (to 22)
16 STORE_FAST               1 (i)
19 JUMP_ABSOLUTE           13</pre>
<pre data-enlighter-language="shell" class="EnlighterJSRAW" data-enlighter-highlight="2">
13 FOR_ITER                 6 (to 22)
16 STORE_NAME               1 (i)
19 JUMP_ABSOLUTE           13</pre>
<p>我们可以看到，差别就是 <a href="http://docs.python.org/library/dis.html#opcode-STORE_FAST" target="_blank"><code>STORE_FAST</code></a> 和 <code><a href="http://docs.python.org/library/dis.html#opcode-STORE_NAME" target="_blank">STORE_NAME</a>，前者比后者快很多。所以，在全局代码中，变量i成了一个全局变量，而函数中的i是放在本地变量表中，所以在全局变量表中查找变量就慢很多。如果你在main函数中声明global i 那么效率也就下来了。</code>原因是，本地变量是存在一个数组中（直到），用一个整型常量去访问，而全局变量存在一个dictionary中，查询很慢。</p>
<p><code>（注：在</code>C/C++中，这个不是一个问题）</p>
<h4><strong>第三个例子</strong></h4>
<p><strong> 为什么排好序的数据在遍历时会更快？</strong>（<a href="http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array" target="_blank">来源StackOverflow</a>）</p>
<p>参看如下C/C++的代码：</p>
<pre data-enlighter-language="c" class="EnlighterJSRAW" data-enlighter-highlight="4"> for (unsigned i = 0; i &lt; 100000; ++i) {
   // primary loop
    for (unsigned j = 0; j &lt; arraySize; ++j) {
        if (data[j] &gt;= 128)
            sum += data[j];
    }
}</pre>
<p>如果你的data数组是排好序的，那么性能是1.93s，如果没有排序，性能为11.54秒。差5倍多。无论是C/C++/Java，或是别的什么语言都基本上一样。</p>
<p>这个问题的原因是——<strong> <a href="http://en.wikipedia.org/wiki/Branch_predictor">branch prediction</a> （分支预判）</strong>伟大的stackoverflow给了一个非常不错的解释。</p>
<p>考虑我们一个铁路分叉，当我们的列车来的时候， 扳道员知道分个分叉通往哪，但不知道这个列车要去哪儿，司机知道要去哪，但是不知道走哪条分叉。所以，我们需要让列车停下来，然后司机和扳道员沟通一下。这样的性能太差了。</p>
<p>所以，我们可以优化一下，那就是猜，我们至少有50%的概率猜对，如果猜对了，火车行驶性能巨高，猜错了，就得让火车退回来。如果我猜对的概率高，那么，我们的性能就会高，否则老是猜错了，性能就很差。</p>
<p><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-7893" title="muxnt" src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt.jpg" alt="" width="440" height="330" srcset="https://coolshell.cn/wp-content/uploads/2012/07/muxnt.jpg 440w, https://coolshell.cn/wp-content/uploads/2012/07/muxnt-300x225.jpg 300w, https://coolshell.cn/wp-content/uploads/2012/07/muxnt-360x270.jpg 360w" sizes="(max-width: 440px) 100vw, 440px" /></p>
<p style="text-align: center;">Image by Mecanismo, from Wikimedia Commons:<a href="http://commons.wikimedia.org/wiki/File:Entroncamento_do_Transpraia.JPG">http://commons.wikimedia.org/wiki/File:Entroncamento_do_Transpraia.JPG</a></p>
<p>我们的if-else 就像这个铁路分叉一样，下面红箭头所指的就是搬道器。</p>
<p><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-7892" title="pyfwC" src="https://coolshell.cn/wp-content/uploads/2012/07/pyfwC.png" alt="" width="567" height="91" srcset="https://coolshell.cn/wp-content/uploads/2012/07/pyfwC.png 567w, https://coolshell.cn/wp-content/uploads/2012/07/pyfwC-300x48.png 300w" sizes="(max-width: 567px) 100vw, 567px" /></p>
<p>那么，我们的搬道器是怎么预判的呢？就是使用过去的历史数据，如果历史数据有90%以上的走左边，那么就走左边。所以，我们排好序的数据就更容易猜得对。</p>
<pre data-enlighter-language="shell" class="EnlighterJSRAW">T = 走分支（条件表达式为true）
N = 不走分支(条件表达式为false)

data[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...
branch = N  N  N  N  N  ...   N    N    T    T    T  ...   T    T    T  ...

= NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT  (easy to predict)</pre>
<pre data-enlighter-language="shell" class="EnlighterJSRAW">data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118,  14, 150, 177, 182, 133, ...
branch =   T,   T,   N,   T,   T,   T,   T,  N,   T,   N,   N,   T,   T,   T,   N  ...

= TTNTTTTNTNNTTTN ...   (completely random - hard to predict)</pre>
<p>从上面我们可以看到，排好序的数据更容易预测分支。</p>
<p>对此，那我们怎么办？我们需要在这种循环中除去if-else语句。比如：</p>
<p>我们把条件语句：</p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">if (data[j] &gt;= 128)
sum += data[j];
</pre>
<p>变成：</p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">int t = (data[j] - 128) &gt;&gt; 31;
sum += ~t &amp; data[j];</pre>
<p>“没有分叉”的性能基本上和“排好序有分支”一个样，无论是C/C++，还是Java。</p>
<blockquote><p><strong>注：</strong>在GCC下，如果你使用 <code>-O3</code> or <code>-ftree-vectorize</code> 编译参数，GCC会帮你优化分叉语句为无分叉语句。VC++2010没有这个功能。</p></blockquote>
<p><strong>最后，推荐大家一个网站——<a href="https://developers.google.com/speed/" target="_blank">Google Speed</a>，网站上的有一些教程告诉你<a href="https://developers.google.com/speed/articles/" target="_blank">如何写出更快的Web程序</a>。</strong></p>
<p><strong></strong>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" id="wp_rp_first"><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/oscar-meyer-wienermobile-150x150.jpg" alt="编程语言汽车" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_title">编程语言汽车</a></li><li ><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="到处都是Unix的胎记" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_title">到处都是Unix的胎记</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/18024.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2017/07/api-design-300x278-2-150x150.jpg" alt="API设计原则 &#8211; Qt官网的设计实践总结" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18024.html" class="wp_rp_title">API设计原则 &#8211; Qt官网的设计实践总结</a></li><li ><a href="https://coolshell.cn/articles/10169.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/5.jpg" alt="类型的本质和函数式实现" width="150" height="150" /></a><a href="https://coolshell.cn/articles/10169.html" class="wp_rp_title">类型的本质和函数式实现</a></li><li ><a href="https://coolshell.cn/articles/8990.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2013/02/linus_pointer_to_pointer-150x150.jpg" alt="Linus：利用二级指针删除单向链表" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8990.html" class="wp_rp_title">Linus：利用二级指针删除单向链表</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/7886.html">代码执行的效率</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/7886.html/feed</wfw:commentRss>
			<slash:comments>70</slash:comments>
		
		
			</item>
		<item>
		<title>一些文章和各种资源</title>
		<link>https://coolshell.cn/articles/5224.html</link>
					<comments>https://coolshell.cn/articles/5224.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Tue, 20 Sep 2011 00:32:52 +0000</pubDate>
				<category><![CDATA[Web开发]]></category>
		<category><![CDATA[技术读物]]></category>
		<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[编程工具]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[Web]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=5224</guid>

					<description><![CDATA[<p>下面是近期收录的一些文章和资源，希望对你有用。 系统方面 印度的电子商务网站flipkart的性能扩展（PPT） http://www.slideshare.n...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/5224.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/5224.html">一些文章和各种资源</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>下面是近期收录的一些文章和资源，希望对你有用。</p>
<h4>系统方面</h4>
<ul>
<li><strong>印度的电子商务网站flipkart的性能扩展</strong>（PPT） <a href="http://www.slideshare.net/sids/how-flipkart-scales-php">http://www.slideshare.net/sids/how-flipkart-scales-php</a>，都是一些最基本的东西，对于初学者来说很不错。PPT做的也不错。</li>
</ul>
<ul>
<li><strong>Tagged.com的扩展之路</strong> &#8211; 1亿用户，1000台服务器，50亿的PV <a href="http://highscalability.com/blog/2011/8/8/tagged-architecture-scaling-to-100-million-users-1000-server.html">http://highscalability.com/blog/2011/8/8/tagged-architecture-scaling-to-100-million-users-1000-server.html</a> 还是PHP的WEB站点。另外，<a href="http://highscalability.com/" target="_blank">highscalability.com</a>这个网站上有很多和高性能有关的文章，很不错。比如最新的：<a href="http://highscalability.com/blog/2011/9/16/stuff-the-internet-says-on-scalability-for-september-16-2011.html">Stuff The Internet Says On Scalability For September 16, 2011</a></li>
</ul>
<p><a href="http://highscalability.com/" target="_blank"><img decoding="async" loading="lazy" id="banner" class="aligncenter" title="High Scalability" src="http://highscalability.com/storage/HSBannerTrebuchet.jpg" alt="High Scalability" width="577" height="84" /></a></p>
<ul>
<li><strong>浏览器是怎么工作的</strong>？ <a href="http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/" target="_blank">http://www.html5rocks.com/en/tutorials/internals/howbrowserswork</a>/ 相当不错的一个教程，告诉你浏览器里面是怎么搞的，很不错。如果图片看不到，可以<a href="http://taligarsiel.com/Projects/howbrowserswork1.htm" target="_blank">看这里</a>。如果你英文不是太好，你可以看看<a href="http://blog.csdn.net/zzzaquarius/article/details/6532299" target="_blank">中译版</a>，译得并不是太好。</li>
</ul>
<figure style="width: 624px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" title="Mozilla's Gecko rendering engine main flow" src="http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/image008.jpg" alt="Mozilla's Gecko rendering engine main flow" width="624" height="289" /><figcaption class="wp-caption-text">Mozilla&#39;s Gecko rendering engine main flow</figcaption></figure>
<ul>
<li><strong>怎么使用epoll的示例</strong> <a href="https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/">https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/</a></li>
</ul>
<ul>
<li><strong>Intel C/C++ 64位程序开发教程</strong> <a href="http://software.intel.com/en-us/articles/lessons-on-development-of-64-bit-cc-applications/" target="_blank">http://software.intel.com/en-us/articles/lessons-on-development-of-64-bit-cc-applications/</a> 本站以前也介绍过一个关于<a title="64位平台C/C++开发注意事项" href="https://coolshell.cn/articles/3512.html" target="_blank">64位C/C++的编程注意事项</a>。</li>
</ul>
<div><span id="more-5224"></span></div>
<h4><span class="Apple-style-span" style="font-weight: 800;">各种教程</span></h4>
<ul>
<ul>
<li><strong>Version Control by Example</strong>(电子书) <a href="http://www.ericsink.com/vcbe/">http://www.ericsink.com/vcbe/</a></li>
</ul>
</ul>
<p><img decoding="async" loading="lazy" class="aligncenter" src="http://www.ericsink.com/scm/1802_image001.jpg" alt="" width="240" height="315" /><strong><strong><br />
</strong></strong></p>
<ul>
<li><strong><strong>SQL注入口袋书</strong></strong>（<a href="https://docs.google.com/Doc?docid=0AZNlBave77hiZGNjanptbV84Z25yaHJmMjk&amp;pli=1#Allowed_Intermediary_Character_30801873723976314" target="_blank">Google Doc</a> 需翻墙）<strong>，</strong>涵盖MySQL, MSSQL和Oracle，我觉得可以用来做你的程序的安全测试。<strong><br />
</strong></li>
</ul>
<ul>
<li><strong>如何写Vim的插件</strong>（教程）<a href="http://stevelosh.com/blog/2011/09/writing-vim-plugins/" target="_blank">http://stevelosh.com/blog/2011/09/writing-vim-plugins/</a> 相信你已读过“<a title="给程序员的VIM速查卡" href="https://coolshell.cn/articles/5479.html" target="_blank">VIM简明攻略</a>” 并收藏了 “<a title="给程序员的VIM速查卡" href="https://coolshell.cn/articles/5479.html" target="_blank">vim的速查卡</a>”，随着你的vim的能力加强，是时候搞搞vim的插件了。</li>
</ul>
<ul>
<li><strong>一个超有意思的学习Javascript的在线课件了</strong>。下面的这个网页上有一个Web的命令行，你可以跟着他的提示去输入一些命令，并以此来学习Javascript，这个创意真是太好了，我觉得这应该推广到我们的学校中去，不是只听老师讲，还需要大家一起来动作。 <a href="http://www.codecademy.com/" target="_blank">http://www.codecademy.com/</a></li>
</ul>
<ul>
<li><strong>一些各种各样的教程</strong> <a href="http://www.dickbaldwin.com/toc.htm">http://www.dickbaldwin.com/toc.htm</a>  这些都是些入门的教程，仅当是练练英语了。</li>
<ul>
<li><a href="http://www.dickbaldwin.com/tocint.htm">Introductory Java Tutorial</a></li>
<li><a href="http://www.dickbaldwin.com/tocmed.htm">Intermediate Java Tutorial </a></li>
<li><a href="http://www.dickbaldwin.com/tocadv.htm">Advanced Java Tutorial</a></li>
<li><a href="http://www.dickbaldwin.com/tocknowledge.htm">Test Your Java Knowledge</a></li>
<li><a href="http://www.dickbaldwin.com/tocjscript1.htm">JavaScript Tutorial</a></li>
<li><a href="http://www.dickbaldwin.com/tocxml.htm">XML &#8212; eXtensible Markup Language</a></li>
<li><a href="http://www.dickbaldwin.com/tocpyth.htm">Python Programming Tutorial</a></li>
<li><a href="http://www.dickbaldwin.com/tocCsharp.htm">C# Programming Tutorial</a></li>
<li><a href="http://www.dickbaldwin.com/tocdsp.htm">Digital Signal Processing</a></li>
</ul>
</ul>
<ul>
<ul>
<li><a href="http://www.dickbaldwin.com/Cosc1315/Pf00100Index.htm">Object-Oriented Programming Fundamentals using C++</a></li>
<li><a href="http://www.dickbaldwin.com/Cosc1315/Pfsg00100StudyGuideIndex.htm">Object-Oriented Programming Fundamentals using C++ (Practice Tests)</a></li>
<li><a href="http://www.dickbaldwin.com/Cosc1315/Slides/Pf00100MainSlideIndex.htm">Object-Oriented Programming Fundamentals using C++ (Slides)</a></li>
</ul>
</ul>
<ul>
<ul>
<li><a href="http://www.dickbaldwin.com/AdvOOP/AdvCpp00100Index.htm">Advanced Object-Oriented Programming using C++</a></li>
<li><a href="http://www.dickbaldwin.com/AdvOOP/PracticeTests/AdvCpp00100PracticeTestIndex.htm">Advanced Object-Oriented Programming using C++ (Practice Tests)</a></li>
<li><a href="http://www.dickbaldwin.com/AdvOOP/Slides/AdvCpMainSlideIndex.htm">Advanced Object-Oriented Programming using C++ (Slides)</a></li>
</ul>
</ul>
<ul>
<ul>
<li><a href="http://www.dickbaldwin.com/allegro/Allegro00100Index.htm">Graphics Programming with Allegro and C++</a></li>
<li><a href="http://www.dickbaldwin.com/allegro/PracticeTests/Allegro00100PracticeTestIndex.htm">Graphics Programming with Allegro and C++ (Practice Tests)</a></li>
<li><a href="http://www.dickbaldwin.com/allegro/Slides/AllegMainSlideIndex.htm">Graphics Programming with Allegro and C++ (Slides)</a></li>
</ul>
</ul>
<ul>
<ul>
<li><a href="http://www.austincc.edu/baldwin/Itnw1351Wireless/LabProjects/FwlProjIndex.htm">Wireless Networking Lab Projects</a></li>
<li><a href="http://www.dickbaldwin.com/tocalice.htm">Learn to Program using Alice</a></li>
<li><a href="http://www.dickbaldwin.com/tocHomeSchool.htm">Computer Programming for Homeschool Students and Other Beginners</a></li>
<li><a href="http://www.dickbaldwin.com/tocFlex.htm">Programming with Adobe Flex</a></li>
<li><a href="http://www.dickbaldwin.com/tocActionScript.htm">Object-Oriented Programming (OOP) with ActionScript </a></li>
<li><a href="http://www.dickbaldwin.com/tocXNA.htm">Programming with XNA Game Studio </a></li>
</ul>
</ul>
<h4><strong>Web库</strong></h4>
<ul>
<li><strong>20 个 jQuery提示插件</strong>：<a href="http://zoomzum.com/jquery-tooltip-plugins/">http://zoomzum.com/jquery-tooltip-plugins/</a></li>
</ul>
<ul>
<li><strong>最近出的一个新的可以做Web幻灯片的Javscript</strong> <a href="http://imakewebthings.github.com/deck.js/#intro">http://imakewebthings.github.com/deck.js/#intro</a> 当然，Web上做幻灯片的库太多了，大家可以看看wikipedia上的一个收集： <a href="http://en.wikipedia.org/wiki/Web-based_slideshow">http://en.wikipedia.org/wiki/Web-based_slideshow</a></li>
</ul>
<ul>
<li><strong><a href="http://code.google.com/p/google-api-php-client/">Google APIs Client Library for PHP</a> &#8211; </strong>用PHP封装的各种Google API<br />
<img decoding="async" loading="lazy" title="Google APIs Client Library for PHP" src="https://coolshell.cn/wp-content/uploads/2011/09/Google-APIs-Client-Library-for-PHP.png" alt="" width="447" height="62" /></li>
<ul>
<li>Buzz API &#8211; <a href="https://code.google.com/p/google-api-php-client/source/browse/#svn%2Ftrunk%2Fexamples%2Fbuzz">Sample</a></li>
<li>Books API &#8211; <a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/books/index.php">Sample</a></li>
<li>Latitude API &#8211; <a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/latitude/index.php">Sample</a></li>
<li>Page Speed Online API &#8211; <a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/pagespeed/index.php">Sample</a></li>
<li>Tasks API &#8211; <a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/tasks/index.php">Sample</a></li>
<li>URL Shortener API &#8211; <a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/urlshortener/index.php">Sample</a></li>
</ul>
</ul>
<ul>
<li><strong>Django Google Chart</strong> <a href="http://publishedin.com/django-google-charts/" target="_blank">http://publishedin.com/django-google-charts/</a>  为Django封闭的Google 统计图API。</li>
</ul>
<p><img decoding="async" class="aligncenter" src="https://s3.amazonaws.com/files_desu/django-google-charts-basic.png" alt="django-google-charts" /></p>
<ul>
<li><strong>一个新的HTML5+CSS3的JS库Kendo UI</strong>：<a href="http://demos.kendoui.com/" target="_blank">http://demos.kendoui.com/</a> 这样的JS库有很多，如比较经典的ExtJS, YUI 和 jQuery。不过大家可以试试这个库。其支持移动设备。</li>
</ul>
<p><a id="launch-image" href="http://www.kendoui.com/aeroviewr/"><img decoding="async" class="aligncenter" src="http://demos.kendoui.com/styles/aeroviewr.png" alt="AeroViewr" /></a></p>
<h4><strong>HTML 5<br />
</strong></h4>
<ul>
<li><strong>HTML5 Canvas 的开发指导</strong>：<a href="http://www.sitepoint.com/a-developer%E2%80%99s-guide-to-html5-canvas/" target="_blank">http://www.sitepoint.com/a-developer%E2%80%99s-guide-to-html5-canvas/</a></li>
</ul>
<ul>
<li><strong>HTML5+ Javascript的游戏开发教程</strong>：<a href="http://gamedev.slashgame.net/2011/08/html5-game-development-tutorial.html" target="_blank">http://gamedev.slashgame.net/2011/08/html5-game-development-tutorial.html</a></li>
</ul>
<ul>
<li><strong>HTML 5速查卡</strong>（PDF） <a href="http://www.thecssninja.com/talks/dnd_and_friends/assets/html5-cheat-sheet.pdf">http://www.thecssninja.com/talks/dnd_and_friends/assets/html5-cheat-sheet.pdf</a></li>
</ul>
<ul>
<li><strong>70 个 HTML5 的精彩示例</strong> <a href="http://www.instantshift.com/2011/07/05/70-inspirational-examples-of-websites-designed-with-html5/">http://www.instantshift.com/2011/07/05/70-inspirational-examples-of-websites-designed-with-html5/</a></li>
</ul>
<h4> 编程规范</h4>
<ul>
<li><strong>The Art of Assembly Language Programming 汇编语言艺术</strong> <a href="http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html">http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html</a></li>
</ul>
<ul>
<li><strong>编程规范 if语句的简单规则</strong>：<a href="http://united-coders.com/christian-harms/basic-rules-for-code-readability-and-the-if-statement">http://united-coders.com/christian-harms/basic-rules-for-code-readability-and-the-if-statement</a></li>
</ul>
<ul>
<li><strong>Linux 内核C编程规范：</strong><a href="http://www.kernel.org/doc/Documentation/CodingStyle" target="_blank">http://www.kernel.org/doc/Documentation/CodingStyle</a></li>
</ul>
<ul>
<li><strong>Google的C++编程规范：</strong><a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml</a></li>
</ul>
<ul>
<li><strong>GNU的编程规范：</strong><a href="http://www.gnu.org/prep/standards/standards.html">http://www.gnu.org/prep/standards/standards.html</a></li>
</ul>
<ul>
<li>最后，强烈推荐你读一下Nokia的Qt的《<a href="http://developer.qt.nokia.com/wiki/API_Design_Principles" target="_blank">API Design Principles</a>》，其中的一条规则写成了本站的《<a title="千万不要把 bool 设计成函数参数" href="https://coolshell.cn/articles/5444.html" target="_blank">千万不要用bool做函数参数</a>》</li>
</ul>
<h4><strong>其它</strong></h4>
<ul>
<li><strong>在OS X上使用gcc而不是xcode编译C++程序</strong> <a href="https://github.com/kennethreitz/osx-gcc-installer">https://github.com/kennethreitz/osx-gcc-installer</a></li>
</ul>
<ul>
<li><strong>声讨PHP的一个slids</strong> <a href="http://zakx.de/phprant-en.pdf">http://zakx.de/phprant-en.pdf</a>， 前面说到的两个网站都是使用PHP做到，不过，你可以通过这个PDF了解一下PHP有哪些地方不好。</li>
</ul>
<ul>
<li><strong>Infinite超级玛丽</strong>：(你可以比较一下，哪个版本不错)</li>
<ul>
<li>HTML5 版： <a href="http://mario.fromlifetodeath.com/" rel="nofollow">http://mario.fromlifetodeath.com/</a> (<a href="https://github.com/robertkleffner/mariohtml5" target="_blank">源码</a>)</li>
<li>Java版：<a href="http://www.mojang.com/notch/mario/">http://www.mojang.com/notch/mario/</a></li>
<li>Flash版：<a href="http://www.supermariobrothers.org/infinite-mario.html">http://www.supermariobrothers.org/infinite-mario.html</a></li>
</ul>
</ul>
<p><em><strong>—— 更新 2011.9.20  21:00 ——</strong></em></p>
<p>@<a href="https://coolshell.cn/articles/5224.html/comment-page-1#comment-82966">xzhaoyang</a> 在留言中问我有没有C写CGI的文章，我看过最好的一篇是下面这篇：</p>
<p><a href="http://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm" target="_blank">http://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm</a> （注意翻墙）</p>
<div>（全文完）</div>
<p><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/3013.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2010/09/biolab-150x150.jpg" alt="一些非常有意思的杂项资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3013.html" class="wp_rp_title">一些非常有意思的杂项资源</a></li><li ><a href="https://coolshell.cn/articles/12206.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2014/12/html6-150x150.jpeg" alt="HTML6 展望" width="150" height="150" /></a><a href="https://coolshell.cn/articles/12206.html" class="wp_rp_title">HTML6 展望</a></li><li ><a href="https://coolshell.cn/articles/12136.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2014/11/inbox2-640x264-150x150.jpg" alt="Google Inbox如何跨平台重用代码？" width="150" height="150" /></a><a href="https://coolshell.cn/articles/12136.html" class="wp_rp_title">Google Inbox如何跨平台重用代码？</a></li><li ><a href="https://coolshell.cn/articles/9666.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2013/05/Render-Process-150x150.jpg" alt="浏览器的渲染原理简介" width="150" height="150" /></a><a href="https://coolshell.cn/articles/9666.html" class="wp_rp_title">浏览器的渲染原理简介</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/5224.html">一些文章和各种资源</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/5224.html/feed</wfw:commentRss>
			<slash:comments>43</slash:comments>
		
		
			</item>
		<item>
		<title>PHP分页技术的代码和示例</title>
		<link>https://coolshell.cn/articles/5160.html</link>
					<comments>https://coolshell.cn/articles/5160.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Sun, 14 Aug 2011 06:49:22 +0000</pubDate>
				<category><![CDATA[PHP脚本]]></category>
		<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[编程语言]]></category>
		<category><![CDATA[Pagination]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=5160</guid>

					<description><![CDATA[<p>本文来自：10 Helpful PHP Pagination Scripts For Web Developers 分页是目前在显示大量结果时所采用的最好的方式...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/5160.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/5160.html">PHP分页技术的代码和示例</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>本文来自：<a href="http://zoomzum.com/php-pagination-scripts/" target="_blank">10 Helpful PHP Pagination Scripts For Web Developers</a></p>
<p>分页是目前在显示大量结果时所采用的最好的方式。有了下面这些代码的帮助，开发人员可以在多个页面中显示大量的数据。在互联网上，分​页是一般用于搜索结果或是浏览全部信息（比如：一个论坛主题）。几乎在每一个Web应用程序都需要划分返回的数据，并按页显示。下面的这个列表给出的代码可以让你的开发很有帮助。<strong>学习这些代码，对于初学者也很有帮助</strong>。</p>
<h4>1)<a href="http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html"> 使用Ajax分页</a></h4>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p style="text-align: left;">下面这个示例使用了jQuery + PHP。 <a href="http://demos.9lessons.info/pagination/pagination.php">Demo link</a></p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2512" title="Pagination" src="http://zoomzum.com/wp-content/uploads/2011/08/Pagination-e1312791884744.jpg" alt="" width="500" height="340" /></p>
<p style="text-align: left;"><span id="more-5160"></span></p>
<h4>2) <a href="http://php.about.com/od/phpwithmysql/ss/php_pagination.htm">MySql 分页</a></h4>
<p>&nbsp;</p>
<p style="text-align: left;">数据库的分页处理。</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2523" title="PHP-Pagination" src="http://zoomzum.com/wp-content/uploads/2011/08/PHP-Pagination1-e1312794857680.jpg" alt="" width="500" height="138" /></p>
<h4>3)<a href="http://youhack.me/2010/05/14/an-alternative-to-pagination-facebook-and-twitter-style/"> Facebook/Twitter 风格的分页</a></h4>
<p style="text-align: center;"><img decoding="async" loading="lazy" title="twitter-pagination" src="http://zoomzum.com/wp-content/uploads/2011/08/twitter-pagination-e1312792153888.png" alt="" width="500" height="350" /></p>
<h4>4)<a href="http://www.phpeasystep.com/phptu/29.html"> Php &amp; MySql 分页</a></h4>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2514" title="PHP Pagination" src="http://zoomzum.com/wp-content/uploads/2011/08/PHP-Pagination-e1312792516937.jpg" alt="" width="550" height="108" /></p>
<h4>5)<a href="http://www.bitrepository.com/css-stylish-pagination-links.html"> 分页风格</a></h4>
<p>&nbsp;</p>
<p style="text-align: left;">一个简单的教程教你如何用CSS定义不同风格的分页。</p>
<p><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2515" title="CSS Pagination" src="http://zoomzum.com/wp-content/uploads/2011/08/CSS-Pagination-e1312792632740.jpg" alt="" width="550" height="242" /></p>
<h4>6) <a href="http://phpsense.com/php/php-pagination-script.html" target="_blank">PHP 分页类</a></h4>
<p>&nbsp;</p>
<p style="text-align: left;">一个PHP的分页类</p>
<p><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2524" title="PHP Pagination Script" src="http://zoomzum.com/wp-content/uploads/2011/08/PHP-Pagination%C2%A0Script-e1312795287434.jpg" alt="" width="500" height="310" /></p>
<h4>7)<a href="http://www.phpeasycode.com/pagination/"> Easy Pagination</a></h4>
<p>这是一个PHP库，可以让你更容易的做分页。<br />
<img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2516" title="php easy code" src="http://zoomzum.com/wp-content/uploads/2011/08/php-easy-code.jpg" alt="" width="515" height="384" /></p>
<h4>8 ) <a href="http://www.phpfreaks.com/tutorial/basic-pagination">基本分页</a></h4>
<p>&nbsp;</p>
<p style="text-align: left;">一个很不错简单易懂的分页教程。</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2518" title="Pagination Script and Tutorial" src="http://zoomzum.com/wp-content/uploads/2011/08/Pagination-Script-and-Tutorial-e1312793432650.jpg" alt="" width="550" height="178" /></p>
<h4>9)<a href="http://www.developphp.com/view_lesson.php?v=289"> Php Page</a></h4>
<h3></h3>
<p style="text-align: left;">一个简单的PHP的教程</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2519" title="PHP Freaks" src="http://zoomzum.com/wp-content/uploads/2011/08/PHP-Freaks-e1312793481308.jpg" alt="" width="550" height="161" /></p>
<h4 style="text-align: left;">10) <a href="http://www.sitepoint.com/perfect-php-pagination/" target="_blank">perfect-php-pagination</a></h4>
<p>&nbsp;</p>
<p style="text-align: left;">也是一个分页教程。</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-2525" title="Perfect PHP Pagination" src="http://zoomzum.com/wp-content/uploads/2011/08/Perfect-PHP-Pagination.jpg" alt="" width="436" height="221" /></p>
<p style="text-align: left;">（全文完）</p>
<p><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/09/image008-150x150.jpg" alt="一些文章和各种资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_title">一些文章和各种资源</a></li><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/2394.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/1.jpg" alt="九个PHP很有用的功能" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2394.html" class="wp_rp_title">九个PHP很有用的功能</a></li><li ><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/20.jpg" alt="最为奇怪的程序语言的特性" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_title">最为奇怪的程序语言的特性</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/5160.html">PHP分页技术的代码和示例</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/5160.html/feed</wfw:commentRss>
			<slash:comments>18</slash:comments>
		
		
			</item>
		<item>
		<title>开源中最好的Web开发的资源</title>
		<link>https://coolshell.cn/articles/4795.html</link>
					<comments>https://coolshell.cn/articles/4795.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Wed, 08 Jun 2011 00:28:52 +0000</pubDate>
				<category><![CDATA[Web开发]]></category>
		<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=4795</guid>

					<description><![CDATA[<p>文章来源：Best “must know” open sources to build the new Web。个人感觉这个收集贴收集成相当的全。 学习HTML...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/4795.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/4795.html">开源中最好的Web开发的资源</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>文章来源：<a title="Best “must know” open sources to build the new Web" href="http://www.b2bweb.fr/molokoloco/best-must-know-ressources-for-building-the-new-web-%E2%98%85/" target="_blank">Best “must know” open sources to build the new Web</a>。个人感觉这个收集贴收集成相当的全。</p>
<h4>学习HTML 5编程和设计</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="wallOfWonder" src="http://www.b2bweb.fr/wp-content/uploads/wallOfWonder.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://www.html5rocks.com/" target="_blank"><strong>HTML5 Rocks</strong></a> : Major Feature Groups  的学习 HTML5 的资源 (HTML5 演示, 教程 ). <a href="http://code.google.com/p/html5rocks/" target="_blank">源码</a></li>
<li>很不错的 <a href="https://mozillademos.org/demos/dashboard/demo.html" target="_blank"><strong>HTML5 Dashboard</strong></a> &#8211; Mozilla，效果很炫。</li>
<li><a href="http://developers.whatwg.org/" target="_blank"><strong>WhatWG Developers</strong></a>, 一个清楚的 HTML5 技术规格说明书。</li>
<li>★ <a href="http://stackoverflow.com/" target="_blank"><strong>StackOverflow</strong></a> : 大名鼎鼎的技术问答式论坛。</li>
<li>★ <a href="http://addyosmani.com/blog/" target="_blank"><strong>Addyosmani</strong></a>, jQuery 和 JavaScript 文章教程</li>
<li><a href="http://www.sohtanaka.com/web-design-tutorials/" target="_blank"><strong>Sohtanaka</strong></a>, jQuery 和 JavaScript 文章和教程</li>
<li>★ <a href="http://net.tutsplus.com/category/tutorials/" target="_blank"><strong>Nettuts+</strong></a> 是一个面对Web开发人员和设计人员的网站，提供各种技术教程和文章，覆盖 HTML, CSS, Javascript, CMS’s, PHP 和 Ruby on Rails.</li>
<li><a href="http://tympanus.net/codrops/" target="_blank"><strong>Codrops</strong></a>, 教程和 web 资源</li>
<li><a href="http://www.webappers.com/" target="_blank"><strong>WebAppers</strong></a>, 最好的开源资源</li>
<li><a href="http://tutorialzine.com/" target="_blank"><strong>Tutorialzine</strong></a> – PHP MySQL jQuery CSS 教程, 资源和赠品</li>
<li><strong><a href="https://developer.mozilla.org/en/JavaScript/Guide" target="_blank">Mozilla JavaScript guide</a></strong></li>
<li> <a href="http://code.google.com/p/molokoloco-coding-project/" target="_blank"><strong>codes snippets</strong></a>, 作者自己收集的一些代码片段</li>
</ul>
<p><span id="more-4795"></span></p>
<h4>服务器端的软件</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="nodeJs" src="http://www.b2bweb.fr/wp-content/uploads/nodeJs.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://nodejs.org/" target="_blank"><strong>Node.js</strong></a> 是服务器端的 JavaScript 环境，其使用了异步事件驱动模式。其让Node.js在很多互联网应用体系结构下获得非常不错的性能。 <a href="https://github.com/joyent/node/" target="_blank">源码</a> 和 <a href="http://jsapp.us/" target="_blank">实时演示</a>。</li>
<li><a href="http://www.phantomjs.org/" target="_blank"><strong>PhantomJS</strong></a> 也是一个服务器端的 JavaScript API的WebKit。其支持各种Web标准： DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG</li>
<li><strong><a href="http://www.lighttpd.net/" target="_blank">Lighttpd</a></strong> 一个轻量级的开源Web服务器。新闻，文档，benchmarks, bugs, 和 download. Lighttpd 支撑了几个非常著名的 Web 2.0 网站，如：YouTube, wikipedia 和 meebo.</li>
<li><strong><a href="http://nginx.net/" target="_blank">NGinx</a></strong>, 性能巨高无比的轻量级的Web服务器。比Apache高多了。花了6年的时间，终于走到了1.0版。</li>
<li><strong><a href="http://httpd.apache.org/" target="_blank">Apache HTTP Server</a></strong> 是一个很流行的并支持多个流行的操作系统的Web服务器。</li>
<li>★ <strong><a href="http://nodejs.org/" target="_blank"></a><a href="http://www.php.net/" target="_blank">PHP</a></strong> 可能是最流行的服务器端的Web脚本动态处理语言。</li>
<li>当然，还有 <strong><a href="http://www.ruby-lang.org/fr/" target="_blank">Ruby</a></strong>, <strong><a href="http://www.python.org/" target="_blank">Python</a></strong>, <strong><a href="http://www.erlang.org/" target="_blank">Erlang</a></strong>, <strong><a href="http://www.perl.org/" target="_blank">Perl</a></strong>, <strong><a href="http://www.java.com/fr/" target="_blank">Java</a></strong>, <strong><a href="http://www.microsoft.com/net/" target="_blank">.NET</a></strong>, <strong><a href="http://www.android.com/" target="_blank">Android</a></strong>, <strong><a href="http://cpp.developpez.com/" target="_blank">C++</a></strong>, <strong><a href="http://golang.org/" target="_blank">Go</a></strong>,<a href="https://github.com/pmcelhaney/Mustache.cfc"></a><strong><a href="http://fantom.org/" target="_blank"> Fantom</a></strong>,<strong><a href="http://jashkenas.github.com/coffee-script/" target="_blank">CoffeeScript</a></strong>, <strong><a href="http://www.digitalmars.com/" target="_blank">D</a></strong>, …</li>
</ul>
<h4>PHP 框架和工具</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="drupal" src="http://www.b2bweb.fr/wp-content/uploads/drupal.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://wordpress.org/download/" target="_blank"><strong>WordPress</strong></a> 是一个基于博客系统的开源软件。参看《<a title="WordPress是怎么赢的？" href="https://coolshell.cn/articles/3716.html" target="_blank">WordPress是怎么赢的？</a>》</li>
<li><strong><a href="http://drupal.org/" target="_blank">Drupal</a></strong> 是一个内容管理系统 (CMS).</li>
<li><a href="http://www.centurion-project.org/" target="_blank"><strong>Centurion</strong></a> 是一个新出现的开源 CMS ，一个灵然的 PHP5 Content Management Framework. 使用 Zend Framework, 其组件坚持通用，简单，清楚和可重用的设计原则。</li>
<li><strong><a href="http://www.phpbb.com/" target="_blank">phpBB</a></strong> 一个开源的论坛（国内的Discuz！更多）</li>
<li><strong>★ <a href="http://simplepie.org/" target="_blank">SimplePie</a></strong> : 超快的，易用的,  RSS  和 Atom feed PHP解析。</li>
<li><strong>★ <a href="http://phpthumb.gxdlabs.com/" target="_blank">PHPthumb</a></strong>, PHP 图片处理库</li>
<li><strong>★ <a href="http://phpmailer.worxware.com/" target="_blank">PHPMailer</a></strong> 强大的全功能的PHP邮件库</li>
<li><strong><a href="http://code.google.com/p/pubsubhubbub/" target="_blank">PubSubHubbub</a></strong>协议，一个简单，开放， server-to-server 的 pubsub (publish/subscribe) 协议——Atom and RSS的扩展。</li>
<li>更多的请参看 &#8211; <a title="20 你应该知道的PHP库" href="https://coolshell.cn/articles/200.html" target="_blank">20个你应该知道PHP库</a> 和 <a title="9个强大免费的PHP库" href="https://coolshell.cn/articles/455.html" target="_blank">9个强大免费的PHP库</a></li>
</ul>
<h4>数据库</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="sqldesigner" src="http://www.b2bweb.fr/wp-content/uploads/sqldesigner.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://couchdb.apache.org/" target="_blank"><strong>Apache CouchDB</strong></a> 是一个面向文档的数据库管理系统。它提供以JSON 作为数据格式的REST 接口来对其进行操作，并可以通过视图来操纵文档的组织和呈现。.<a href="https://github.com/apache/couchdb" target="_blank">源码</a>.</li>
<li><a href="http://code.google.com/p/monoql/" target="_blank"><strong>MonoQL</strong></a> 是一个采用PHP+ExtJS开发的MySQL数据库管理工具。界面极像一个桌面应用程序，支持大部分常用的功能包括：表格设计，数据浏览/编辑，数据导入/导出和高级查询等。</li>
<li><strong> </strong><a href="http://mariadb.org/"><strong>MariaDB</strong></a> 是<a title="MySQL" href="http://www.mysql.com/" target="_blank">MySQL</a>的一个分支，由MySQL 创始人Monty Widenius 所开发。GPL，用来对抗Oracle所有的MySQL的license的不测。自Oracle收购SUN以来，整个社区对于MySQL前途的担忧就没有停止过。</li>
<li>★ <a href="http://www.sqlite.org/" target="_blank"><strong>SQLite</strong></a> 不像常见的客户端/服务器结构范例，SQLite引擎不是个程序与之通信的独立进程，而是连接到程序中成为它的一个主要部分。所以主要的通信协议是在编程语言内的直接API调用。这在消耗总量、延迟时间和整体简单性上有积极的作用。整个数据库（定义、表、索引和数据本身）都在宿主主机上存储在一个单一的文件中。它的简单的设计是通过在开始一个事务的时候锁定整个数据文件而完成的。库实现了多数的SQL-92标准，包括事务，就是代表原子性、一致性、隔离性和持久性的（ACID），触发器和多数的复杂查询。不进行类型检查。你可以把字符串插入到整数列中。某些用户发现这是使数据库更加有用的创新，特别是与无类型的脚本语言一起使用的时候。其他用户认为这是主要的缺点。</li>
<li><strong><a href="http://ondras.zarovi.cz/sql/demo/" target="_blank">SQL 在线设计编辑器</a></strong>，这一节的那个图片就是这个在线编辑器的样子了。一个画数据库图表的在线工具。很强大。</li>
</ul>
<h4>API 和 在线数据</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="yql" src="http://www.b2bweb.fr/wp-content/uploads/yql.png" alt="" width="600" height="238" /></p>
<ul>
<li><a href="http://www.programmableweb.com/apis/directory" target="_blank"><strong>ProgrammableWeb</strong></a>, 最流行的Web Services 和 API 目录大全。</li>
<li><strong><a href="http://code.google.com/intl/fr/apis/gdata/docs/directory.html" target="_blank">Google Data Protocol</a></strong> 一组Google服务的数据服务API。</li>
<li><a href="http://developer.yahoo.com/everything.html" target="_blank"><strong>Yahoo! Developer Network</strong></a> – APIs 和 Tools</li>
<li><a href="http://pipes.yahoo.com/" target="_blank"><strong>Yahoo! Pipes</strong></a> 可视化在线编程工具，它是一个用于过滤、转换和聚合网页内容的服务。</li>
<li>★ The <a href="http://developer.yahoo.com/yql/console/" target="_blank"><strong>Yahoo! Query Language</strong></a> 一个很像 SQL的网页查询工具。</li>
</ul>
<h4>在线代码和媒体编辑器</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="jsfiddle" src="http://www.b2bweb.fr/wp-content/uploads/jsfiddle.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://www.coderun.com/" target="_blank"><strong>CodeRun Studio</strong></a>一个基于JavaScript语言开发的跨平台的集成开发环境，它立足于云计算的设计思路，方便开发者在浏览器端便可以轻松开发、调试和部署网络应用程序。（参看《<a rel="bookmark" href="https://coolshell.cn/articles/1883.html" target="_blank">Coderun.com 在线开发IDE</a>》）</li>
<li><a href="https://github.com/ajaxorg/cloud9" target="_blank"><strong>Cloud9 IDE</strong></a> – 一个基于Node.JS构建的JavaScript程序开发Web IDE。它拥有一个非常快的文本编辑器支持为JS, HTML, CSS和这几种的混合代码进行着色显示。</li>
<li>★ <a href="http://jsfiddle.net/" target="_blank"><strong>jsFiddle</strong></a> – Javascript的在线运行展示框架，这个工具可以有效的帮助web前端开发人员来有效分享和演示前端效果，其简单而强大 (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)</li>
<li><strong><a href="http://www.akshell.com/" target="_blank">Akshell</a>，</strong>一种云服务，它使用服务端的JavaScript和在线的IDE帮助开发者进行快速应用程序开发。 它还提供云托管，所以部署是即时的。</li>
<li><a href="http://braincast.nl/samples/jsoneditor/" target="_blank"><strong>JSONeditor</strong></a>, 一个好用的JSON 编辑器</li>
<li>★ <a href="http://tinymce.moxiecode.com/wiki.php/TinyMCE" target="_blank"><strong>TinyMCE</strong></a> 一个轻量级的基于浏览器的所见即所得编辑器，支持目前流行的各种浏览器，由JavaScript写成。</li>
<li><a href="http://www.sencha.com/products/designer/" target="_blank"><strong>Ext Designer</strong></a> 是一个桌面应用工具，帮助你快速开发基于ExtJS 的用户界面。</li>
<li>★  <strong><a href="http://www.lucidchart.com/" target="_blank">LucidChart</a></strong>，一款基于最新的html5技术的在线图表绘制软件，功能强大，速度快捷，运行此软件需要支持html5的浏览器。</li>
<li><a href="http://balsamiq.com/products/mockups" target="_blank"><strong>Balsamiq Mockups</strong></a>, 产品设计师绘制线框图或产品原型界面的利器。</li>
<li><a href="http://colorschemedesigner.com/" target="_blank"><strong>Color Scheme Designer</strong></a> 3 &#8211; 一个免费的线上调色工具</li>
<li>★ <a href="http://pixlr.com/editor/" target="_blank"><strong>Pixlr</strong></a>, 是一个来自瑞典基于Flash的免费在线图片处理网站。除了操作介面和功能接近Photoshop，还是多语言版本，支持简体中文。（以前<a title="在线作图编辑服务" href="https://coolshell.cn/articles/3244.html" target="_blank">酷壳介绍过</a>）</li>
<li><a href="http://www.aviary.com/" target="_blank"><strong>Aviary</strong></a>, 是一个基于HTML5 的在线图片处理工具，可以很容易的对图片进行后期处理。 <a href="http://developers.aviary.com/" target="_blank">Aviary API</a></li>
<li><strong><a href="http://www.degraeve.com/favicon/" target="_blank">Favicon Generator</a>, </strong>线上favicon(16&#215;16)制作工具。</li>
</ul>
<h4>代码资源和版本控制</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="github" src="http://www.b2bweb.fr/wp-content/uploads/github.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="https://github.com/" target="_blank"><strong>GitHub</strong></a> 是一个用于使用Git版本控制系统的项目的基于互联网的存取服务。</li>
<li><a href="http://code.google.com/p/msysgit/" target="_blank"><strong>Git</strong></a> 是一个由Linus为了更好地管理linux内核开发而创立的分布式版本控制／软件配置管理软件。其巨快无比，高效，采用了分布式版本库的方式，不必服务器端软件支持，使源代码的发布和交流极其方便。</li>
<li><a href="http://code.google.com/" target="_blank"><strong>Google Code</strong></a> 谷歌公司官方的开发者网站，包含各种开发技术的API、开发工具、以及开发技术参考资料。</li>
<li><strong><a href="http://code.google.com/intl/zh-CN/apis/libraries/" target="_blank">Google Libraries API</a></strong> Google 将优秀的 JavaScript 框架部署在其 CDN 上，在我们的网站上使用 Google Libraries API 可以加速 JavaScript 框架的加载速度。</li>
<li><a href="http://snipplr.com/" target="_blank"><strong>Snipplr</strong></a> 一个开放的源代码技巧分享社区，号称Code 2.0。和一般的源码分享网站不同，它针对的并不是大型网站源码，而是一些编程的代码技巧。</li>
</ul>
<h4>JavaScript 桌面应用框架</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="jqueryUI" src="http://www.b2bweb.fr/wp-content/uploads/jqueryUI.jpg" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://jquery.com/" target="_blank"><strong>jQuery</strong></a> 是一个快速、简单的JavaScript library， 它简化了HTML 文件的traversing，事件处理、动画、Ajax 互动，从而方便了网页制作的快速发展。  <a href="https://github.com/jquery/jquery" target="_blank">源码</a>, <a href="http://api.jquery.com/" target="_blank">API</a>, <a href="http://api.jquery.com/browser/" target="_blank">API浏览</a>, <a href="http://interface.eyecon.ro/docs/animate" target="_blank">很不错的文档</a>.</li>
<li>★ 官方的 <a href="http://jqueryui.com/" target="_blank"><strong>jQuery User Interface (UI) library</strong></a> (演示和文档). <a href="https://github.com/jquery/jquery-ui%20" target="_blank">源码</a>,<a href="http://jqueryui.com/themeroller/" target="_blank">Themes Roller</a>, <a href="http://jqueryui.com/download" target="_blank">Download</a>.</li>
<li><a href="http://developer.yahoo.com/yui/2/" target="_blank"><strong>YUI 2</strong></a> — Yahoo! User Interface Library</li>
<li><a href="http://mootools.net/" target="_blank"><strong>Mootools</strong></a>, 一个超级轻量级的 web2.0 JavaScript framework</li>
<li><a href="http://www.prototypejs.org/" target="_blank"><strong>Prototype</strong></a> 提供面向对象的Javascript和AJAX</li>
<li><a href="http://dojotoolkit.org/" target="_blank"><strong>Dojo</strong></a> The Dojo Toolkit，一个强大的无法被打败的面向对象JavaScript框架。主要由三大模块组成：Core、Dijit、DojoX。Core提供Ajax,events,packaging,CSS-based querying,animations,JSON等相关操作API。Dijit是一个可更换皮肤，基于模板的WEB UI控件库。DojoX包括一些创新/新颖的代码和控件：DateGrid，charts，离线应用，跨浏览器矢量绘图等。</li>
<li>★ <a href="http://dev.sencha.com/deploy/ext-4.0.0/docs/" target="_blank"><strong>Ext JS 4</strong></a>, 业内最强大的 JavaScript framework。</li>
<li><a href="http://phpjs.org/functions/index" target="_blank"><strong>PHP.js</strong></a>, 一个开源的JavaScript 库，它尝试在JavaScript 中实现PHP 函数。在你的项目中导入<em>PHP.JS</em> 库，可以在静态页面使用你喜欢的PHP 函数。</li>
</ul>
<h4>JavaScript 移动和触摸框架</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="senchatouch" src="http://www.b2bweb.fr/wp-content/uploads/senchatouch.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://jquerymobile.com/" target="_blank"><strong>jQuery Mobile</strong></a> : 是 jQuery 在手机上和平板设备上的版本。jQuery Mobile 不仅会给主流移动平台带来jQuery核心库，而且会发布一个完整统一的jQuery移动UI框架。支持全球主流的移动平台。jQuery Mobile开发团队说：能开发这个项目，我们非常兴奋。移动Web太需要一个跨浏览器的框架，让开发人员开发出真正的移动Web网站。我们将尽全力去满足这样的需求。 <a href="https://github.com/jquery/jquery-mobile" target="_blank">Sources</a>.</li>
<li><a href="http://zeptojs.com/" target="_blank"><strong>Zepto.js</strong></a> Zepto.js 是支持移动WebKit浏览器的JavaScript框架，具有与jQuery兼容的语法。2-5k的库，通过不错的API处理绝大多数的基本工作。 <a href="https://github.com/madrobby/zepto" target="_blank">Sources</a>.</li>
<li><a href="http://microjs.com/" target="_blank"><strong>MicroJS</strong></a> : Microjs网站应用列出了很多轻量的Javascript类库和框架，它们都很小，大部分小于5kb。这样你不需要因为只需要一个功能就要加载一个JS的框架。</li>
<li>★ <a href="http://phonegap.com/" target="_blank"><strong>PhoneGap</strong></a> :是一款开源的手机应用开发平台，它仅仅只用HTML和JavaScript语言就可以制作出能在多个移动设备上运行的应用。 <a href="https://github.com/phonegap/phonegap" target="_blank">Sources</a>.</li>
<li>★ <a href="http://www.sencha.com/products/touch/" target="_blank"><strong>Sencha Touch</strong></a> Sencha Touch 是一个支持多种智能手机平台（iPhone, Android, 和BlackBerry）的 HTML5 框架。Sencha Touch可以让你的Web App看起来像Native App。美丽的用户界面组件和丰富的数据管理，全部基于最新的HTML5和CSS3的 WEB标准，全面兼容Android和Apple iOS设备。</li>
<li><a href="http://jqtouch.com/" target="_blank"><strong>JQtouch</strong></a>, 是一个jQuery 的插件，主要用于手机上的Webkit 浏览器上实现一些包括动画、列表导航、默认应用样式等各种常见UI效果的JavaScript 库。 <a href="http://github.com/senchalabs/jQTouch" target="_blank">Sources</a>.</li>
<li><a href="http://www.dhtmlx.com/touch/" target="_blank"><strong>DHTMLX Touch</strong></a> 针对移动和触摸设备的JavaScript 框架。DHTMLX Touch基于HTML5，创建移动web应用。它不只是一组UI 小工具，而是一个完整的框架，可以针对移动和触摸设备创建跨平台的web应用。它兼容主流的web浏览器，用DHTMLX Touch创建的应用，可以在iPad、iPhone、Android智能手机等上面运行流畅。</li>
</ul>
<h4>jQuery 插件</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="flexiGrid" src="http://www.b2bweb.fr/wp-content/uploads/flexiGrid.jpg" alt="" width="600" height="238" /></p>
<ul>
<li><a href="http://imakewebthings.github.com/jquery-waypoints/" target="_blank"><strong>Waypoints</strong></a> 是一个jQuery 用来实现捕获各种滚动事件的插件，例如实现无翻页的内容浏览，或者固定某个元素不让滚动等等。支持主流浏览器版本。</li>
<li><strong><a href="http://plugins.jquery.com/project/lazy" target="_blank">Lazy loader</a> </strong>插件可以实现图片的延迟加载，当网页比较长的时候，会先只加载用户视窗内的图片，视窗外的图片会等到你拖动滚动条至后面才加载，这样有效的避免了因图片过多而加载慢的弊端。</li>
<li><a href="https://github.com/gskinner/TweenJS" target="_blank"><strong>TweenJS</strong></a> : 一个简单和强大的 tweening / animation 的Javascript库。</li>
<li><a href="http://janne.aukia.com/easie/" target="_blank"> <strong>Easings</strong></a> 类Css3的jQuery 动画插件</li>
<li><a href="http://www.spritely.net/" target="_blank"><strong>Spritely</strong></a> 这个插件可以创建出如flash一样的动画效果，比如：在页面上有一只飞动的小鸟，一个动态滚动的背景等。</li>
<li><strong><a href="https://github.com/blueimp/jQuery-File-Upload/" target="_blank">File Upload</a>, </strong>jQuery 文件上传插件4.4.1</li>
<li><a href="http://www.agilecarousel.com/" target="_blank"><strong>Slideshow/Carousel</strong></a> 插件. <a href="https://github.com/edtalmadge/Agile-Carousel" target="_blank">Sources</a>.</li>
<li><a href="http://www.buildinternet.com/project/supersized/" target="_blank"><strong>Supersized</strong></a> – 全屏式的背景/幻灯片插件</li>
<li><a href="http://desandro.com/resources/jquery-masonry" target="_blank"><strong>Masonry</strong></a> i一款非常酷的自动排版插件，这款jQuery工具可以根据网格来自动排列水平和垂直元素，超越原来的css. <a href="https://github.com/desandro/masonry" target="_blank">Sources</a>.</li>
<li>jQuery 简单 <a href="http://layout.jquery-dev.net/demos.cfm" target="_blank"><strong>Layout</strong></a> 演示，管理各种边栏式，可改变大小式的布局。</li>
<li><a href="http://www.flexigrid.info/" target="_blank"><strong>Flexigrid</strong></a> – jQuery <a href="http://www.flexigrid.info/" target="_blank"><strong></strong></a>数据表插件</li>
<li><a href="http://isotope.metafizzy.co/" target="_blank"><strong>Isotope</strong></a>绝对是一个令人难以置信的<em>jQuery</em>插件，你可以用它来创建动态和智能布局。你可以隐藏和显示与过滤项目，重新排序和整理甚至更多。</li>
<li><a href="http://www.evanbyrne.com/article/super-gestures-jquery-plugin" target="_blank"><strong>Super Gestures</strong></a> jQuery 插件可以实现鼠标手势的功能。</li>
<li><a href="https://github.com/brandonaaron/jquery-mousewheel" target="_blank"><strong>MouseWheel</strong></a> 是由Brandon Aaron开发的<em>jQuery</em>插件，用于添加跨浏览器的鼠标滚轮支持。</li>
<li><a href="http://code.drewwilson.com/entry/autosuggest-jquery-plugin" target="_blank"><strong>AutoSuggest</strong></a> jQuery 插件可以让你添加一些自动完成的功能。</li>
<li><a href="http://craigsworks.com/projects/qtip/" target="_blank"><strong>qTip</strong></a> 一个漂亮的<em>jQuery</em> 的工具提示插件，这个插件功能相当强大。</li>
<li>jQuery <a href="http://www.highcharts.com/demo/" target="_blank"><strong>Charts and graphic</strong></a> 用来制作图表。</li>
<li>jQuery Tools– The <a href="http://flowplayer.org/tools/demos/" target="_blank"><strong>missing UI library</strong></a></li>
</ul>
<h4>其它 jQuery 资源</h4>
<ul>
<li><a href="http://www.smashingmagazine.com/2011/04/07/useful-javascript-and-jquery-tools-libraries-plugins" target="_blank">http://www.smashingmagazine.com/2011/04/07/useful-javascript-and-jquery-tools-libraries-plugins</a></li>
<li><a href="http://webdesigneraid.com/weekly-html5-news-and-inspirations-%E2%80%93-tutorials-tools-resources-and-freebies-v-2/" target="_blank">http://webdesigneraid.com/weekly-html5-news-and-inspirations-%E2%80%93-tutorials-tools-resources-and-freebies-v-2/</a></li>
<li><a href="http://www.designer-daily.com/15-useful-jquery-plugins-and-tutorials-5207" target="_blank">http://www.designer-daily.com/15-useful-jquery-plugins-and-tutorials-5207</a></li>
<li><a href="http://www.julien-verkest.fr/22/11/2007/240-plugins-jquery" target="_blank">http://www.julien-verkest.fr/22/11/2007/240-plugins-jquery</a></li>
<li><a href="http://www.hotscripts.com/blog/10-great-html5-experiments-apps/" target="_blank">http://www.hotscripts.com/blog/10-great-html5-experiments-apps/</a></li>
<li><a href="http://www.noupe.com/jquery/excellent-jquery-navigation-menu-tutorials.html" target="_blank">http://www.noupe.com/jquery/excellent-jquery-navigation-menu-tutorials.html</a></li>
<li><a href="http://www.noupe.com/php/20-useful-php-jquery-tutorials.html" target="_blank">http://www.noupe.com/php/20-useful-php-jquery-tutorials.html</a></li>
<li><a href="http://aext.net/2010/04/excellent-jquery-plugins-resources-for-data-presentation-and-grid-layout/" target="_blank">http://aext.net/2010/04/excellent-jquery-plugins-resources-for-data-presentation-and-grid-layout/</a></li>
<li><a href="http://webdesigneraid.com/html5-canvas-graphing-solutions-every-web-developers-must-know/" target="_blank">http://webdesigneraid.com/html5-canvas-graphing-solutions-every-web-developers-must-know/</a></li>
<li><a href="http://gestureworks.com/features/open-source-gestures/" target="_blank">http://gestureworks.com/features/open-source-gestures/</a></li>
<li><a href="http://edtechdev.wordpress.com/2011/01/14/some-exciting-new-html5javascript-projects/" target="_blank">http://edtechdev.wordpress.com/2011/01/14/some-exciting-new-html5javascript-projects/</a></li>
<li><a href="http://net.tutsplus.com/articles/web-roundups/30-developers-you-must-subscribe-to-as-a-javascript-junkie/" target="_blank">http://net.tutsplus.com/articles/web-roundups/30-developers-you-must-subscribe-to-as-a-javascript-junkie/</a></li>
</ul>
<h4>HTML5 视频播放器</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="leanBackPlayer" src="http://www.b2bweb.fr/wp-content/uploads/leanBackPlayer.jpg" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="https://github.com/webmademovies/popcorn-js" target="_blank"><strong>Popcorn.js</strong></a> 是一个HTML5 Video框架，它提供了易于使用的API来同步交互式内容，让操作HTML5 Video元素的属性，方法和事件变得简单易用。 (来自Mozilla)</li>
<li><a href="http://dev.mennerich.name/showroom/html5_video/" target="_blank"><strong>LeanBack Player</strong></a> HTML5视频播放器,没有依赖任何JavaScript框架。支持全屏播放，音量控制，在同一个页面中播放多个视频。 (来自Google)</li>
<li><a href="http://m.vid.ly/user/" target="_blank"><strong>Vid.ly</strong></a> 为你上传的视频提供转换功能，并且为转换后的视频创建一个短网址。通过Vid.ly，让你的视频可以在14种不同的浏览器和设备上播放，不需要再去考虑将要浏览视频的人使用什么设备了，以避免各各软件巨头之间的利益之争带来了不兼容，给用户带来了巨大的困扰，短网址让你可以通过Twitter、Facebook等方式方便分享视频。Vid.ly还可以通过html代码嵌入到其他网页中。Vid.ly免费帐户空间为1GB，免费帐户也没有播放或浏览限制。</li>
</ul>
<h4>JavaScript 音频处理与可视化效果</h4>
<p><img decoding="async" loading="lazy" title="soundmanager" src="http://www.b2bweb.fr/wp-content/uploads/soundmanager.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ 使用HTML5 和 Flash, <a href="http://www.schillmania.com/projects/soundmanager2/" target="_blank"><strong>SoundManager V2</strong></a> 只用单一API的提供了可靠，简单和强大的跨平台的音频处理。</li>
<li><a href="https://github.com/corbanbrook/dsp.js/" target="_blank"><strong>DSP</strong></a>, JavaScript的声音Digital Signal Processing</li>
<li>The Radiolab <a href="http://yoyodyne.cc/radiolab/" target="_blank"><strong>Hyper Audio Player</strong></a> v1, 带给你 WNYC Radiolab, SoundCloud 和 Mozilla Drumbeat</li>
<li><a href="http://jplayer.org/" target="_blank"><strong>jPlayer</strong></a>, 一个 jQuery HTML5 音频/ 视频库，功能齐全的API</li>
</ul>
<h4>JavaScript 图形 和 3D</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="processing" src="http://www.b2bweb.fr/wp-content/uploads/processing.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://processingjs.org/" target="_blank"><strong>Processing.js</strong></a>是一个开放的编程语言，在不使用Flash或Java小程序的前提下, 可以实现程序图像、动画和互动的应用。其使用Web标准，无需任何插件。</li>
<li>★ Javascript 3D 引擎: <a href="https://github.com/mrdoob/three.js" target="_blank"><strong>ThreeJS</strong></a> 由 Mr Doob 开发，一个轻量级的 3D 引擎，不需要了解细节，傻瓜都能使用。这个引擎可以使用&lt;canvas&gt;, &lt;svg&gt; 和 WebGL.</li>
<li><a href="http://www.iquilezles.org/apps/shadertoy/" target="_blank"><strong>Shader Toy</strong></a>, 一款使用WebGL的在线着色器编辑器(2D/3D). 基于在线的应用架构使您无需下载任何软件即可开始体验. Shader Toy包含大量实用着色器, 诸如光线追踪, 场景距离渲染, 球体, 隧道, 变形, 后期处理特效等.</li>
<li><a href="http://senchalabs.github.com/philogl/" target="_blank"><strong>PhiloGL</strong></a>, Sencha的PhiloGL是首个WebGL开发工具之一，提供了高水准的功能，来构建WebGL应用。Sencha创建了几个演示，来描述框架交互式3D虚拟化的能力，比如<a href="http://senchalabs.github.com/philogl/PhiloGL/examples/temperatureAnomalies/">3D view of global temperature changes</a>。</li>
<li><a href="http://benvanik.github.com/WebGL-Inspector/" target="_blank"><strong>WebGL Inspector</strong></a> 你就Firebug等Web调试工具一样，这个是 WebGL的调试工具。</li>
<li><a href="http://www.khronos.org/webgl/wiki_1_15/" target="_blank"><strong>WebGL frameworks</strong></a> 由 Khronos Group 收集的一个WebGL框架列表。</li>
<li><a href="http://easeljs.com/" target="_blank"><strong>EaselJS</strong></a>, 一个使用html5的canvas的 JavaScript 库. <a href="https://github.com/gskinner/EaselJS" target="_blank">Sources</a>.</li>
<li><a href="http://www.webresourcesdepot.com/free-javascript-game-frameworks-to-create-a-web-based-fun/" target="_blank"><strong>JavaScript Game Frameworks</strong></a> 免费的JS游戏框架列表。另，可参看 <a title="JS游戏引擎列表" href="https://coolshell.cn/articles/3516.html" target="_blank">JS游戏框架列表</a>。</li>
<li><a href="http://raphaeljs.com/" target="_blank"><strong>Raphaël</strong></a>是一个小型的JavaScript 库，用来简化在页面上显示向量图的工作。你可以用它在页面上绘制各种图表、并进行图片的剪切、旋转等操作。参看<a title="Javascript向量图Lib–Raphaël" href="https://coolshell.cn/articles/3107.html" target="_blank">Javascript向量图Lib–Raphaël</a></li>
<li><a href="http://keith-wood.name/svgRef.html" target="_blank"><strong>jQuery SVG</strong></a> 插件让你可以了 SVG canvas 进行交互。</li>
<li><a href="http://code.google.com/intl/fr/apis/chart/" target="_blank"><strong>Google chart tools</strong></a> &#8211;  参看本站的<a href="https://coolshell.cn/articles/582.html" target="_blank">使用Google API做统计图</a></li>
<li><a href="https://coolshell.cn/articles/582.html" target="_blank"></a><a href="http://arborjs.org/" target="_blank"><strong>Arbor.js</strong></a>, 是一个利用webworkers和jQuery创建的数据图形可视化JavaScript框架。它为图形组织和屏幕刷新处理提供了一个高效、力导向布局算法。</li>
</ul>
<h4>JavaScript 浏览器接口 (HTML5)</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="amplify" src="http://www.b2bweb.fr/wp-content/uploads/amplify.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://www.modernizr.com/" target="_blank"><strong>Modernizr</strong></a> – 是一个专为HTML5 和CSS3 开发的功能检测类库，可以根据浏览器对HTML5 和CSS3 的支持程度提供更加便捷的前端优化方案.<a href="https://github.com/Modernizr/Modernizr" target="_blank">Sources</a>. 一个有用的列表 <a href="https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills" target="_blank">cross-browser Polyfills</a></li>
<li><a href="http://code.google.com/p/html5shiv/" target="_blank"><strong>HTML5Shiv</strong></a> : 该项目的目的是为了让IE 能识别HTML5 的元素。</li>
<li><a href="https://github.com/remy/polyfills" target="_blank"><strong>Polyfills</strong></a> : 这个项目收集了一些代码片段其用Javascript支持不同的浏览器的特别功能，有些代码需要Flash。</li>
<li><a href="http://yepnopejs.com/" target="_blank"><strong>YepNopeJS</strong></a> : 一个异步的条件式的加载器。<a href="https://github.com/SlexAxton/yepnope.js" target="_blank">Sources</a>.</li>
<li>jQuery <a href="https://github.com/codler/jQuery-Css3-Finalize/" target="_blank"><strong>CSS3 Finalise</strong></a> : 是否厌倦了为每一个浏览器的CSS3属性加前缀？</li>
<li>★ <a href="http://amplifyjs.com/" target="_blank"><strong>Amplify.js</strong></a> :一套用于web应用数据管理和应用程序通讯的<strong> jQuery 组件库</strong>。提供简单易用的API接口。Amplify的目标是通过为各种数据源提供一个统一的程序接口简化各种格式数据的数据处理。Amplify的存储组件使用localStorage 和 sessionStorage标准处理客户端的存储信息，对一些老的浏览器支持可能有问题。Amplify&#8217;为jQuery的ajax方法request增加了一些额外的特性。 <a href="https://github.com/appendto/amplify" target="_blank">Sources</a>.</li>
<li><a href="https://github.com/balupton/history.js" target="_blank"><strong>History.js</strong></a> 优美地支持了HTML5 History/State APIs</li>
<li><a href="http://socket.io/" target="_blank"><strong>Socket.IO</strong></a> Web的socket编程。</li>
</ul>
<h4>JavaScript 工具</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="headJs" src="http://www.b2bweb.fr/wp-content/uploads/headJs.png" alt="" width="600" height="238" /></p>
<ul>
<li>★  {{<a href="http://mustache.github.com/" target="_blank"><strong>mustaches</strong></a>}} 小型的 JavaScript 模板引擎。</li>
<li><a href="http://jsonselect.org/" target="_blank"><strong>json:select()</strong></a>, CSS式的JSON选择器</li>
<li><a href="http://headjs.com/" target="_blank"><strong>HeadJS</strong></a>, 异步JavaScript装载。其最大特点就是不仅可以按顺序执行还可以并发装载载js。</li>
<li><a href="http://code.google.com/p/jsdoc-toolkit/" target="_blank"><strong>JsDoc Toolkit</strong></a>是一款辅助工具，你只需要根据约定在JavaScript 代码中添加相应的注释，它就可以根据这些注释来自动生成API文档。</li>
<li><a href="https://github.com/filamentgroup/Responsive-Images" target="_blank"><strong>Responsive image</strong></a>, 一个试验性的项目，用来处理<a href="http://www.alistapart.com/articles/responsive-web-design/">responsive layouts</a> 式的图片。</li>
<li><a href="http://marijnhaverbeke.nl/uglifyjs" target="_blank"><strong>UglifyJS</strong></a>是基于NodeJS的Javascript语法解析/压缩/格式化工具，它支持任何CommonJS模块系统的Javascript平台。</li>
<li><a href="http://www.dhteumeuleu.com/" target="_blank"><strong>Dhteumeuleu</strong></a>, 交互式的 DOM 脚本和DHTML 的开源演示。</li>
<li><a href="https://github.com/documentcloud/backbone/" target="_blank"><strong>Backbone</strong></a>是一个前端 JS 代码 MVC 框架，被著名的 37signals 用来构建他们的移动客户端。它不可取代 Jquery，不可取代现有的Template 库。而是和这些结合起来构建复杂的 web 前端交互应用。如果项目涉及大量的 javascript 代码，实现很多复杂的前端交互功能，首先你会想到把数据和展示分离。使用 Jquery 的 selector 和 callback 可以轻松做到这点。但是对于富客户端的WEB应用大量代码的结构化组织非常必要。Backbone 就提供了 javascript 代码的组织的功能。Backbone 主要包括 models, collections, views 和 events, controller 。</li>
</ul>
<h4>客户端和模拟器</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="firebug" src="http://www.b2bweb.fr/wp-content/uploads/firebug.png" alt="" width="600" height="238" /></p>
<ul>
<li><a href="http://browsershots.org/" target="_blank"><strong>BrowserShot</strong></a>, 检查浏览器的兼容性，跨浏览器平器的测试</li>
<li><strong><a href="http://tester.jonasjohn.de/" target="_blank">Test everything</a></strong>… 输入一个你想要测试的URL……</li>
<li><a href="http://tmobile.modeaondemand.com/htc/g1/" target="_blank"><strong>Android browser</strong></a> 模拟器</li>
<li><a href="http://iphonetester.com/" target="_blank"><strong>iPhone browser</strong></a> 模拟器</li>
<li><a href="http://www.opera.com/mobile/demo/" target="_blank"><strong>Opera browser</strong></a> 模拟器</li>
<li>★ <a href="http://getfirebug.com/whatisfirebug" target="_blank"><strong>Firebug</strong></a> 与 <strong><a href="http://www.mozilla.com/fr/firefox/" target="_blank">Firefox</a></strong> 集成，可以查看和调试你的Web页面。</li>
</ul>
<h3>CSS3 和 字库</h3>
<p><img decoding="async" loading="lazy" class="aligncenter" title="patternTap" src="http://www.b2bweb.fr/wp-content/uploads/patternTap.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://www.css3maker.com/" target="_blank"><strong>CSS3 Maker</strong></a> CCS3的生成器</li>
<li>容易地创建 <strong><a href="http://www.sencha.com/products/animator/" target="_blank">CSS3 animations</a>。</strong> Sencha Animator 是一个桌面应用可以为WebKit浏览器和触摸式移动设备创建 CSS3 animations 。</li>
<li><a href="http://csswarp.eleqtriq.com/" target="_blank"><strong>CSSwarp</strong></a> – CSS 文本扭曲生成器</li>
<li><a href="http://www.colorzilla.com/gradient-editor/" target="_blank"><strong>Gradient Editor</strong></a>, 一个强大的Photoshop式的CSS 渐变编译器。来自 ColorZilla</li>
<li>★ <a href="http://www.google.com/webfonts" target="_blank"><strong>Google Web Fonts</strong></a> 通过Google Web Fonts API 可以浏览所有的字体</li>
<li><a href="http://www.fontsquirrel.com/fontface/generator" target="_blank"><strong>@font-face Kit Generator</strong></a>, 为Web转换字体</li>
<li><a href="http://www.typetester.org/" target="_blank"><strong>Typetester</strong></a>, 比较字体。</li>
<li><a href="http://mediaqueri.es/" target="_blank"><strong>Media Queries</strong></a>. 一组 responsive web 设计。</li>
<li><a href="http://patterntap.com/" target="_blank"><strong>Pattern TAP</strong></a>, UI组件。</li>
</ul>
<h4>Website (FULL) 模板</h4>
<p><img decoding="async" loading="lazy" class="aligncenter" title="boilerplate" src="http://www.b2bweb.fr/wp-content/uploads/boilerplate1.png" alt="" width="600" height="238" /></p>
<ul>
<li>★ <a href="http://html5boilerplate.com/" target="_blank"><strong>HTML5 Boilerplate</strong></a> 是一个<a href="http://www.mhtml5.com/">HTML5 </a>/ CSS / js模板，是实现跨浏览器正常化、性能优化，稳定的可选功能如跨域Ajax和Flash的最佳实践。 项目的开发商称之为技巧集合，目的是满足您开发一个跨浏览器，并且面向未来的网站的需求。 <a href="https://github.com/paulirish/html5-boilerplate" target="_blank">Sources</a>.</li>
<li><a href="http://sickdesigner.com/resources/HTML5-starter-pack/" target="_blank"><strong>HTML5 starter pack</strong></a> 是一个干净的和有组织的目录结构，其可适合很多项目，还有一些很常用的文件，以及简单的Photoshop设计模板。</li>
<li>★ <a href="http://initializr.com/" target="_blank"><strong>Initializr</strong></a> 是一个HTML5 模板生成器，其可以帮你在15秒内创建一个HTML5的项目。</li>
<li><a href="http://tympanus.net/Tutorials/AnimatedPortfolioGallery/" target="_blank"><strong>Animated Portfolio Gallery</strong></a> （<a href="http://tympanus.net/codrops/2010/11/14/animated-portfolio-gallery/" target="_blank">教程</a>）</li>
<li> <a href="http://tutorialzine.com/2010/07/making-slick-mobileapp-website-jquery-css/" target="_blank"><strong>Slick MobileApp Website</strong></a> 如果通过 jQuery 和 CSS 制作一个手机应用的网站。</li>
<li> <a href="http://net.tutsplus.com/tutorials/javascript-ajax/how-to-build-an-rss-reader-with-jquery-mobile-2/" target="_blank"><strong>RSS Reader</strong></a> 如果通过 jQuery Mobile 创建一个RSS Reader</li>
<li>★ <a href="http://addyosmani.com/blog/building-spas-jquerys-best-friends/" target="_blank"><strong>Single Page Applications</strong></a> 使用jQuery的朋友们 (Backbone, Underscore, …)创建单一页面。</li>
<li><a href="http://code.google.com/p/gtv-resources/" target="_blank"><strong>Google TV Optimized Templates</strong></a>, 传统电视已经开始和网路融合，但现阶段产业仍然正在摸索之中，为此将来的网页亦会有结构上的改变。<a href="http://code.google.com/p/gtv-resources/">Google TV Optimized Templates</a>是一个用HTML/JavaScript制成的开源软体，一如其名是一个对Google TV作出了最佳化的的网页范本，其特色是以遥控器作为操作的前提，令使用者无需输入任何文字就可以进行控制。未来除了会有专用遥控器外，还会采用智能手机透过W-iFi控制Google TV的方法。Optimized Templates的界面中左方会展示分类，右方会显示该分类下的影片截图，影片播放、切换、全画面表示都可透过键盘上的方向键、Backspace或Enter等键完成，方便今后的网站开发人员借镜。HTML5 版的模板使用了 <a href="http://code.google.com/p/gtv-ui-lib" target="_blank">Google TV UI library</a>, jQuery  和 Closure 。</li>
</ul>
<p>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/9666.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2013/05/Render-Process-150x150.jpg" alt="浏览器的渲染原理简介" width="150" height="150" /></a><a href="https://coolshell.cn/articles/9666.html" class="wp_rp_title">浏览器的渲染原理简介</a></li><li ><a href="https://coolshell.cn/articles/5537.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/11/stackparts.com_-150x150.png" alt="一些文章资源和趣闻" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5537.html" class="wp_rp_title">一些文章资源和趣闻</a></li><li ><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/09/image008-150x150.jpg" alt="一些文章和各种资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_title">一些文章和各种资源</a></li><li ><a href="https://coolshell.cn/articles/3903.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/14.jpg" alt="一些有意思的贴子和工具" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3903.html" class="wp_rp_title">一些有意思的贴子和工具</a></li><li ><a href="https://coolshell.cn/articles/1949.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/16.jpg" alt="Web中的省略号" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1949.html" class="wp_rp_title">Web中的省略号</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/4795.html">开源中最好的Web开发的资源</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/4795.html/feed</wfw:commentRss>
			<slash:comments>124</slash:comments>
		
		
			</item>
		<item>
		<title>Web开发人员速查卡</title>
		<link>https://coolshell.cn/articles/3684.html</link>
					<comments>https://coolshell.cn/articles/3684.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Wed, 16 Feb 2011 10:59:06 +0000</pubDate>
				<category><![CDATA[Web开发]]></category>
		<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[Cheat Sheet]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unicode]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=3684</guid>

					<description><![CDATA[<p>无论你是多牛的程序员，你都无法记住所有的东西。而很多时候，查找某些知识又比较费事。所以，网上有很多Cheat Sheets，翻译成小抄也好 ，速查卡也好，总之就...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/3684.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/3684.html">Web开发人员速查卡</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>无论你是多牛的程序员，你都无法记住所有的东西。而很多时候，查找某些知识又比较费事。所以，网上有很多Cheat Sheets，翻译成小抄也好 ，速查卡也好，总之就是帮你节省 时间的。之前给大家介绍过<a rel="bookmark" href="https://coolshell.cn/articles/870.html" target="_blank">Web设计的速查卡</a>、<a rel="bookmark" href="https://coolshell.cn/articles/2964.html" target="_blank">25个jQuery的编程小抄</a>，还有<a rel="bookmark" href="https://coolshell.cn/articles/1566.html" target="_blank">程序员小抄大全</a>，今天转一篇开发人员的速查卡，<a href="http://www.topdesignmag.com/all-the-cheat-sheets-that-a-web-developer-needs/" target="_blank">源文在这里</a>。下面的文章我就不翻译了。</p>
<h2>HTML Cheat Sheet</h2>
<p><img decoding="async" loading="lazy" title="1" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/1128.jpg" alt="" width="450" height="127" /></p>
<ul>
<li><a href="http://www.html.su/" target="_blank">HTML/XTML in one page</a></li>
<li><a href="http://refcardz.dzone.com/refcardz/html5-new-standards-web-interactivity" target="_blank">HTML5: The Evolution of Web Standards by James Sugrue</a></li>
<li><a href="http://www.elizabethcastro.com/html/extras/xhtml_ref.html" target="_blank">(X)HTML Elements and Attributes</a></li>
<li><a href="http://www.w3.org/QA/2002/04/valid-dtd-list.html" target="_blank">Doctype Declarations (DTDs)</a></li>
<li><a href="http://www.digitalmediaminute.com/reference/entity/index.php" target="_blank">XHTML Character Entity Reference</a></li>
<li><a href="http://downloads.gosquared.com/help_sheets/08/HTML-Help-Sheet-02.jpg" target="_blank">GoSquared HTML Help Sheet</a></li>
</ul>
<p><span id="more-3684"></span></p>
<p><strong> </strong></p>
<h2>CSS Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="2" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/2104.jpg" alt="" width="451" height="112" /></p>
<ul>
<li><a href="http://www.css.su/" target="_blank">CSS in one page</a></li>
<li><a href="http://www.elizabethcastro.com/html/extras/cssref.html" target="_blank">CSS Properties and Values</a></li>
<li><a href="http://www.blooberry.com/indexdot/css/propindex/all.htm" target="_blank">All CSS Properties Listed Alphabetically</a></li>
<li><a href="http://www.dustindiaz.com/css-shorthand/" target="_blank">CSS Shorthand Guide</a></li>
<li><a href="http://www.gosquared.com/liquidicity/archives/1010" target="_blank">GoSquared CSS Help Sheet</a></li>
</ul>
<h2>Adobe Flash Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="3" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/312.png" alt="" width="449" height="87" /></p>
<ul>
<li><a href="http://michaeldoyle.eu/blog/wp-content/uploads/2009/10/flash-cheat-sheet.pdf" target="_blank">Flash Cheat Sheet</a></li>
<li><a href="http://edutechwiki.unige.ch/en/Flash_CS3_keyboard_shortcuts" target="_blank">Flash CS3 Keyboard Shortcuts</a></li>
</ul>
<p><strong> </strong></p>
<h2><strong>ASP Cheat Sheets</strong></h2>
<h2><strong><img decoding="async" loading="lazy" title="4" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/430.jpg" alt="" width="451" height="106" /><br />
</strong></h2>
<ul>
<li><a href="http://refcardz.dzone.com/refcardz/core-aspnet" target="_blank">Core ASP.NET</a></li>
<li><a href="http://www.newdrp.com/Posters/Development/tabid/67/id/284/Default.aspx" target="_blank">ASP.NET MVC Framework Cheat Sheet</a></li>
<li><a href="http://www.newdrp.com/Posters/Development/tabid/67/id/286/Default.aspx" target="_blank">ASP.NET MVC View Cheat Sheet</a></li>
</ul>
<h2>PHP Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="5" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/55.png" alt="" width="450" height="112" /></p>
<ul>
<li><a href="http://www.dreamincode.net/forums/topic/35660-php-quick-reference-cheat-sheet/" target="_blank">PHP Basics Quick Reference Sheet</a></li>
<li><a href="http://www.digilife.be/quickreferences/QRC/PHP%20Cheat%20Sheet.pdf" target="_blank">PHP Cheat Sheet</a></li>
<li><a href="http://www.sk89q.com/content/2010/04/phpsec_cheatsheet.pdf" target="_blank">PHP Security Cheat Sheet</a></li>
<li><a title="PHP Variable and Array Tests (php version 5.1.6) by Barry Hunter" href="http://www.deformedweb.co.uk/php_variable_tests.php" target="_blank">PHP Variable and Array Tests</a></li>
<li><a href="http://downloads.gosquared.com/help_sheets/08/PHP-Help-Sheet-01.jpg" target="_blank">GoSquared PHP Help Sheet</a></li>
</ul>
<h2>MySQL Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="6" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/65.png" alt="" width="450" height="89" /></p>
<ul>
<li><a href="http://www.addedbytes.com/cheat-sheets/mysql-cheat-sheet/" target="_blank">MySQL Cheat Sheet by Dave Child</a></li>
<li><a href="http://www.cheat-sheets.org/saved-copy/MySQL_QuickRef.pdf" target="_blank">MySQL Database Quick Reference</a></li>
<li><a href="http://www.sqltutorial.org/sql-cheat-sheet.aspx" target="_blank">SQL Statements Cheat Sheet</a></li>
</ul>
<h2>JavaScript Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="7" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/75.png" alt="" width="451" height="118" /></p>
<ul>
<li><a href="http://www.javascript.su/" target="_blank">JavaScript in one page</a></li>
<li><a href="http://www.addedbytes.com/cheat-sheets/javascript-cheat-sheet/" target="_blank">JavaScript Cheat Sheet</a></li>
<li><a href="http://wps.aw.com/wps/media/objects/2234/2287950/javascript_refererence.pdf" target="_blank">Addison-Wesley’s JavaScript Reference Card</a></li>
</ul>
<h2>jQuery Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="8" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/85.png" alt="" width="450" height="109" /></p>
<ul>
<li><a href="http://colorcharge.com/jquery/" target="_blank">jQuery Cheatsheet</a></li>
<li><a href="http://woork.blogspot.com/2009/09/jquery-visual-cheat-sheet.html" target="_blank">jQuery 1.3 Visual Cheat Sheet by Antonio Lupetti</a></li>
<li><a href="http://refcardz.dzone.com/refcardz/jquery-selectors" target="_blank">jQuery Selectors by Bear Bibeault &amp; Yehuda Katz</a></li>
</ul>
<h2>Unicode Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="9" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/97.png" alt="" width="450" height="112" /></p>
<ul>
<li><a href="http://www.utf.ru/" target="_blank">The Unicode Character Code</a></li>
<li><a href="http://www.visibone.com/htmlref/char/cer.htm" target="_blank">HTML Characters, Numeric Codes, 0-65535 by Bob Stein</a></li>
</ul>
<h2>XML Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="10" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/106.png" alt="" width="450" height="111" /></p>
<ul>
<li><a href="http://www.xml.su/" target="_blank">XML in one page</a></li>
<li><a href="http://www.mulberrytech.com/quickref/XMLquickref.pdf" target="_blank">XML 1.0 Syntax Quick Reference by Mulberry Technologies</a></li>
</ul>
<h2>mod_rewrite and .htaccess Cheat Sheets</h2>
<p><img decoding="async" loading="lazy" title="11" src="http://www.topdesignmag.com/wp-content/uploads/2011/01/1111.png" alt="" width="455" height="95" /></p>
<ul>
<li><a href="http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/" target="_blank">mod_rewrite Cheat Sheet by Dave Child</a></li>
<li><a href="http://www.thejackol.com/htaccess-cheatsheet/" target="_blank">htaccess Cheatsheet</a></li>
</ul>
<p>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/9666.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2013/05/Render-Process-150x150.jpg" alt="浏览器的渲染原理简介" width="150" height="150" /></a><a href="https://coolshell.cn/articles/9666.html" class="wp_rp_title">浏览器的渲染原理简介</a></li><li ><a href="https://coolshell.cn/articles/3013.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2010/09/biolab-150x150.jpg" alt="一些非常有意思的杂项资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3013.html" class="wp_rp_title">一些非常有意思的杂项资源</a></li><li ><a href="https://coolshell.cn/articles/1949.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/16.jpg" alt="Web中的省略号" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1949.html" class="wp_rp_title">Web中的省略号</a></li><li ><a href="https://coolshell.cn/articles/17634.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2017/01/pretty-code-150x150.gif" alt="Chrome开发者工具的小技巧" width="150" height="150" /></a><a href="https://coolshell.cn/articles/17634.html" class="wp_rp_title">Chrome开发者工具的小技巧</a></li><li ><a href="https://coolshell.cn/articles/6840.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/03/css-layouts-150x150.gif" alt="CSS 布局:40个教程、技巧、例子和最佳实践" width="150" height="150" /></a><a href="https://coolshell.cn/articles/6840.html" class="wp_rp_title">CSS 布局:40个教程、技巧、例子和最佳实践</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/3684.html">Web开发人员速查卡</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/3684.html/feed</wfw:commentRss>
			<slash:comments>22</slash:comments>
		
		
			</item>
		<item>
		<title>九个PHP很有用的功能</title>
		<link>https://coolshell.cn/articles/2394.html</link>
					<comments>https://coolshell.cn/articles/2394.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Thu, 06 May 2010 00:37:49 +0000</pubDate>
				<category><![CDATA[PHP脚本]]></category>
		<category><![CDATA[编程语言]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=2394</guid>

					<description><![CDATA[<p>下面是九个PHP中很有用的功能，不知道你用过了吗？ 1. 函数的任意数目的参数 你可能知道PHP允许你定义一个默认参数的函数。但你可能并不知道PHP还允许你定义...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/2394.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/2394.html">九个PHP很有用的功能</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>下面是九个PHP中很有用的功能，不知道你用过了吗？</p>
<h4><span>1. 函数的任意数目的参数</span></h4>
<p>你可能知道PHP允许你定义一个默认参数的函数。但你可能并不知道PHP还允许你定义一个完全任意的参数的函数</p>
<p>下面是一个示例向你展示了默认参数的函数：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 两个默认参数的函数
function foo($arg1 = &#039;&#039;, $arg2 = &#039;&#039;) {

	echo &quot;arg1: $arg1\n&quot;;
	echo &quot;arg2: $arg2\n&quot;;

}

foo(&#039;hello&#039;,&#039;world&#039;);
/* 输出:
arg1: hello
arg2: world
*/

foo();
/* 输出:
arg1:
arg2:
*/
</pre>
<p>现在我们来看一看一个不定参数的函数，其使用到了?<a href="http://us2.php.net/manual/en/function.func-get-args.php">func_get_args()</a>方法：<br />
<span id="more-2394"></span></p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 是的，形参列表为空
function foo() {

	// 取得所有的传入参数的数组
	$args = func_get_args();

	foreach ($args as $k =&gt; $v) {
		echo &quot;arg&quot;.($k+1).&quot;: $v\n&quot;;
	}

}

foo();
/* 什么也不会输出 */

foo(&#039;hello&#039;);
/* 输出
arg1: hello
*/

foo(&#039;hello&#039;, &#039;world&#039;, &#039;again&#039;);
/* 输出
arg1: hello
arg2: world
arg3: again
*/
</pre>
<h4><span>2. </span>使用 Glob() 查找文件</h4>
<p>很多PHP的函数都有一个比较长的自解释的函数名，但是，当你看到?<a href="http://us.php.net/manual/en/function.glob.php">glob()</a> 的时候，你可能并不知道这个函数是用来干什么的，除非你对它已经很熟悉了。</p>
<p>你可以认为这个函数就好?<a href="http://php.net/manual/en/function.scandir.php">scandir()</a> 一样，其可以用来查找文件。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 取得所有的后缀为PHP的文件
$files = glob(&#039;*.php&#039;);

print_r($files);
/* 输出:
Array
(
    [0] =&gt; phptest.php
    [1] =&gt; pi.php
    [2] =&gt; post_output.php
    [3] =&gt; test.php
)
*/
</pre>
<p>你还可以查找多种后缀名</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 取PHP文件和TXT文件
$files = glob(&#039;*.{php,txt}&#039;, GLOB_BRACE);

print_r($files);
/* 输出:
Array
(
    [0] =&gt; phptest.php
    [1] =&gt; pi.php
    [2] =&gt; post_output.php
    [3] =&gt; test.php
    [4] =&gt; log.txt
    [5] =&gt; test.txt
)
*/
</pre>
<p>你还可以加上路径：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
$files = glob(&#039;../images/a*.jpg&#039;);

print_r($files);
/* 输出:
Array
(
    [0] =&gt; ../images/apple.jpg
    [1] =&gt; ../images/art.jpg
)
*/
</pre>
<p>如果你想得到绝对路径，你可以调用?<a href="http://php.net/manual/en/function.realpath.php">realpath()</a> 函数：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
$files = glob(&#039;../images/a*.jpg&#039;);

// applies the function to each array element
$files = array_map(&#039;realpath&#039;,$files);

print_r($files);
/* output looks like:
Array
(
    [0] =&gt; C:\wamp\www\images\apple.jpg
    [1] =&gt; C:\wamp\www\images\art.jpg
)
*/
</pre>
<h4><span>3. </span>内存使用信息</h4>
<p>观察你程序的内存使用能够让你更好的优化你的代码。</p>
<p>PHP 是有垃圾回收机制的，而且有一套很复杂的内存管理机制。你可以知道你的脚本所使用的内存情况。要知道当前内存使用情况，你可以使用?<a href="http://us2.php.net/manual/en/function.memory-get-usage.php">memory_get_usage()</a> 函数，如果你想知道使用内存的峰值，你可以调用<a href="http://us2.php.net/manual/en/function.memory-get-peak-usage.php">memory_get_peak_usage()</a> 函数。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
echo &quot;Initial: &quot;.memory_get_usage().&quot; bytes \n&quot;;
/* 输出
Initial: 361400 bytes
*/

// 使用内存
for ($i = 0; $i &lt; 100000; $i++) {
	$array []= md5($i);
}

// 删除一半的内存
for ($i = 0; $i &lt; 100000; $i++) {
	unset($array[$i]);
}

echo &quot;Final: &quot;.memory_get_usage().&quot; bytes \n&quot;;
/* prints
Final: 885912 bytes
*/

echo &quot;Peak: &quot;.memory_get_peak_usage().&quot; bytes \n&quot;;
/* 输出峰值
Peak: 13687072 bytes
*/
</pre>
<h4><span>4. </span>CPU使用信息</h4>
<p>使用?<a href="http://us2.php.net/manual/en/function.getrusage.php">getrusage()</a> 函数可以让你知道CPU的使用情况。注意，这个功能在Windows下不可用。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
print_r(getrusage());
/* 输出
Array
(
    [ru_oublock] =&gt; 0
    [ru_inblock] =&gt; 0
    [ru_msgsnd] =&gt; 2
    [ru_msgrcv] =&gt; 3
    [ru_maxrss] =&gt; 12692
    [ru_ixrss] =&gt; 764
    [ru_idrss] =&gt; 3864
    [ru_minflt] =&gt; 94
    [ru_majflt] =&gt; 0
    [ru_nsignals] =&gt; 1
    [ru_nvcsw] =&gt; 67
    [ru_nivcsw] =&gt; 4
    [ru_nswap] =&gt; 0
    [ru_utime.tv_usec] =&gt; 0
    [ru_utime.tv_sec] =&gt; 0
    [ru_stime.tv_usec] =&gt; 6269
    [ru_stime.tv_sec] =&gt; 0
)

*/
</pre>
<p>这个结构看上出很晦涩，除非你对CPU很了解。下面一些解释：</p>
<ul>
<li>ru_oublock: 块输出操作</li>
<li>ru_inblock: 块输入操作</li>
<li>ru_msgsnd: 发送的message</li>
<li>ru_msgrcv: 收到的message</li>
<li>ru_maxrss: 最大驻留集大小</li>
<li>ru_ixrss: 全部共享内存大小</li>
<li>ru_idrss:全部非共享内存大小</li>
<li>ru_minflt: 页回收</li>
<li>ru_majflt: 页失效</li>
<li>ru_nsignals: 收到的信号</li>
<li>ru_nvcsw: 主动上下文切换</li>
<li>ru_nivcsw: 被动上下文切换</li>
<li>ru_nswap: 交换区</li>
<li>ru_utime.tv_usec: 用户态时间 (microseconds)</li>
<li>ru_utime.tv_sec: 用户态时间(seconds)</li>
<li>ru_stime.tv_usec: 系统内核时间 (microseconds)</li>
<li>ru_stime.tv_sec: 系统内核时间?(seconds)</li>
</ul>
<p>要看到你的脚本消耗了多少CPU，我们需要看看“用户态的时间”和“系统内核时间”的值。秒和微秒部分是分别提供的，您可以把微秒值除以100万，并把它添加到秒的值后，可以得到有小数部分的秒数。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// sleep for 3 seconds (non-busy)
sleep(3);

$data = getrusage();
echo &quot;User time: &quot;.
	($data[&#039;ru_utime.tv_sec&#039;] +
	$data[&#039;ru_utime.tv_usec&#039;] / 1000000);
echo &quot;System time: &quot;.
	($data[&#039;ru_stime.tv_sec&#039;] +
	$data[&#039;ru_stime.tv_usec&#039;] / 1000000);

/* 输出
User time: 0.011552
System time: 0
*/
</pre>
<p>sleep是不占用系统时间的，我们可以来看下面的一个例子：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// loop 10 million times (busy)
for($i=0;$i&lt;10000000;$i++) {

}

$data = getrusage();
echo &quot;User time: &quot;.
	($data[&#039;ru_utime.tv_sec&#039;] +
	$data[&#039;ru_utime.tv_usec&#039;] / 1000000);
echo &quot;System time: &quot;.
	($data[&#039;ru_stime.tv_sec&#039;] +
	$data[&#039;ru_stime.tv_usec&#039;] / 1000000);

/* 输出
User time: 1.424592
System time: 0.004204
*/
</pre>
<p>这花了大约14秒的CPU时间，几乎所有的都是用户的时间，因为没有系统调用。</p>
<p>系统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) - $start &lt; 3) {

}

$data = getrusage();
echo &quot;User time: &quot;.
	($data[&#039;ru_utime.tv_sec&#039;] +
	$data[&#039;ru_utime.tv_usec&#039;] / 1000000);
echo &quot;System time: &quot;.
	($data[&#039;ru_stime.tv_sec&#039;] +
	$data[&#039;ru_stime.tv_usec&#039;] / 1000000);

/* prints
User time: 1.088171
System time: 1.675315
*/
</pre>
<p>我们可以看到上面这个例子更耗CPU。</p>
<h4><span>5. </span>系统常量</h4>
<p>PHP 提供非常有用的<a href="http://php.net/manual/en/language.constants.predefined.php">系统常量</a> 可以让你得到当前的行号 (__LINE__)，文件 (__FILE__)，目录 (__DIR__)，函数名 (__FUNCTION__)，类名(__CLASS__)，方法名(__METHOD__) 和名字空间 (__NAMESPACE__)，很像C语言。</p>
<p>我们可以以为这些东西主要是用于调试，当也不一定，比如我们可以在include其它文件的时候使用?__FILE__ (当然，你也可以在 PHP 5.3以后使用 __DIR__ )，下面是一个例子。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// this is relative to the loaded script&#039;s path
// it may cause problems when running scripts from different directories
require_once(&#039;config/database.php&#039;);

// this is always relative to this file&#039;s path
// no matter where it was included from
require_once(dirname(__FILE__) . &#039;/config/database.php&#039;);
</pre>
<p>下面是使用 __LINE__ 来输出一些debug的信息，这样有助于你调试程序：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// some code
// ...
my_debug(&quot;some debug message&quot;, __LINE__);
/* 输出
Line 4: some debug message
*/

// some more code
// ...
my_debug(&quot;another debug message&quot;, __LINE__);
/* 输出
Line 11: another debug message
*/

function my_debug($msg, $line) {
	echo &quot;Line $line: $msg\n&quot;;
}
</pre>
<h4><span>6.生成唯一的ID</span></h4>
<p>有很多人使用 md5() 来生成一个唯一的ID，如下所示：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// generate unique string
echo md5(time() . mt_rand(1,1000000));
</pre>
<p>其实，PHP中有一个叫?<a href="http://us2.php.net/manual/en/function.uniqid.php">uniqid()</a> 的函数是专门用来干这个的：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// generate unique string
echo uniqid();
/* 输出
4bd67c947233e
*/

// generate another unique string
echo uniqid();
/* 输出
4bd67c9472340
*/
</pre>
<p>可能你会注意到生成出来的ID前几位是一样的，这是因为生成器依赖于系统的时间，这其实是一个非常不错的功能，因为你是很容易为你的这些ID排序的。这点MD5是做不到的。</p>
<p>你还可以加上前缀避免重名：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 前缀
echo uniqid(&#039;foo_&#039;);
/* 输出
foo_4bd67d6cd8b8f
*/

// 有更多的熵
echo uniqid(&#039;&#039;,true);
/* 输出
4bd67d6cd8b926.12135106
*/

// 都有
echo uniqid(&#039;bar_&#039;,true);
/* 输出
bar_4bd67da367b650.43684647
*/
</pre>
<p>而且，生成出来的ID会比MD5生成的要短，这会让你节省很多空间。</p>
<h4><span>7. </span>序列化</h4>
<p>你是否会把一个比较复杂的数据结构存到数据库或是文件中？你并不需要自己去写自己的算法。PHP早已为你做好了，其提供了两个函数：?<a href="http://php.net/manual/en/function.serialize.php">serialize()</a> 和 <a href="http://www.php.net/manual/en/function.unserialize.php">unserialize()</a>:</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 一个复杂的数组
$myvar = array(
	&#039;hello&#039;,
	42,
	array(1,&#039;two&#039;),
	&#039;apple&#039;
);

// 序列化
$string = serialize($myvar);

echo $string;
/* 输出
a:4:{i:0;s:5:&quot;hello&quot;;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:&quot;two&quot;;}i:3;s:5:&quot;apple&quot;;}
*/

// 反序例化
$newvar = unserialize($string);

print_r($newvar);
/* 输出
Array
(
    [0] =&gt; hello
    [1] =&gt; 42
    [2] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; two
        )

    [3] =&gt; apple
)
*/
</pre>
<p>这是PHP的原生函数，然而在今天JSON越来越流行，所以在PHP5.2以后，PHP开始支持JSON，你可以使用 json_encode() 和 json_decode() 函数</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// a complex array
$myvar = array(
	&#039;hello&#039;,
	42,
	array(1,&#039;two&#039;),
	&#039;apple&#039;
);

// convert to a string
$string = json_encode($myvar);

echo $string;
/* prints
[&quot;hello&quot;,42,[1,&quot;two&quot;],&quot;apple&quot;]
*/

// you can reproduce the original variable
$newvar = json_decode($string);

print_r($newvar);
/* prints
Array
(
    [0] =&gt; hello
    [1] =&gt; 42
    [2] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; two
        )

    [3] =&gt; apple
)
*/
</pre>
<p>这看起来更为紧凑一些了，而且还兼容于Javascript和其它语言。但是对于一些非常复杂的数据结构，可能会造成数据丢失。</p>
<h4><span>8. </span>字符串压缩</h4>
<p>当我们说到压缩，我们可能会想到文件压缩，其实，字符串也是可以压缩的。PHP提供了?<a href="http://php.net/manual/en/function.gzcompress.php">gzcompress()</a> 和 <a href="http://www.php.net/manual/en/function.gzuncompress.php">gzuncompress()</a> 函数：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
$string =
&quot;Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,
sapien vel feugiat vestibulum, nulla dui pretium orci,
non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
mollis congue. Praesent ipsum diam, consectetur vitae
ornare a, aliquam a nunc. In id magna pellentesque
tellus posuere adipiscing. Sed non mi metus, at lacinia
augue. Sed magna nisi, ornare in mollis in, mollis
sed nunc. Etiam at justo in leo congue mollis.
Nullam in neque eget metus hendrerit scelerisque
eu non enim. Ut malesuada lacus eu nulla bibendum
id euismod urna sodales. &quot;;

$compressed = gzcompress($string);

echo &quot;Original size: &quot;. strlen($string).&quot;\n&quot;;
/* 输出原始大小
Original size: 800
*/

echo &quot;Compressed size: &quot;. strlen($compressed).&quot;\n&quot;;
/* 输出压缩后的大小
Compressed size: 418
*/

// 解压缩
$original = gzuncompress($compressed);
</pre>
<p>几乎有<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">50% 压缩比率。同时，你还可以使用?<a href="http://www.php.net/manual/en/function.gzencode.php">gzencode()</a> 和 <a href="http://www.php.net/manual/en/function.gzdecode.php">gzdecode()</a> 函数来压缩，只不用其用了不同的压缩算法。</span></p>
<h4><span>9. 注册停止</span>函数</h4>
<p>有一个函数叫做?<a href="http://www.php.net/manual/en/function.register-shutdown-function.php">register_shutdown_function()</a>，可以让你在整个脚本停时前运行代码。让我们看下面的一个示例：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// capture the start time
$start_time = microtime(true);

// do some stuff
// ...

// display how long the script took
echo &quot;execution took: &quot;.
		(microtime(true) - $start_time).
		&quot; seconds.&quot;;
</pre>
<p>上面这个示例只不过是用来计算某个函数运行的时间。然后，如果你在函数中间调用?<a href="http://php.net/manual/en/function.exit.php">exit()</a> 函数，那么你的最后的代码将不会被运行到。并且，如果该脚本在浏览器终止（用户按停止按钮），其也无法被运行。</p>
<p>而当我们使用了register_shutdown_function()后，你的程序就算是在脚本被停止后也会被运行：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
$start_time = microtime(true);

register_shutdown_function(&#039;my_shutdown&#039;);

// do some stuff
// ...

function my_shutdown() {
	global $start_time;

	echo &quot;execution took: &quot;.
			(microtime(true) - $start_time).
			&quot; seconds.&quot;;
}
</pre>
<p>文章：<a href="http://net.tutsplus.com/tutorials/php/9-useful-php-functions-and-features-you-need-to-know/" target="_blank">来源</a><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/09/image008-150x150.jpg" alt="一些文章和各种资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_title">一些文章和各种资源</a></li><li ><a href="https://coolshell.cn/articles/5160.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/08/Pagination-e1312791884744-150x150.jpg" alt="PHP分页技术的代码和示例" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5160.html" class="wp_rp_title">PHP分页技术的代码和示例</a></li><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/20.jpg" alt="最为奇怪的程序语言的特性" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_title">最为奇怪的程序语言的特性</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/2394.html">九个PHP很有用的功能</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/2394.html/feed</wfw:commentRss>
			<slash:comments>26</slash:comments>
		
		
			</item>
		<item>
		<title>最为奇怪的程序语言的特性</title>
		<link>https://coolshell.cn/articles/2053.html</link>
					<comments>https://coolshell.cn/articles/2053.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Thu, 21 Jan 2010 00:16:18 +0000</pubDate>
				<category><![CDATA[编程语言]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=2053</guid>

					<description><![CDATA[<p>这些最为奇怪的程序语言的特性，来自stackoverflow.com，原贴在这里。我摘选了一些例子，的确是比较怪异，让我们一个一个来看看。  1、C语言中的数组...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/2053.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/2053.html">最为奇怪的程序语言的特性</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>这些最为奇怪的程序语言的特性，来自stackoverflow.com，原贴在<a href="http://stackoverflow.com/questions/1995113?sort=votes&amp;page=1" target="_blank">这里</a>。我摘选了一些例子，的确是比较怪异，让我们一个一个来看看。 </p>
<p><strong>1、C语言中的数组</strong> </p>
<p style="padding-left: 30px;">在C/C++中，a[10] 可以写成 10[a] </p>
<p style="padding-left: 30px;">&#8220;Hello World&#8221;[i] 也可以写成 i[&#8220;Hello World&#8221;] </p>
<p>这样的特性是不是很怪异？如果你想知道为什么的话，你可以看看本站的这篇文章——《<a href="https://coolshell.cn/articles/945.html" target="_blank">C语言的谜题</a>》中的第12题。 </p>
<p><strong>2、在Javascript中</strong> </p>
<p> &#8216;5&#8217; + 3 的结果是：&#8217;53&#8217;<br />
 &#8216;5&#8217; &#8211; 3 的结果是：2 </p>
<p><strong>3、C/C++中的Trigraphs</strong> </p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">int main() {
   cout &lt;&lt; &quot;LOL??!&quot;;
}</pre>
<p>上面的这段程序会输出： “LOL|”，这是因为 ??! 被转成了 | ，关于Trigraphs，下面有个表格： </p>
<p><span id="more-2053"></span></p>
<table style="width: 237px; height: 184px;">
<tbody>
<tr valign="top">
<td>??=</td>
<td>#</td>
</tr>
<tr valign="top">
<td>??(</td>
<td>[</td>
</tr>
<tr valign="top">
<td>??/</td>
<td>\</td>
</tr>
<tr valign="top">
<td>??)</td>
<td>]</td>
</tr>
<tr valign="top">
<td>??’</td>
<td>^</td>
</tr>
<tr valign="top">
<td>??&lt;</td>
<td>{</td>
</tr>
<tr valign="top">
<td>??!</td>
<td>|</td>
</tr>
<tr valign="top">
<td>??&gt;</td>
<td>}</td>
</tr>
<tr valign="top">
<td>??-</td>
<td>~</td>
</tr>
</tbody>
</table>
<p>  </p>
<p><strong>4、JavaScript 的条件表</strong> </p>
<p>看到下面这个表，不难理解为什么Javascript程序员为什么痛苦了——《<a rel="bookmark" href="https://coolshell.cn/articles/1850.html">Javascript程序员嘴最脏??</a>》 </p>
<p>[javascript]&#8221;        ==   &#8216;0&#8217;           //false<br />
0         ==   &#8221;            //true<br />
0         ==   &#8216;0&#8217;           //true<br />
false     ==   &#8216;false&#8217;       //false<br />
false     ==   &#8216;0&#8217;           //true<br />
false     ==   undefined     //false<br />
false     ==   null          //false<br />
null      ==   undefined     //true<br />
&quot; \t\r\n&quot; ==   0             //true[/javascript] </p>
<p><strong>5、Java的Integer cache</strong> </p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">Integer foo = 1000;
Integer bar = 1000;
 
foo &lt;= bar; // true
foo &gt;= bar; // true
foo == bar; // false
 
//然后，如果你的 foo 和 bar 的值在 127 和 -128 之间（包括）
//那么，其行为则改变了：
 
Integer foo = 42;
Integer bar = 42;
 
foo &lt;= bar; // true
foo &gt;= bar; // true
foo == bar; // true</pre>
<p>为什么会这样呢？你需要了解一下Java Interger Cache，下面是相关的程序，注意其中的注释</p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">/**
     * Returns a &lt;tt&gt;Integer&lt;/tt&gt; instance representing the specified
     * &lt;tt&gt;int&lt;/tt&gt; value.
     * If a new &lt;tt&gt;Integer&lt;/tt&gt; instance is not required, this method
     * should generally be used in preference to the constructor
     * &lt;a href=&quot;mailto:{@link&quot;&gt;{@link&lt;/a&gt; #Integer(int)}, as this method is likely to yield
     * significantly better space and time performance by caching
     * frequently requested values.
     *
     * @param  i an &lt;code&gt;int&lt;/code&gt; value.
     * @return a &lt;tt&gt;Integer&lt;/tt&gt; instance representing &lt;tt&gt;i&lt;/tt&gt;.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if(i &gt;= -128 &amp;&amp; i &lt;= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }
</pre>
<p><strong>5、Perl的那些奇怪的变量</strong></p>
<p>[perl]$.<br />
$_<br />
$_#<br />
$$<br />
$[<br />
@_[/perl]</p>
<p>其所有的这些怪异的变量请参看：<a href="http://www.kichwa.com/quik_ref/spec_variables.html">http://www.kichwa.com/quik_ref/spec_variables.html</a> </p>
<p><strong>6、Java的异常返回</strong></p>
<p>请看下面这段程序，你觉得其返回true还是false？</p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">try {
    return true;
} finally {
    return false;
}</pre>
<p>在 javascript 和python下，其行为和Java的是一样的。 </p>
<p><strong>7、C语言中的Duff device</strong></p>
<p>下面的这段程序你能看得懂吗？这就是所谓的Duff Device，相当的怪异。</p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">void duff_memcpy( char* to, char* from, size_t count ) {
    size_t n = (count+7)/8;
    switch( count%8 ) {
    case 0: do{ *to++ = *from++;
    case 7:     *to++ = *from++;
    case 6:     *to++ = *from++;
    case 5:     *to++ = *from++;
    case 4:     *to++ = *from++;
    case 3:     *to++ = *from++;
    case 2:     *to++ = *from++;
    case 1:     *to++ = *from++;
            }while(--n&gt;0);
    }
} </pre>
<p><strong>8、PHP中的字符串当函数用</strong></p>
<p>PHP中的某些用法也是很怪异的</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">$x = &quot;foo&quot;;
function foo(){ echo &quot;wtf&quot;; }
$x();</pre>
<p><strong>9、在C++中，你可以使用空指针调用静态函数</strong></p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">class Foo {
  public:
    static void bar() {
      std::cout &lt;&lt; &quot;bar()&quot; &lt;&lt; std::endl;
    }
};
 
int main(void) {
  Foo * foo = NULL;
  foo-&gt;bar(); //=&gt; WTF!?
  return 0; // Ok!
}</pre>
<p>呵呵。的确是挺怪异的。<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/1850.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/programming_language-150x150.jpg" alt="Javascript程序员嘴最脏??" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1850.html" class="wp_rp_title">Javascript程序员嘴最脏??</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/1992.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys-150x150.jpg" alt="程序员眼中的编程语言" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1992.html" class="wp_rp_title">程序员眼中的编程语言</a></li><li ><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/oscar-meyer-wienermobile-150x150.jpg" alt="编程语言汽车" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_title">编程语言汽车</a></li><li ><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="到处都是Unix的胎记" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_title">到处都是Unix的胎记</a></li><li ><a href="https://coolshell.cn/articles/20845.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2020/03/rust-social-wide-150x150.jpg" alt="Rust语言的编程范式" width="150" height="150" /></a><a href="https://coolshell.cn/articles/20845.html" class="wp_rp_title">Rust语言的编程范式</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/2053.html">最为奇怪的程序语言的特性</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/2053.html/feed</wfw:commentRss>
			<slash:comments>39</slash:comments>
		
		
			</item>
		<item>
		<title>程序员眼中的编程语言</title>
		<link>https://coolshell.cn/articles/1992.html</link>
					<comments>https://coolshell.cn/articles/1992.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Thu, 24 Dec 2009 06:31:25 +0000</pubDate>
				<category><![CDATA[编程语言]]></category>
		<category><![CDATA[轶事趣闻]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[programming language]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[程序员]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1992</guid>

					<description><![CDATA[<p>下图是一个搞笑的图片——程序员眼中的编程语言。 图片的横轴是编程语言。 纵轴是各语言的程序员、粉丝、信徒。 中间的各个小图片则是，粉丝眼中的编程语言的形象。 比...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1992.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1992.html">程序员眼中的编程语言</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>下图是一个搞笑的图片——程序员眼中的编程语言。</p>
<ul>
<li>图片的横轴是编程语言。</li>
<li>纵轴是各语言的程序员、粉丝、信徒。</li>
<li>中间的各个小图片则是，粉丝眼中的编程语言的形象。</li>
</ul>
<p>比如说，</p>
<ul>
<li>第一行第一列，是Java程序员看Java语言的样子，一幢现代化的大厦。</li>
<li>第一行第二列，是Java程序员看C语言，一个年老过时的骨灰级老头。</li>
<li>当然，C程序员看Java语言也比较搞，见第二行第一列。呵呵。</li>
</ul>
<p>其它的大家自己看吧。还有另外一个关于操作系统的《<a rel="bookmark" href="https://coolshell.cn/articles/1998.html">粉丝眼中的操作系统</a>》</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-1994" title="程序员眼中的编程语言" src="https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys.jpg" alt="程序员眼中的编程语言" width="575" height="420" srcset="https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys.jpg 575w, https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys-300x219.jpg 300w, https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys-370x270.jpg 370w" sizes="(max-width: 575px) 100vw, 575px" /></p>
<p> </p>
<p><a href="https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys.jpg"></a><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/8088.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="对技术的态度" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8088.html" class="wp_rp_title">对技术的态度</a></li><li ><a href="https://coolshell.cn/articles/4990.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/07/programmer-150x150.png" alt="程序员技术练级攻略" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4990.html" class="wp_rp_title">程序员技术练级攻略</a></li><li ><a href="https://coolshell.cn/articles/4758.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/30.jpg" alt="如何写出无法维护的代码" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4758.html" class="wp_rp_title">如何写出无法维护的代码</a></li><li ><a href="https://coolshell.cn/articles/4102.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/25.jpg" alt="如何学好C语言" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4102.html" class="wp_rp_title">如何学好C语言</a></li><li ><a href="https://coolshell.cn/articles/1391.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/18.jpg" alt="编程真难啊" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1391.html" class="wp_rp_title">编程真难啊</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1992.html">程序员眼中的编程语言</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1992.html/feed</wfw:commentRss>
			<slash:comments>33</slash:comments>
		
		
			</item>
		<item>
		<title>SQL的Where语句</title>
		<link>https://coolshell.cn/articles/1889.html</link>
					<comments>https://coolshell.cn/articles/1889.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Tue, 01 Dec 2009 05:48:25 +0000</pubDate>
				<category><![CDATA[编程语言]]></category>
		<category><![CDATA[轶事趣闻]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1889</guid>

					<description><![CDATA[<p>某DBA在查看自己的数库日志的时候，看到了数据库服务器上出现了很多很怪异的SQL的Where条件语句，是下面这个样子：（所有的where语句前都有了一个叫“1=...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1889.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1889.html">SQL的Where语句</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>某DBA在查看自己的数库日志的时候，看到了数据库服务器上出现了很多很怪异的SQL的Where条件语句，是下面这个样子：（所有的where语句前都有了一个叫“1=1”的子条件）呵呵。</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-1890" title="SQL Where Clause" src="https://coolshell.cn/wp-content/uploads/2009/12/sql.where_.clause.jpg" alt="SQL Where Clause" width="460" height="631" srcset="https://coolshell.cn/wp-content/uploads/2009/12/sql.where_.clause.jpg 460w, https://coolshell.cn/wp-content/uploads/2009/12/sql.where_.clause-219x300.jpg 219w, https://coolshell.cn/wp-content/uploads/2009/12/sql.where_.clause-197x270.jpg 197w" sizes="(max-width: 460px) 100vw, 460px" /></p>
<p>要理解这个事情的原因其实并不难。只要你是一个编写数据库的程序员，你就会知道——动态生成where后的条件的“麻烦”，那就是条件的“分隔”-and或or。下面听我慢慢说来。</p>
<p><span id="more-1889"></span></p>
<p>我们知道，大多数的查询表单都需要用户输出一堆查询条件，然后我们的程序在后台要把这些子条件用and组合起来。于是，把and加在各个条件的中间就成为了一件“难事”，因为你的程序需要判断：</p>
<ul>
<li>如果没有条件的话，则不需要where</li>
<li>如果只有一个条件的话，不需要and.</li>
<li>如果有多个条件的话，
<ul>
<li>如果and是持续加在每个条件后面的话，那么就要判断是否是最后一个条件，因为最后一个不能加and.</li>
<li>同样，如果and是要加在每个条件前面的话，你就需要判断是否是第一个，如果是，那就不加。</li>
</ul>
</li>
</ul>
<p>真是TMD太烦了，所以，编程“大拿”引入了“1=1”条件语句。于是，编程的难度大幅度下降，你可以用单一的方式来处理这若干的查询子条件了。而1=1应该会被数据库引擎优化时给去掉了。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
&lt;pre&gt;&lt;code&gt;$query = &quot;SELECT name FROM table WHERE 1=1 &quot;;

foreach($clauses as $key =&gt; $value){
    $query .= &quot; AND &quot;.escape($key).&quot;=&quot;.escape($value).&quot; &quot;;
}
&lt;/code&gt;&lt;/pre&gt;
</pre>
<p>呵呵，<strong>DBA怎么能够理解我们疯狂程序员的用心良苦啊</strong>。</p>
<p>另外，在这里说一下，这样的做法看似很愚蠢，但很有效，在PHP的世界中，也有人使用下面这样的做法——使用了PHP的implode函数。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
&lt;pre&gt;&lt;code&gt;/**
 * @param string $base base of query, e.g. UPDATE table SET
 * @param string $logic logic for concatenating $assoc, e.g. AND
 * @param array $assoc associative array of `field`=&gt;&#039;value&#039; pairs to concatenate and append to $base
 * @param string $suffix additional clauses, e.g. LIMIT 0,30
 * @return string
 */
function construct_sql($base, $logic, $clauses, $suffix=&#039;&#039;)
{
    // initialise array to avoid warnings/notices on some PHP installations
    $queries = array();

    // create array of strings to be glued together by logic
    foreach($clauses as $key =&gt; $value)
        $queries[] = &quot;`&quot; . escape($key) . &quot;`=&#039;&quot; . escape($value) . &quot;&#039;&quot;;

    // add a space in case $base doesn&#039;t have a space at the end and glue clauses together
    $query .= &quot; &quot; . implode(&quot; $logic &quot;,$queries) . &quot; &quot; . $suffix . &quot;;&quot;;

    return $query;
}

/**
 * @param string $str string to escape for intended use
 * @return string
 */
function escape($str)
{
    return mysql_real_escape_string($str);
}
&lt;/code&gt;&lt;/pre&gt;
</pre>
<p>于是，我们可以这样使用：（<code>为什么我们要在update语句后加上“Limit 1”呢？这个关系到性能问题，关于这方面的话题，你可以查看本站的《<a title="MySQL性能优化的最佳20+条经验" href="https://coolshell.cn/articles/1846.html">MySQL性能优化的最佳20+条经验</a>》</code>）</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
&lt;pre&gt;&lt;code&gt;$updates = array(
    &#039;field1&#039;=&gt;&#039;val1&#039;
    &#039;field2&#039;=&gt;&#039;val2&#039;
);
$wheres = array(
    &#039;field1&#039;=&gt;&#039;cond1&#039;,
    &#039;field2&#039;=&gt;&#039;cond2&#039;
);
echo construct_sql(construct_sql(&quot;UPDATE table SET&quot;, &quot;, &quot;, $updates) . &quot; WHERE &quot;, &quot; AND &quot;, $wheres),&quot;LIMIT 1&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
</pre>
<p><code>下面是输出结果：</code></p>
<p><code data-enlighter-language="php" class="EnlighterJSRAW">UPDATE table SET `field1`=&#039;val1&#039;, `field2`=&#039;val2&#039; WHERE `field1`=&#039;cond1&#039; AND `field2`=&#039;cond2&#039; LIMIT 1;</code></p>
<p> </p>
<p><code>（全文完）</code><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/7270.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/05/overview2-1-150x150.png" alt="NoSQL 数据建模技术" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7270.html" class="wp_rp_title">NoSQL 数据建模技术</a></li><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/1846.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/unoptimized_explain-150x150.jpg" alt="MySQL性能优化的最佳20+条经验" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1846.html" class="wp_rp_title">MySQL性能优化的最佳20+条经验</a></li><li ><a href="https://coolshell.cn/articles/8711.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/12/200906020837401710-150x150.jpg" alt="程序员疫苗：代码注入" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8711.html" class="wp_rp_title">程序员疫苗：代码注入</a></li><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/7490.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/06/f1-150x150.jpg" alt="性能调优攻略" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7490.html" class="wp_rp_title">性能调优攻略</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1889.html">SQL的Where语句</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1889.html/feed</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
			</item>
		<item>
		<title>Javascript程序员嘴最脏??</title>
		<link>https://coolshell.cn/articles/1850.html</link>
					<comments>https://coolshell.cn/articles/1850.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Mon, 30 Nov 2009 00:16:54 +0000</pubDate>
				<category><![CDATA[编程语言]]></category>
		<category><![CDATA[轶事趣闻]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1850</guid>

					<description><![CDATA[<p>请看下图，我在Google Code上，针对每个程序语言都搜索了一下“fuck”一词的出现文件的个数X，以及没有出现fuck一词的文件的个数Y，然后放在Exce...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1850.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1850.html">Javascript程序员嘴最脏??</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>请看下图，我在Google Code上，针对每个程序语言都搜索了一下“fuck”一词的出现文件的个数X，以及没有出现fuck一词的文件的个数Y，然后放在Excel里求了一下百分比（X/(X+Y) * 100%），做了一个图。结果，JavaScript语言中出现的次数高达0.56%，名列全部语言之首，然后是Perl，C 和 PHP。（对于Javascript程序员的这种行为可以理解，因为IE，因为浏览器嘛，我就不多说了）</p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-1851" title="Google Code 中程序语言出现 fuck 一词的比率" src="https://coolshell.cn/wp-content/uploads/2009/11/programming_language.jpg" alt="Google Code 中程序语言出现 fuck 一词的比率" width="543" height="303" srcset="https://coolshell.cn/wp-content/uploads/2009/11/programming_language.jpg 543w, https://coolshell.cn/wp-content/uploads/2009/11/programming_language-300x167.jpg 300w, https://coolshell.cn/wp-content/uploads/2009/11/programming_language-360x200.jpg 360w, https://coolshell.cn/wp-content/uploads/2009/11/programming_language-484x270.jpg 484w" sizes="(max-width: 543px) 100vw, 543px" /></p>
<p style="text-align: left;">相关的数据表格如下：</p>
<p style="text-align: left;"><span id="more-1850"></span></p>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-1852" title="Google Code 中程序语言出现 fuck 一词的比率" src="https://coolshell.cn/wp-content/uploads/2009/11/programming_language_table.jpg" alt="Google Code 中程序语言出现 fuck 一词的比率" width="262" height="225" /></p>
<p style="text-align: left;">（全文完）</p>
<p><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/20.jpg" alt="最为奇怪的程序语言的特性" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_title">最为奇怪的程序语言的特性</a></li><li ><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/oscar-meyer-wienermobile-150x150.jpg" alt="编程语言汽车" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_title">编程语言汽车</a></li><li ><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="到处都是Unix的胎记" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_title">到处都是Unix的胎记</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/10739.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2013/12/lua-150x150.gif" alt="Lua简明教程" width="150" height="150" /></a><a href="https://coolshell.cn/articles/10739.html" class="wp_rp_title">Lua简明教程</a></li><li ><a href="https://coolshell.cn/articles/10337.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="数据即代码：元驱动编程" width="150" height="150" /></a><a href="https://coolshell.cn/articles/10337.html" class="wp_rp_title">数据即代码：元驱动编程</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1850.html">Javascript程序员嘴最脏??</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1850.html/feed</wfw:commentRss>
			<slash:comments>86</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL性能优化的最佳20+条经验</title>
		<link>https://coolshell.cn/articles/1846.html</link>
					<comments>https://coolshell.cn/articles/1846.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Fri, 27 Nov 2009 10:57:33 +0000</pubDate>
				<category><![CDATA[PHP脚本]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1846</guid>

					<description><![CDATA[<p>今天，数据库的操作越来越成为整个应用的性能瓶颈了，这点对于Web应用尤其明显。关于数据库的性能，这并不只是DBA才需要担心的事，而这更是我们程序员需要去关注的事...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1846.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1846.html">MySQL性能优化的最佳20+条经验</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>今天，数据库的操作越来越成为整个应用的性能瓶颈了，这点对于Web应用尤其明显。关于数据库的性能，这并不只是DBA才需要担心的事，而这更是我们程序员需要去关注的事情。当我们去设计数据库表结构，对操作数据库时（尤其是查表时的SQL语句），我们都需要注意数据操作的性能。这里，我们不会讲过多的SQL语句的优化，而只是针对MySQL这一Web应用最多的数据库。希望下面的这些优化技巧对你有用。</p>
<h4>1. 为查询缓存优化你的查询</h4>
<p>大多数的MySQL服务器都开启了查询缓存。这是提高性最有效的方法之一，而且这是被MySQL的数据库引擎处理的。当有很多相同的查询被执行了多次的时候，这些查询结果会被放到一个缓存中，这样，后续的相同的查询就不用操作表而直接访问缓存结果了。</p>
<p>这里最主要的问题是，对于程序员来说，这个事情是很容易被忽略的。因为，我们某些查询语句会让MySQL不使用缓存。请看下面的示例：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 查询缓存不开启
$r = mysql_query(&quot;SELECT username FROM user WHERE signup_date &gt;= CURDATE()&quot;);

// 开启查询缓存
$today = date(&quot;Y-m-d&quot;);
$r = mysql_query(&quot;SELECT username FROM user WHERE signup_date &gt;= &#039;$today&#039;&quot;);
</pre>
<p>上面两条SQL语句的差别就是 CURDATE() ，MySQL的查询缓存对这个函数不起作用。所以，像 NOW() 和 RAND() 或是其它的诸如此类的SQL函数都不会开启查询缓存，因为这些函数的返回是会不定的易变的。所以，你所需要的就是用一个变量来代替MySQL的函数，从而开启缓存。</p>
<p><span id="more-1846"></span></p>
<h4>2. EXPLAIN 你的 SELECT 查询</h4>
<p>使用 <a href="http://dev.mysql.com/doc/refman/5.0/en/explain.html" target="_blank">EXPLAIN</a> 关键字可以让你知道MySQL是如何处理你的SQL语句的。这可以帮你分析你的查询语句或是表结构的性能瓶颈。</p>
<p>EXPLAIN 的查询结果还会告诉你你的索引主键被如何利用的，你的数据表是如何被搜索和排序的……等等，等等。</p>
<p>挑一个你的SELECT语句（推荐挑选那个最复杂的，有多表联接的），把关键字EXPLAIN加到前面。你可以使用phpmyadmin来做这个事。然后，你会看到一张表格。下面的这个示例中，我们忘记加上了group_id索引，并且有表联接：</p>
<div class="tutorial_image"><img decoding="async" src="http://nettuts.s3.amazonaws.com/500_mysql/unoptimized_explain.jpg" border="0" alt="" /></div>
<p>当我们为 group_id 字段加上索引后：</p>
<div class="tutorial_image"><img decoding="async" src="http://nettuts.s3.amazonaws.com/500_mysql/optimized_explain.jpg" border="0" alt="" /></div>
<p>我们可以看到，前一个结果显示搜索了 7883 行，而后一个只是搜索了两个表的 9 和 16 行。查看rows列可以让我们找到潜在的性能问题。</p>
<h4>3. 当只要一行数据时使用 LIMIT 1</h4>
<p>当你查询表的有些时候，你已经知道结果只会有一条结果，但因为你可能需要去fetch游标，或是你也许会去检查返回的记录数。</p>
<p>在这种情况下，加上 LIMIT 1 可以增加性能。这样一样，MySQL数据库引擎会在找到一条数据后停止搜索，而不是继续往后查少下一条符合记录的数据。</p>
<p>下面的示例，只是为了找一下是否有“中国”的用户，很明显，后面的会比前面的更有效率。（请注意，第一条中是Select *，第二条是Select 1）</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">

// 没有效率的：
$r = mysql_query(&quot;SELECT * FROM user WHERE country = &#039;China&#039;&quot;);
if (mysql_num_rows($r) &gt; 0) {
	// ...
}

// 有效率的：
$r = mysql_query(&quot;SELECT 1 FROM user WHERE country = &#039;China&#039; LIMIT 1&quot;);
if (mysql_num_rows($r) &gt; 0) {
	// ...
}
</pre>
<h4>4. 为搜索字段建索引</h4>
<p>索引并不一定就是给主键或是唯一的字段。如果在你的表中，有某个字段你总要会经常用来做搜索，那么，请为其建立索引吧。</p>
<div class="tutorial_image"><img decoding="async" src="http://nettuts.s3.amazonaws.com/500_mysql/search_index.jpg" border="0" alt="" /></div>
<p>从上图你可以看到那个搜索字串 &#8220;last_name LIKE &#8216;a%'&#8221;，一个是建了索引，一个是没有索引，性能差了4倍左右。</p>
<p>另外，你应该也需要知道什么样的搜索是不能使用正常的索引的。例如，当你需要在一篇大的文章中搜索一个词时，如： &#8220;WHERE post_content LIKE &#8216;%apple%'&#8221;，索引可能是没有意义的。你可能需要使用<a href="http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html" target="_blank">MySQL全文索引</a> 或是自己做一个索引（比如说：搜索关键词或是Tag什么的）</p>
<h4>5. 在Join表的时候使用相当类型的例，并将其索引</h4>
<p>如果你的应用程序有很多 JOIN 查询，你应该确认两个表中Join的字段是被建过索引的。这样，MySQL内部会启动为你优化Join的SQL语句的机制。</p>
<p>而且，这些被用来Join的字段，应该是相同的类型的。例如：如果你要把 DECIMAL 字段和一个 INT 字段Join在一起，MySQL就无法使用它们的索引。对于那些STRING类型，还需要有相同的字符集才行。（两个表的字符集有可能不一样）</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 在state中查找company
$r = mysql_query(&quot;SELECT company_name FROM users
	LEFT JOIN companies ON (users.state = companies.state)
	WHERE users.id = $user_id&quot;);

// 两个 state 字段应该是被建过索引的，而且应该是相当的类型，相同的字符集。
</pre>
<h4>6. 千万不要 ORDER BY RAND()</h4>
<p>想打乱返回的数据行？随机挑一个数据？真不知道谁发明了这种用法，但很多新手很喜欢这样用。但你确不了解这样做有多么可怕的性能问题。</p>
<p>如果你真的想把返回的数据行打乱了，你有N种方法可以达到这个目的。这样使用只让你的数据库的性能呈指数级的下降。这里的问题是：MySQL会不得不去执行RAND()函数（很耗CPU时间），而且这是为了每一行记录去记行，然后再对其排序。就算是你用了Limit 1也无济于事（因为要排序）</p>
<p>下面的示例是随机挑一条记录</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 千万不要这样做：
$r = mysql_query(&quot;SELECT username FROM user ORDER BY RAND() LIMIT 1&quot;);

// 这要会更好：
$r = mysql_query(&quot;SELECT count(*) FROM user&quot;);
$d = mysql_fetch_row($r);
$rand = mt_rand(0,$d[0] - 1);

$r = mysql_query(&quot;SELECT username FROM user LIMIT $rand, 1&quot;);
</pre>
<h4>7. 避免 SELECT *</h4>
<p>从数据库里读出越多的数据，那么查询就会变得越慢。并且，如果你的数据库服务器和WEB服务器是两台独立的服务器的话，这还会增加网络传输的负载。</p>
<p>所以，你应该养成一个需要什么就取什么的好的习惯。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 不推荐
$r = mysql_query(&quot;SELECT * FROM user WHERE user_id = 1&quot;);
$d = mysql_fetch_assoc($r);
echo &quot;Welcome {$d[&#039;username&#039;]}&quot;;

// 推荐
$r = mysql_query(&quot;SELECT username FROM user WHERE user_id = 1&quot;);
$d = mysql_fetch_assoc($r);
echo &quot;Welcome {$d[&#039;username&#039;]}&quot;;
</pre>
<h4>8. 永远为每张表设置一个ID</h4>
<p>我们应该为数据库里的每张表都设置一个ID做为其主键，而且最好的是一个INT型的（推荐使用UNSIGNED），并设置上自动增加的AUTO_INCREMENT标志。</p>
<p>就算是你 users 表有一个主键叫 “email”的字段，你也别让它成为主键。使用 VARCHAR 类型来当主键会使用得性能下降。另外，在你的程序中，你应该使用表的ID来构造你的数据结构。</p>
<p>而且，在MySQL数据引擎下，还有一些操作需要使用主键，在这些情况下，主键的性能和设置变得非常重要，比如，集群，分区……</p>
<p>在这里，只有一个情况是例外，那就是“关联表”的“外键”，也就是说，这个表的主键，通过若干个别的表的主键构成。我们把这个情况叫做“外键”。比如：有一个“学生表”有学生的ID，有一个“课程表”有课程ID，那么，“成绩表”就是“关联表”了，其关联了学生表和课程表，在成绩表中，学生ID和课程ID叫“外键”其共同组成主键。</p>
<h4>9. 使用 ENUM 而不是 VARCHAR</h4>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/enum.html" target="_blank">ENUM</a> 类型是非常快和紧凑的。在实际上，其保存的是 TINYINT，但其外表上显示为字符串。这样一来，用这个字段来做一些选项列表变得相当的完美。</p>
<p>如果你有一个字段，比如“性别”，“国家”，“民族”，“状态”或“部门”，你知道这些字段的取值是有限而且固定的，那么，你应该使用 ENUM 而不是 VARCHAR。</p>
<p>MySQL也有一个“建议”（见第十条）告诉你怎么去重新组织你的表结构。当你有一个 VARCHAR 字段时，这个建议会告诉你把其改成 ENUM 类型。使用 PROCEDURE ANALYSE() 你可以得到相关的建议。</p>
<h4>10. 从 PROCEDURE ANALYSE() 取得建议</h4>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.html" target="_blank">PROCEDURE ANALYSE()</a> 会让 MySQL 帮你去分析你的字段和其实际的数据，并会给你一些有用的建议。只有表中有实际的数据，这些建议才会变得有用，因为要做一些大的决定是需要有数据作为基础的。</p>
<p>例如，如果你创建了一个 INT 字段作为你的主键，然而并没有太多的数据，那么，PROCEDURE ANALYSE()会建议你把这个字段的类型改成 MEDIUMINT 。或是你使用了一个 VARCHAR 字段，因为数据不多，你可能会得到一个让你把它改成 ENUM 的建议。这些建议，都是可能因为数据不够多，所以决策做得就不够准。</p>
<p>在phpmyadmin里，你可以在查看表时，点击 &#8220;Propose table structure&#8221; 来查看这些建议</p>
<div class="tutorial_image"><img decoding="async" src="http://nettuts.s3.amazonaws.com/500_mysql/suggestions.jpg" border="0" alt="" /></div>
<p>一定要注意，这些只是建议，只有当你的表里的数据越来越多时，这些建议才会变得准确。一定要记住，你才是最终做决定的人。</p>
<h4>11. 尽可能的使用 NOT NULL</h4>
<p>除非你有一个很特别的原因去使用 NULL 值，你应该总是让你的字段保持 NOT NULL。这看起来好像有点争议，请往下看。</p>
<p>首先，问问你自己“Empty”和“NULL”有多大的区别（如果是INT，那就是0和NULL）？如果你觉得它们之间没有什么区别，那么你就不要使用NULL。（你知道吗？在 Oracle 里，NULL 和 Empty 的字符串是一样的！)</p>
<p>不要以为 NULL 不需要空间，其需要额外的空间，并且，在你进行比较的时候，你的程序会更复杂。 当然，这里并不是说你就不能使用NULL了，现实情况是很复杂的，依然会有些情况下，你需要使用NULL值。</p>
<p>下面摘自MySQL自己的文档：</p>
<blockquote><p>&#8220;NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.&#8221;</p></blockquote>
<h4>12. Prepared Statements</h4>
<p>Prepared Statements很像存储过程，是一种运行在后台的SQL语句集合，我们可以从使用 prepared statements 获得很多好处，无论是性能问题还是安全问题。</p>
<p>Prepared Statements 可以检查一些你绑定好的变量，这样可以保护你的程序不会受到“SQL注入式”攻击。当然，你也可以手动地检查你的这些变量，然而，手动的检查容易出问题，而且很经常会被程序员忘了。当我们使用一些framework或是ORM的时候，这样的问题会好一些。</p>
<p>在性能方面，当一个相同的查询被使用多次的时候，这会为你带来可观的性能优势。你可以给这些Prepared Statements定义一些参数，而MySQL只会解析一次。</p>
<p>虽然最新版本的MySQL在传输Prepared Statements是使用二进制形势，所以这会使得网络传输非常有效率。</p>
<p>当然，也有一些情况下，我们需要避免使用Prepared Statements，因为其不支持查询缓存。但据说版本5.1后支持了。</p>
<p>在PHP中要使用prepared statements，你可以查看其使用手册：<a href="http://php.net/manual/en/book.mysqli.php" target="_blank">mysqli 扩展</a> 或是使用数据库抽象层，如： <a href="http://us.php.net/manual/en/book.pdo.php" target="_blank">PDO</a>.</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
// 创建 prepared statement
if ($stmt = $mysqli-&gt;prepare(&quot;SELECT username FROM user WHERE state=?&quot;)) {

	// 绑定参数
    $stmt-&gt;bind_param(&quot;s&quot;, $state);

	// 执行
    $stmt-&gt;execute();

	// 绑定结果
    $stmt-&gt;bind_result($username);

	// 移动游标
    $stmt-&gt;fetch();

    printf(&quot;%s is from %s\n&quot;, $username, $state);

    $stmt-&gt;close();
}
</pre>
<h4>13. 无缓冲的查询</h4>
<p>正常的情况下，当你在当你在你的脚本中执行一个SQL语句的时候，你的程序会停在那里直到没这个SQL语句返回，然后你的程序再往下继续执行。你可以使用无缓冲查询来改变这个行为。</p>
<p>关于这个事情，在PHP的文档中有一个非常不错的说明： <a href="http://php.net/manual/en/function.mysql-unbuffered-query.php" target="_blank">mysql_unbuffered_query()</a> 函数：</p>
<blockquote><p>&#8220;mysql_unbuffered_query() sends the SQL query query to MySQL without automatically fetching and buffering the result rows as mysql_query() does. This saves a considerable amount of memory with SQL queries that produce large result sets, and you can start working on the result set immediately after the first row has been retrieved as you don&#8217;t have to wait until the complete SQL query has been performed.&#8221;</p></blockquote>
<p>上面那句话翻译过来是说，mysql_unbuffered_query() 发送一个SQL语句到MySQL而并不像mysql_query()一样去自动fethch和缓存结果。这会相当节约很多可观的内存，尤其是那些会产生大量结果的查询语句，并且，你不需要等到所有的结果都返回，只需要第一行数据返回的时候，你就可以开始马上开始工作于查询结果了。</p>
<p>然而，这会有一些限制。因为你要么把所有行都读走，或是你要在进行下一次的查询前调用 <a href="http://us2.php.net/manual/en/function.mysql-free-result.php" target="_blank">mysql_free_result()</a> 清除结果。而且， <a href="http://us2.php.net/manual/en/function.mysql-num-rows.php" target="_blank">mysql_num_rows()</a> 或 <a href="http://us2.php.net/manual/en/function.mysql-data-seek.php" target="_blank">mysql_data_seek()</a> 将无法使用。所以，是否使用无缓冲的查询你需要仔细考虑。</p>
<h4>14. 把IP地址存成 UNSIGNED INT</h4>
<p>很多程序员都会创建一个 VARCHAR(15) 字段来存放字符串形式的IP而不是整形的IP。如果你用整形来存放，只需要4个字节，并且你可以有定长的字段。而且，这会为你带来查询上的优势，尤其是当你需要使用这样的WHERE条件：IP between ip1 and ip2。</p>
<p>我们必需要使用UNSIGNED INT，因为 IP地址会使用整个32位的无符号整形。</p>
<p>而你的查询，你可以使用 <a href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton" target="_blank">INET_ATON()</a> 来把一个字符串IP转成一个整形，并使用 <a href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-ntoa" target="_blank">INET_NTOA()</a> 把一个整形转成一个字符串IP。在PHP中，也有这样的函数 <a href="http://php.net/manual/en/function.ip2long.php" target="_blank">ip2long()</a> 和 <a href="http://us.php.net/manual/en/function.long2ip.php" target="_blank">long2ip()</a>。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
$r = &quot;UPDATE users SET ip = INET_ATON(&#039;{$_SERVER[&#039;REMOTE_ADDR&#039;]}&#039;) WHERE user_id = $user_id&quot;;
</pre>
<h4>15. 固定长度的表会更快</h4>
<p>如果表中的所有字段都是“固定长度”的，整个表会被认为是 <a href="http://dev.mysql.com/doc/refman/5.1/en/static-format.html" target="_blank">&#8220;static&#8221; 或 &#8220;fixed-length&#8221;</a>。 例如，表中没有如下类型的字段： VARCHAR，TEXT，BLOB。只要你包括了其中一个这些字段，那么这个表就不是“固定长度静态表”了，这样，MySQL 引擎会用另一种方法来处理。</p>
<p>固定长度的表会提高性能，因为MySQL搜寻得会更快一些，因为这些固定的长度是很容易计算下一个数据的偏移量的，所以读取的自然也会很快。而如果字段不是定长的，那么，每一次要找下一条的话，需要程序找到主键。</p>
<p>并且，固定长度的表也更容易被缓存和重建。不过，唯一的副作用是，固定长度的字段会浪费一些空间，因为定长的字段无论你用不用，他都是要分配那么多的空间。</p>
<p>使用“垂直分割”技术（见下一条），你可以分割你的表成为两个一个是定长的，一个则是不定长的。</p>
<h4>16. 垂直分割</h4>
<p>“垂直分割”是一种把数据库中的表按列变成几张表的方法，这样可以降低表的复杂度和字段的数目，从而达到优化的目的。（以前，在银行做过项目，见过一张表有100多个字段，很恐怖）</p>
<p><strong>示例一</strong>：在Users表中有一个字段是家庭地址，这个字段是可选字段，相比起，而且你在数据库操作的时候除了个人信息外，你并不需要经常读取或是改写这个字段。那么，为什么不把他放到另外一张表中呢？ 这样会让你的表有更好的性能，大家想想是不是，大量的时候，我对于用户表来说，只有用户ID，用户名，口令，用户角色等会被经常使用。小一点的表总是会有好的性能。</p>
<p><strong>示例二</strong>： 你有一个叫 &#8220;last_login&#8221; 的字段，它会在每次用户登录时被更新。但是，每次更新时会导致该表的查询缓存被清空。所以，你可以把这个字段放到另一个表中，这样就不会影响你对用户ID，用户名，用户角色的不停地读取了，因为查询缓存会帮你增加很多性能。</p>
<p>另外，你需要注意的是，这些被分出去的字段所形成的表，你不会经常性地去Join他们，不然的话，这样的性能会比不分割时还要差，而且，会是极数级的下降。</p>
<h4>17. 拆分大的 DELETE 或 INSERT 语句</h4>
<p>如果你需要在一个在线的网站上去执行一个大的 DELETE 或 INSERT 查询，你需要非常小心，要避免你的操作让你的整个网站停止相应。因为这两个操作是会锁表的，表一锁住了，别的操作都进不来了。</p>
<p>Apache 会有很多的子进程或线程。所以，其工作起来相当有效率，而我们的服务器也不希望有太多的子进程，线程和数据库链接，这是极大的占服务器资源的事情，尤其是内存。</p>
<p>如果你把你的表锁上一段时间，比如30秒钟，那么对于一个有很高访问量的站点来说，这30秒所积累的访问进程/线程，数据库链接，打开的文件数，可能不仅仅会让你泊WEB服务Crash，还可能会让你的整台服务器马上掛了。</p>
<p>所以，如果你有一个大的处理，你定你一定把其拆分，使用 LIMIT 条件是一个好的方法。下面是一个示例：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
while (1) {
    //每次只做1000条
	mysql_query(&quot;DELETE FROM logs WHERE log_date &lt;= &#039;2009-11-01&#039; LIMIT 1000&quot;);
	if (mysql_affected_rows() == 0) {
		// 没得可删了，退出！
		break;
	}
	// 每次都要休息一会儿
	usleep(50000);
}
</pre>
<h4>18. 越小的列会越快</h4>
<p>对于大多数的数据库引擎来说，硬盘操作可能是最重大的瓶颈。所以，把你的数据变得紧凑会对这种情况非常有帮助，因为这减少了对硬盘的访问。</p>
<p>参看 MySQL 的文档 <a href="http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html" target="_blank">Storage Requirements</a> 查看所有的数据类型。</p>
<p>如果一个表只会有几列罢了（比如说字典表，配置表），那么，我们就没有理由使用 INT 来做主键，使用 MEDIUMINT, SMALLINT 或是更小的 TINYINT 会更经济一些。如果你不需要记录时间，使用 DATE 要比 DATETIME 好得多。</p>
<p>当然，你也需要留够足够的扩展空间，不然，你日后来干这个事，你会死的很难看，参看<a href="http://news.slashdot.org/article.pl?sid=06/11/09/1534204" target="_blank">Slashdot的例子</a>（2009年11月06日），一个简单的ALTER TABLE语句花了3个多小时，因为里面有一千六百万条数据。</p>
<h4>19. 选择正确的存储引擎</h4>
<p>在 MySQL 中有两个存储引擎 MyISAM 和 InnoDB，每个引擎都有利有弊。酷壳以前文章《<a href="https://coolshell.cn/articles/652.html" target="_blank">MySQL: InnoDB 还是 MyISAM?</a>》讨论和这个事情。</p>
<p>MyISAM 适合于一些需要大量查询的应用，但其对于有大量写操作并不是很好。甚至你只是需要update一个字段，整个表都会被锁起来，而别的进程，就算是读进程都无法操作直到读操作完成。另外，MyISAM 对于 SELECT COUNT(*) 这类的计算是超快无比的。</p>
<p>InnoDB 的趋势会是一个非常复杂的存储引擎，对于一些小的应用，它会比 MyISAM 还慢。他是它支持“行锁” ，于是在写操作比较多的时候，会更优秀。并且，他还支持更多的高级应用，比如：事务。</p>
<p>下面是MySQL的手册</p>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/myisam-storage-engine.html">target=&#8221;_blank&#8221;MyISAM Storage Engine</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/innodb.html" target="_blank">InnoDB Storage Engine</a></li>
</ul>
<h4>20. 使用一个对象关系映射器（Object Relational Mapper）</h4>
<p>使用 ORM (Object Relational Mapper)，你能够获得可靠的性能增涨。一个ORM可以做的所有事情，也能被手动的编写出来。但是，这需要一个高级专家。</p>
<p>ORM 的最重要的是“Lazy Loading”，也就是说，只有在需要的去取值的时候才会去真正的去做。但你也需要小心这种机制的副作用，因为这很有可能会因为要去创建很多很多小的查询反而会降低性能。</p>
<p>ORM 还可以把你的SQL语句打包成一个事务，这会比单独执行他们快得多得多。</p>
<p>目前，个人最喜欢的PHP的ORM是：<a href="http://www.doctrine-project.org" target="_blank">Doctrine</a>。</p>
<h4>21. 小心“永久链接”</h4>
<p>“永久链接”的目的是用来减少重新创建MySQL链接的次数。当一个链接被创建了，它会永远处在连接的状态，就算是数据库操作已经结束了。而且，自从我们的Apache开始重用它的子进程后——也就是说，下一次的HTTP请求会重用Apache的子进程，并重用相同的 MySQL 链接。</p>
<ul>
<li><a href="http://php.net/manual/en/function.mysql-pconnect.php" target="_blank">PHP手册：mysql_pconnect() </a></li>
</ul>
<p>在理论上来说，这听起来非常的不错。但是从个人经验（也是大多数人的）上来说，这个功能制造出来的麻烦事更多。因为，你只有有限的链接数，内存问题，文件句柄数，等等。</p>
<p>而且，Apache 运行在极端并行的环境中，会创建很多很多的了进程。这就是为什么这种“永久链接”的机制工作地不好的原因。在你决定要使用“永久链接”之前，你需要好好地考虑一下你的整个系统的架构。</p>
<p>文章：<a href="http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/" target="_blank">来源</a></p>
<p>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/1889.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/12/sql.where_.clause-150x150.jpg" alt="SQL的Where语句" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1889.html" class="wp_rp_title">SQL的Where语句</a></li><li ><a href="https://coolshell.cn/articles/652.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/17.jpg" alt="MySQL: InnoDB 还是 MyISAM?" width="150" height="150" /></a><a href="https://coolshell.cn/articles/652.html" class="wp_rp_title">MySQL: InnoDB 还是 MyISAM?</a></li><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/7490.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/06/f1-150x150.jpg" alt="性能调优攻略" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7490.html" class="wp_rp_title">性能调优攻略</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1846.html">MySQL性能优化的最佳20+条经验</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1846.html/feed</wfw:commentRss>
			<slash:comments>169</slash:comments>
		
		
			</item>
		<item>
		<title>编程语言汽车</title>
		<link>https://coolshell.cn/articles/1839.html</link>
					<comments>https://coolshell.cn/articles/1839.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Tue, 24 Nov 2009 10:24:22 +0000</pubDate>
				<category><![CDATA[编程语言]]></category>
		<category><![CDATA[轶事趣闻]]></category>
		<category><![CDATA[Basic]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1839</guid>

					<description><![CDATA[<p>以前酷壳发布过《操作系统航空公司》戏谑了一下如果操作系统是航空公司会怎么样的一种情况。现在，我们来YY一下编程语言，如果编程语言是汽车，又会怎么样？ Ada  ...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1839.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1839.html">编程语言汽车</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script><strong><img decoding="async" loading="lazy" class="alignright" title="Oscar Mayer Wienermobile" src="https://coolshell.cn/wp-content/uploads/2009/11/oscar-meyer-wienermobile.jpg" alt="Oscar Mayer Wienermobile" width="225" height="167" /></strong>以前酷壳发布过《<a rel="bookmark" href="https://coolshell.cn/articles/1272.html">操作系统航空公司</a>》戏谑了一下如果操作系统是航空公司会怎么样的一种情况。现在，我们来YY一下编程语言，如果编程语言是汽车，又会怎么样？</p>
<li><strong>Ada</strong>   这是一辆坦克。一个很厚重但很丑的坦克，从不会崩溃。如果你告诉别人你正在驾驶Ada，别人会狂笑不已。但是，你会开着一辆跑车去打战吗？[from Amit Dubey]</li>
<li><strong>汇编语言</strong>   只是一个祼露在外的引擎。你不得不自己去造车，并向其提供汽油，但你在驾车时要小心，因为他会像一只从地狱放出来的蝙蝠一样。其实，对于汇编语言，你自己才是车。[From &#8220;Subterfug&#8221; off digg.com:]</li>
<li><strong>Basic</strong>   是一辆很简单的车，对于一些短途的交通比如去一些超市商店，他是很有用的。以前这是一个对初学者很流行的车，然而，近来它蜕变成脚本，而更新的车型被抛光以应对长途旅程，但那也只是新瓶装旧酒。[from Przemyslaw Wrzos]</li>
<li><strong>C</strong>   是一辆赛车，它的速度是令人难以想象的快，可惜的是它每50公里就会损毁一次。</li>
<li><strong>Cobol</strong>   号称是一辆车，但是，没有哪个“有自尊的司机”会承认以前驾驶过它。</li>
<li><strong>C#</strong>   是一个竞争性的家庭旅行车。一旦你开始使用，你就别想再使用别的竞争者的产品了。</li>
<li><strong>C++</strong>   是一个加大马力的C赛车，其有一堆新增的功能，而且，它只会每250公里损毁一次。可是，一旦它有故障，没人会知道故障发生在哪里。</li>
<p><span id="more-1839"></span></p>
<li><strong>Eiffel</strong>   是一个车，其包括了一个法国口音的内建的驾驶讲师。他会帮你很快的识别你的错误，但是你不能和他争，不然，他会凌辱你后毫不迟疑地把你扔到窗外。[From Daniel Prager ]</li>
<li><strong>Erlang</strong>   是一个汽车车队，你想去哪它都会非常合作。你只需要用一只脚就可以开动好几辆车。但是，一旦你学会了如何在它给你设计的地形上驾驶，你就会很难在别的地形上驾驶了。另外，由于你一次驾驶好几辆车，所以，就算是其中几车损毁了也无关紧要。</li>
<li><strong>Forth</strong>   是一辆你通过一些工具可以自己造出来的车。你的这个车不需要像别的车。然后，一辆Forth 车只有倒档。[By &#8220;256byteram&#8221;, on a comment on Digg.com ]</li>
<li><strong>Fortran</strong>   是一个非常漂亮的老爷车。它可以走得很快，但条件是那是一条很直的路，而且路上只有你自己。我们相信，学习去驾驶一辆Fortran车，你就可能去学习别的车型。</li>
<li><strong>Java</strong>   也是一个家用旅行车，很容易驾驶，但不是很快，而且这是一个你无法伤害自己的车。</li>
<li><strong>Haskell</strong>  是一个令人难以想象的超完美设计的相当漂亮的车，有谣言说，这是一辆要可以行驶在极端怪异地形上的车。有一天，你尝试着要去开它，但你发现它并不是顺着路行驶，而是，它把自己和道路都复制了很多份，每一个道路的复制品上都有一辆车，而这些车的位置都比前一个要往前一些。按理来说，我们可以更便捷地驾驶它，但你却对数据不是很懂，所以，你不知道怎么做。<br />
[Monadic 版:]<strong>Haskell</strong>  并不是一个真正的车。这是一个抽象机器，你需要给足你是怎么去驾驶汽车的流程描述。你不得不把这些抽象机器放到某一个真实的机器中，这样它才能真正的行驶。你并不需要知道，那个真实的机器是怎么工作的。而且，我们还可以把多个抽象机器作成一个抽象机器，这样，当你把其放进真实机器中时，你就能去很多地方了。</li>
<li><strong>Lisp</strong>  看上去像一辆车，但你只需要调整，你可把它变成一个飞机或是一个潜水艇。[from Paul Tanimoto:] 首先，这看起来并不像一辆车，但是你会发现还是有人在开他四处走。在你决定去学习驾驶它后，你会意识到这是一辆你可以制造更多的车的车。你告诉你的朋友，但你的朋友们嘲笑你说这个车看起来太怪异了。但就算是这样，你还是始终在你的车库中放着一辆Lisp，并希望有一天你的朋友会开关他到街上。</li>
<li><strong>Mathematica</strong>   是一个设置精良的车，其从Lisp借鉴了很多但却没有得到应得的声望。它可以知道什么才是到达目的地最有效的道路，但是那需要运气。</li>
<li><strong>Matlab</strong>   是一辆设计给新手司机使用的车，它过可用作一些短途用途，而且，适合它的地形也不多，和是那些“数学车”适合的地形差不多。在这种地面上，驾驶它是非常舒服的，但是一旦你离开适合它的地形，就算是一小辆Matlab的车也会变得很难驾驶。而很多专业的司机都拒绝承认这是一辆车。</li>
<li><strong>Ocaml</strong>   是一个很性感的欧洲车。它并不像 <strong>C </strong>一样的快，但他永远不会被损毁。然后，这是法国式的，所有的控制装置都不在正常的位置。</li>
<li><strong>Perl</strong>   本来应该是一个很酷的车，但是它的驾驶员手册相当的难以理解。另外，即使你能搞懂如何驾驶Perl车，你也不能去驾驶别的车。</li>
<li>
<p style="TEXT-ALIGN: left"><strong>PHP</strong>   是一个 Oscar Mayer Wienermobile（见本文文章头上的图片），它是一个很怪异的车，但是还是有很多的人喜欢去驾驶它。 [from &#8220;CosmicJustice&#8221; off of digg.com]</p>
</li>
<li><strong>Prolog</strong>   是一个完全自动化的车：你只要告诉它目的地是什么样的，它就可以带着你去那。[附录 from Paul Graham:] 然而，说明目的地的工作量和你自己开车到那里的工作时是一样的。[另一个版本] <strong>Prolog</strong>   这个车有一个独一无二的GPS装置。它会去为你寻找你要到的目的地，如果到了路的尽头还没有找到，那么，他会回来然后再去试另一条路，直到找到你的目的地为止。</li>
<li><strong>Python</strong>   是一个相当不错的入门者的车。你没有驾照也可以驾驶它。除非，你真的想把它开得很快，或是在很BT的地形上驾驶。有了它，你可能不再需要别的车。</li>
<li><strong>Ruby</strong>   是一个把Perl, Python和Smalltalk三辆车混合起来的一辆拼装车。一个日本的技师找到了Perl, Python和Smalltalk一些碎片并把这些碎片拼成成了一辆车。很多司机认为这个拼装车比其它三个全部加起来都好。而其它一些司机却喃喃道，这个车提供了很多重复的功能，甚至是三重一样的功能，这些重复的功能在不固定的环境下却又有一些细小的不同，这些重复的功能让这个车更难驾驶。有谣言说Ruby这个车要重新设计。</li>
<li><strong>Smalltalk</strong>   只是一个小型车，其原来的目的只是为了让大家学习驾驶。但是，这个车设计的太好了，就算是很有经验的老手也很喜欢驾驶它。它开起来并不是很快，但是你可以把这个车的各个部件全部解开，并且换上你像要的部件，或是组装成你喜欢的样子。你可以给他发一个短信告诉它你要去哪，它会带着你去那，或是告诉你它听不懂你在说什么。很人性化的一辆车。</li>
<li><strong>Visual Basic</strong>   这是一辆驾驭你的车。 [from &#8220;yivkX360&#8221; on digg.com]</li>
<p> </p>
<p>文章：<a href="http://www.cs.caltech.edu/~mvanier/hacking/rants/cars.html" target="_blank">来源</a><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="到处都是Unix的胎记" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_title">到处都是Unix的胎记</a></li><li ><a href="https://coolshell.cn/articles/2529.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/6.jpg" alt="StackOverflow的404错误页" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2529.html" class="wp_rp_title">StackOverflow的404错误页</a></li><li ><a href="https://coolshell.cn/articles/10337.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="数据即代码：元驱动编程" width="150" height="150" /></a><a href="https://coolshell.cn/articles/10337.html" class="wp_rp_title">数据即代码：元驱动编程</a></li><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/20.jpg" alt="最为奇怪的程序语言的特性" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_title">最为奇怪的程序语言的特性</a></li><li ><a href="https://coolshell.cn/articles/1992.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys-150x150.jpg" alt="程序员眼中的编程语言" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1992.html" class="wp_rp_title">程序员眼中的编程语言</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1839.html">编程语言汽车</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1839.html/feed</wfw:commentRss>
			<slash:comments>23</slash:comments>
		
		
			</item>
		<item>
		<title>到处都是Unix的胎记</title>
		<link>https://coolshell.cn/articles/1532.html</link>
					<comments>https://coolshell.cn/articles/1532.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Sun, 11 Oct 2009 10:01:06 +0000</pubDate>
				<category><![CDATA[Unix/Linux]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[fork]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[Unix]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1532</guid>

					<description><![CDATA[<p>一说起Unix编程，不必多说，最著名的系统调用就是fork，pipe，exec，kill或是socket了（fork(2), execve(2), pipe(2...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1532.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1532.html">到处都是Unix的胎记</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>一说起Unix编程，不必多说，最著名的系统调用就是fork，pipe，exec，kill或是socket了（<a href="http://www.kernel.org/doc/man-pages/online/pages/man2/fork.2.html"><code>fork(2)</code></a>, <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/execve.2.html"><code>execve(2)</code></a>, <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/pipe.2.html"><code>pipe(2)</code></a>, <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/socketpair.2.html"><code>socketpair(2)</code></a>, <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/select.2.html"><code>select(2)</code></a>, <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/kill.2.html"><code>kill(2)</code></a>, <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/sigaction.2.html"><code>sigaction(2)</code></a>）这些系统调用都像是Unix编程的胎记或签名一样，表明着它来自于Unix。</p>
<p>下面这篇文章，将向大家展示Unix下最经典的socket的编程例子——使用fork + socket来创建一个TCP/IP的服务程序。这个编程模式很简单，首先是创建Socket，然后把其绑定在某个IP和Port上上侦听连接，接下来的一般做法是使用一个fork创建一个client服务进程再加上一个死循环用于处理和client的交互。这个模式是Unix下最经典的Socket编程例子。</p>
<p>下面，让我们看看用C，Ruby，Python，Perl，PHP和Haskell来实现这一例子，你会发现这些例子中的Unix的胎记。如果你想知道这些例子中的技术细节，那么，向你推荐两本经典书——《Unix高级环境编程》和《Unix网络编程》。</p>
<p><span id="more-1532"></span></p>
<h4>C语言</h4>
<p>我们先来看一下经典的C是怎么实现的。</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/**
 * A simple preforking echo server in C.
 *
 * Building:
 *
 * $ gcc -Wall -o echo echo.c
 *
 * Usage:
 *
 * $ ./echo
 *
 *   ~ then in another terminal ... ~
 *
 * $ echo 'Hello, world!' | nc localhost 4242
 *
 */

#include &lt;unistd.h&gt; /* fork, close */
#include &lt;stdlib.h&gt; /* exit */
#include &lt;string.h&gt; /* strlen */
#include &lt;stdio.h&gt; /* perror, fdopen, fgets */
#include &lt;sys/socket.h&gt;
#include &lt;sys/wait.h&gt; /* waitpid */
#include &lt;netdb.h&gt; /* getaddrinfo */

#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)

#define PORT "4242"
#define NUM_CHILDREN 3

#define MAXLEN 1024

int readline(int fd, char *buf, int maxlen); // forward declaration

int
main(int argc, char** argv)
{
    int i, n, sockfd, clientfd;
    int yes = 1; // used in setsockopt(2)
    struct addrinfo *ai;
    struct sockaddr_in *client;
    socklen_t client_t;
    pid_t cpid; // child pid
    char line[MAXLEN];
    char cpid_s[32];
    char welcome[32];

    /* Create a socket and get its file descriptor -- socket(2) */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
    die("Couldn't create a socket");
    }

    /* Prevents those dreaded "Address already in use" errors */
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&amp;yes, sizeof(int)) == -1) {
    die("Couldn't setsockopt");
    }

    /* Fill the address info struct (host + port) -- getaddrinfo(3) */
    if (getaddrinfo(NULL, PORT, NULL, &amp;ai) != 0) {
    die("Couldn't get address");
    }

    /* Assign address to this socket's fd */
    if (bind(sockfd, ai-&gt;ai_addr, ai-&gt;ai_addrlen) != 0) {
    die("Couldn't bind socket to address");
    }

    /* Free the memory used by our address info struct */
    freeaddrinfo(ai);

    /* Mark this socket as able to accept incoming connections */
    if (listen(sockfd, 10) == -1) {
    die("Couldn't make socket listen");
    }

    /* Fork you some child processes. */
    for (i = 0; i &lt; NUM_CHILDREN; i++) {
    cpid = fork();
    if (cpid == -1) {
        die("Couldn't fork");
    }

    if (cpid == 0) { // We're in the child ...
        for (;;) { // Run forever ...
        /* Necessary initialization for accept(2) */
        client_t = sizeof client;

        /* Blocks! */
        clientfd = accept(sockfd, (struct sockaddr *)&amp;client, &amp;client_t);
        if (clientfd == -1) {
            die("Couldn't accept a connection");
        }

        /* Send a welcome message/prompt */
        bzero(cpid_s, 32);
        bzero(welcome, 32);
        sprintf(cpid_s, "%d", getpid());
        sprintf(welcome, "Child %s echo&gt; ", cpid_s);
        send(clientfd, welcome, strlen(welcome), 0);

        /* Read a line from the client socket ... */
        n = readline(clientfd, line, MAXLEN);
        if (n == -1) {
            die("Couldn't read line from connection");
        }

        /* ... and echo it back */
        send(clientfd, line, n, 0);

        /* Clean up the client socket */
        close(clientfd);
        }
    }
    }

    /* Sit back and wait for all child processes to exit */
    while (waitpid(-1, NULL, 0) &gt; 0);

    /* Close up our socket */
    close(sockfd);

    return 0;
}

/**
 * Simple utility function that reads a line from a file descriptor fd,
 * up to maxlen bytes -- ripped from Unix Network Programming, Stevens.
 */
int
readline(int fd, char *buf, int maxlen)
{
    int n, rc;
    char c;

    for (n = 1; n &lt; maxlen; n++) {
    if ((rc = read(fd, &amp;c, 1)) == 1) {
        *buf++ = c;
        if (c == '\n')
        break;
    } else if (rc == 0) {
        if (n == 1)
        return 0; // EOF, no data read
        else
        break; // EOF, read some data
    } else
        return -1; // error
    }

    *buf = '\0'; // null-terminate
    return n;
}
</pre>
<h4>Ruby</h4>
<p>下面是Ruby，你可以看到其中的fork</p>
<pre class="EnlighterJSRAW" data-enlighter-language="ruby">
# simple preforking echo server in Ruby
require 'socket'

# Create a socket, bind it to localhost:4242, and start listening.
# Runs once in the parent; all forked children inherit the socket's
# file descriptor.
acceptor = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
address = Socket.pack_sockaddr_in(4242, 'localhost')
acceptor.bind(address)
acceptor.listen(10)

# Close the socket when we exit the parent or any child process. This
# only closes the file descriptor in the calling process, it does not
# take the socket out of the listening state (until the last fd is
# closed).
#
# The trap is guaranteed to happen, and guaranteed to happen only
# once, right before the process exits for any reason (unless
# it's terminated with a SIGKILL).
trap('EXIT') { acceptor.close }

# Fork you some child processes. In the parent, the call to fork
# returns immediately with the pid of the child process; fork never
# returns in the child because we exit at the end of the block.
3.times do
  fork do
    # now we're in the child process; trap (Ctrl-C) interrupts and
    # exit immediately instead of dumping stack to stderr.
    trap('INT') { exit }

    puts "child #$$ accepting on shared socket (localhost:4242)"
    loop {
      # This is where the magic happens. accept(2) blocks until a
      # new connection is ready to be dequeued.
      socket, addr = acceptor.accept
      socket.write "child #$$ echo&gt; "
      socket.flush
      message = socket.gets
      socket.write message
      socket.close
      puts "child #$$ echo'd: '#{message.strip}'"
    }
    exit
  end
end

# Trap (Ctrl-C) interrupts, write a note, and exit immediately
# in parent. This trap is not inherited by the forks because it
# runs after forking has commenced.
trap('INT') { puts "\nbailing" ; exit }

# Sit back and wait for all child processes to exit.
Process.waitall

</pre>
<h4>Python</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="python">"""
Simple preforking echo server in Python.
"""

import os
import sys
import socket

# Create a socket, bind it to localhost:4242, and start
# listening. Runs once in the parent; all forked children
# inherit the socket's file descriptor.
acceptor = socket.socket()
acceptor.bind(('localhost', 4242))
acceptor.listen(10)

# Ryan's Ruby code here traps EXIT and closes the socket. This
# isn't required in Python; the socket will be closed when the
# socket object gets garbage collected.

# Fork you some child processes. In the parent, the call to
# fork returns immediately with the pid of the child process;
# fork never returns in the child because we exit at the end
# of the block.
for i in range(3):
    pid = os.fork()

    # os.fork() returns 0 in the child process and the child's
    # process id in the parent. So if pid == 0 then we're in
    # the child process.
    if pid == 0:
        # now we're in the child process; trap (Ctrl-C)
        # interrupts by catching KeyboardInterrupt) and exit
        # immediately instead of dumping stack to stderr.
        childpid = os.getpid()
        print "Child %s listening on localhost:4242" % childpid
        try:
            while 1:
                # This is where the magic happens. accept(2)
                # blocks until a new connection is ready to be
                # dequeued.
                conn, addr = acceptor.accept()

                # For easier use, turn the socket connection
                # into a file-like object.
                flo = conn.makefile()
                flo.write('Child %s echo&gt; ' % childpid)
                flo.flush()
                message = flo.readline()
                flo.write(message)
                flo.close()
                conn.close()
                print "Child %s echo'd: %r" % \
                          (childpid, message.strip())
        except KeyboardInterrupt:
            sys.exit()

# Sit back and wait for all child processes to exit.
#
# Trap interrupts, write a note, and exit immediately in
# parent. This trap is not inherited by the forks because it
# runs after forking has commenced.
try:
    os.waitpid(-1, 0)
except KeyboardInterrupt:
    print "\nbailing"
    sys.exit()
</pre>
<h4>Perl</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="perl">#!/usr/bin/perl
use 5.010;
use strict;

# simple preforking echo server in Perl
use Proc::Fork;
use IO::Socket::INET;

sub strip { s/\A\s+//, s/\s+\z// for my @r = @_; @r }

# Create a socket, bind it to localhost:4242, and start listening.
# Runs once in the parent; all forked children inherit the socket's
# file descriptor.
my $acceptor = IO::Socket::INET-&gt;new(
    LocalPort =&gt; 4242,
    Reuse     =&gt; 1,
    Listen    =&gt; 10,
) or die "Couln't start server: $!\n";

# Close the socket when we exit the parent or any child process. This
# only closes the file descriptor in the calling process, it does not
# take the socket out of the listening state (until the last fd is
# closed).
END { $acceptor-&gt;close }

# Fork you some child processes. The code after the run_fork block runs
# in all process, but because the child block ends in an exit call, only
# the parent executes the rest of the program. If a parent block were
# specified here, it would be invoked in the parent only, and passed the
# PID of the child process.
for ( 1 .. 3 ) {
    run_fork { child {
        while (1) {
            my $socket = $acceptor-&gt;accept;
            $socket-&gt;printflush( "child $$ echo&gt; " );
            my $message = $socket-&gt;getline;
            $socket-&gt;print( $message );
            $socket-&gt;close;
            say "child $$ echo'd: '${\strip $message}'";
        }
        exit;
    } }
}

# Trap (Ctrl-C) interrupts, write a note, and exit immediately
# in parent. This trap is not inherited by the forks because it
# runs after forking has commenced.
$SIG{ 'INT' } = sub { print "bailing\n"; exit };

# Sit back and wait for all child processes to exit.
1 while 0 &lt; waitpid -1, 0;
</pre>
<h4>PHP</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="perl">
&lt;?
/*
Simple preforking echo server in PHP.
Russell Beattie (russellbeattie.com)
*/

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

# Create a socket, bind it to localhost:4242, and start
# listening. Runs once in the parent; all forked children
# inherit the socket's file descriptor.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket,'localhost', 4242);
socket_listen($socket, 10);

pcntl_signal(SIGTERM, 'shutdown');
pcntl_signal(SIGINT, 'shutdown');

function shutdown($signal){
    global $socket;
    socket_close($socket);
    exit();
}
# Fork you some child processes. In the parent, the call to
# fork returns immediately with the pid of the child process;
# fork never returns in the child because we exit at the end
# of the block.
for($x = 1; $x &lt;= 3; $x++){
   
    $pid = pcntl_fork();
   
    # pcntl_fork() returns 0 in the child process and the child's
    # process id in the parent. So if $pid == 0 then we're in
    # the child process.
    if($pid == 0){

        $childpid = posix_getpid();
       
        echo "Child $childpid listening on localhost:4242 \n";

        while(true){
            # This is where the magic happens. accept(2)
            # blocks until a new connection is ready to be
            # dequeued.
            $conn = socket_accept($socket);

            $message = socket_read($conn,1000,PHP_NORMAL_READ);
           
            socket_write($conn, "Child $childpid echo&gt; $message");
       
            socket_close($conn);
       
            echo "Child $childpid echo'd: $message \n";
       
        }

    }
}
#
# Trap interrupts, write a note, and exit immediately in
# parent. This trap is not inherited by the forks because it
# runs after forking has commenced.
try{

    pcntl_waitpid(-1, $status);

} catch (Exception $e) {

    echo "bailing \n";
    exit();

}</pre>
<h4>Haskell</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="haskell">import Network
import Prelude hiding ((-))
import Control.Monad
import System.IO
import Control.Applicative
import System.Posix
import System.Exit
import System.Posix.Signals

main :: IO ()
main = with =&lt;&lt; (listenOn - PortNumber 4242) where

  with socket = do
    replicateM 3 - forkProcess work
    wait

    where
    work = do
      installHandler sigINT (Catch trap_int) Nothing
      pid &lt;- show &lt;$&gt; getProcessID
      puts - "child " ++ pid ++ " accepting on shared socket (localhost:4242)"
     
      forever - do
        (h, _, _) &lt;- accept socket

        let write   = hPutStr h
            flush   = hFlush h
            getline = hGetLine h
            close   = hClose h

        write - "child " ++ pid ++ " echo&gt; "
        flush
        message &lt;- getline
        write - message ++ "\n"
        puts - "child " ++ pid ++ " echo'd: '" ++ message ++ "'"
        close

    wait = forever - do
      ( const () &lt;$&gt; getAnyProcessStatus True True  ) <code data-enlighter-language="raw" class="EnlighterJSRAW">catch</code> const trap_exit

    trap_int = exitImmediately ExitSuccess

    trap_exit = do
      puts "\nbailing"
      sClose socket
      exitSuccess

    puts = putStrLn

  (-) = ($)
  infixr 0 -

</pre>
<p>如果你知道更多的，请你告诉我们。（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/oscar-meyer-wienermobile-150x150.jpg" alt="编程语言汽车" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_title">编程语言汽车</a></li><li ><a href="https://coolshell.cn/articles/2529.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/6.jpg" alt="StackOverflow的404错误页" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2529.html" class="wp_rp_title">StackOverflow的404错误页</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/7965.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/fork01jpg-150x150.jpg" alt="一个fork的面试题" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7965.html" class="wp_rp_title">一个fork的面试题</a></li><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/20.jpg" alt="最为奇怪的程序语言的特性" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2053.html" class="wp_rp_title">最为奇怪的程序语言的特性</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1532.html">到处都是Unix的胎记</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1532.html/feed</wfw:commentRss>
			<slash:comments>19</slash:comments>
		
		
			</item>
		<item>
		<title>编程真难啊</title>
		<link>https://coolshell.cn/articles/1391.html</link>
					<comments>https://coolshell.cn/articles/1391.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Thu, 03 Sep 2009 14:24:57 +0000</pubDate>
				<category><![CDATA[Java语言]]></category>
		<category><![CDATA[PHP脚本]]></category>
		<category><![CDATA[技术读物]]></category>
		<category><![CDATA[编程语言]]></category>
		<category><![CDATA[轶事趣闻]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[程序员]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1391</guid>

					<description><![CDATA[<p>上周，在Sun的Java论坛上出现了一个这样的帖子，这个贴子的链接如下： http://forums.sun.com/thread.jspa?threadID=...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1391.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1391.html">编程真难啊</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>上周，在Sun的Java论坛上出现了一个这样的帖子，这个贴子的链接如下：<br />
<a href="http://forums.sun.com/thread.jspa?threadID=5404590&amp;start=0&amp;tstart=0" target="_blank">http://forums.sun.com/thread.jspa?threadID=5404590&amp;start=0&amp;tstart=0</a></p>
<p>LZ的贴子翻译如下：</p>
<blockquote><p>大家好，我是一个Java的新手，我有一个简单的问题：请问我怎么才能反转一个整数的符号啊。比如把-12转成+12。是的，毫无疑问这是个简单的问题，但我弄了一整天我也找不到什么好的方法。非常感谢如果你能告诉我Java有什么方法可以做到这个事，或者告诉我一个正确的方向——比如使用一些数学库或是二进制方法什么的。谢谢！</p></blockquote>
<p>这个贴子的沙发给出了答案：</p>
<p><span id="more-1391"></span></p>
<p style="PADDING-LEFT: 30px">n = -n;</p>
<p>LZ在四楼回复到：</p>
<blockquote><p>我知道是个很简单的事，可我没有想到居然这么简单，我觉得你可能是对的。谢谢你。</p></blockquote>
<p>过了一会，又回复到：</p>
<blockquote><p>不开玩笑地说，我试了，真的没有问题耶！</p></blockquote>
<p>看到这样的贴子，就能想到国内论坛上很多这样的“问弱智问题的贴子”，结果可能都会是比较惨！是的，国外的互联网文化和国内差不多，都是恶搞的人多于热心的人，呵呵。<strong>不过，国外的网民们有一点是好的，再恶搞也是就事搞事，不会有侮辱人的语言，这点真是值国内的人学习</strong>。</p>
<p>这本是一个平淡无奇的贴子，不过回复中那些恶搞的“解决方案”太强大了，在这里例举一下吧。</p>
<p>贴子的板凳给出了这样的答案（这是恶搞的开始）</p>
<p> </p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">
int x = numberToInvertSign;
boolean pos = x &gt; 0;
for(int i = 0; i &lt; 2*Math.abs(x); i++){
    if(pos){
        numberToInvertSign--;
    }
    else{
        numberToInvertSign++;
    }
}
</pre>
<p>然后，有人说，n = -n 可以是可以，但不够晦涩，于是一个晦涩的解决方案出现了：</p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">
int n = ....;
 n = (0xffffffff ^ n) + 1;
</pre>
<p>然后，又出现了一些看似简单，其实是比较晦涩的方案</p>
<p><code><code data-enlighter-language="java" class="EnlighterJSRAW">n = ~n + 1; </code></p>
<p> </p>
<p></code><code><code><code data-enlighter-language="java" class="EnlighterJSRAW">n = ~--n; </code></p>
<p> </p>
<p></code></code><code><code>继续，有才的人从来就不少：</code></code></p>
<p><code><code></p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">
n^= 0xffffffff;
int m;
for (m= 1; m != 0 &amp;&amp; ((n&amp;m) != 0); m&lt;&lt;= 1);
n|= m;
if (m == 0) n= m;
else for (m &gt;&gt;= 1; m != 0; n^= m, m&gt;&gt;=1);
</pre>
<p> </p>
<p></code></code><code><code>呵呵，开始越来越强大了，我以前也向大家介绍过《<a rel="bookmark" href="https://coolshell.cn/articles/933.html">如何加密/弄乱C源代码</a>》的文章，和这些恶搞的人可能有点相似吧。上面这个例子一出，大家都在讨论上面例子中的for循环语句，呵呵，很费解啊。</code></code></p>
<p><code><code>然后，后面几个就开始乱来了：</code></code></p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">public int invert(int i) {
  return i - (i + i);
}</pre>
<pre data-enlighter-language="java" class="EnlighterJSRAW">switch (i)
{
  case 1: return -1;
  case 2: return -2;
  case 3: return -3;
  // ... etc, you get the proper pattern
}</pre>
<p>不过事情还没有结束，看看下面这个吧，OMG。</p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">int absoluteValue(int num)
{
 int max = 0;
 for(int i = 0; true; ++i)
 {
  max = i &gt; max ? i : max;
  if(i == num)
  {
   if(i &gt;= max)
    return i;
   return -i;
  }
 }
}</pre>
<p> 还有用字符串的解决方案：</p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">public int invert(int n) {
    String nStr = String.valueOf(n);
 
    if (nStr.startsWith(&quot;-&quot;)) {
        nStr = nStr.replace(&quot;-&quot;, &quot;&quot;);
    } else {
        nStr = &quot;-&quot; + nStr;
    }
 
    return Integer.parseInt(nStr);
}</pre>
<p>别忘了面象对象，有最新Java支持的模板库： </p>
<pre data-enlighter-language="java" class="EnlighterJSRAW">public interface Negatable&lt;T extends Number&gt; {
  T value();
  T negate();
}
 
 
 
public abstract class NegatableInteger implements Negatable&lt;Integer&gt; {
  private final int value;
 
  protected NegatableInteger(int value) {
    this.value = value;
  }
 
  public static NegatableInteger createNegatableInteger(int value) {
    if (value &gt; 0) {
      return new NegatablePositiveInteger(value);
    }
    else if (value == Integer.MIN_VALUE) {
      throw new IllegalArgumentException(&quot;cannot negate &quot; + value);
    }
    else if (value &lt; 0) {
      return new NegatableNegativeInteger(value);
    }
    else {
      return new NegatableZeroInteger(value);
    }
  }
 
  public Integer value() {
    return value;
  }
 
  public Integer negate() {
    String negatedString = negateValueAsString ();
    Integer negatedInteger = Integer.parseInt(negatedString);
    return negatedInteger;
  }
 
  protected abstract String negateValueAsString ();
}
 
 
 
public class NegatablePositiveInteger extends NegatableInteger {
  public NegatablePositiveInteger(int value) {
    super(value);
  }
 
  protected String negateValueAsString () {
    String valueAsString = String.valueOf (value());
    return &quot;-&quot; + valueAsString;
  }
}
 
 
 
public class NegatableNegativeInteger extends NegatableInteger {
  public NegatableNegativeInteger (int value) {
    super(value);
  }
 
  protected String negateValueAsString () {
    String valueAsString = String.valueOf (value());
    return valueAsString.substring(1);
  }
}
 
 
 
public class NegatableZeroInteger extends NegatableInteger {
  public NegatableZeroInteger (int value) {
    super(value);
  }
 
  protected String negateValueAsString () {
    return String.valueOf (value());
  }
}</pre>
<p> </p>
<p>这个贴子基本上就是两页，好像不算太严重，如果你这样想的话，你就大错特错了。这个贴子被人转到了reddit.com，于是一发不可收拾，在上面的回贴达到了490多条。链接如下：</p>
<p><a href="http://www.reddit.com/r/programming/comments/9egb6/programming_is_hard/" target="_blank">http://www.reddit.com/r/programming/comments/9egb6/programming_is_hard/</a></p>
<p>有人说，要用try catch；有人说要使用XML配置文件……，程序员们在追逐更为变态和疯狂的东西，并从中找到快乐，呵呵。</p>
<p>看完后，正如reddit.com所说——“<strong>编程好难啊</strong>”！</p>
<p>无独有偶，这并不是第一次，也不会是最后一次，让我们看看在PHP的官网上发生的类似的一幕——讨论PHP的abs取绝对值函数的函数说明文档中的回复：</p>
<p><a href="http://us.php.net/manual/en/function.abs.php#58508" target="_blank">http://us.php.net/manual/en/function.abs.php#58508</a></p>
<p>又是一个长贴，还带着很多性能分析，真的很好很强大！</p>
<p>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/4758.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/30.jpg" alt="如何写出无法维护的代码" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4758.html" class="wp_rp_title">如何写出无法维护的代码</a></li><li ><a href="https://coolshell.cn/articles/1992.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/12/language-fanboys-150x150.jpg" alt="程序员眼中的编程语言" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1992.html" class="wp_rp_title">程序员眼中的编程语言</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/18024.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2017/07/api-design-300x278-2-150x150.jpg" alt="API设计原则 &#8211; Qt官网的设计实践总结" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18024.html" class="wp_rp_title">API设计原则 &#8211; Qt官网的设计实践总结</a></li><li ><a href="https://coolshell.cn/articles/8088.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="对技术的态度" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8088.html" class="wp_rp_title">对技术的态度</a></li><li ><a href="https://coolshell.cn/articles/5444.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/14.jpg" alt="千万不要把 bool 设计成函数参数" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5444.html" class="wp_rp_title">千万不要把 bool 设计成函数参数</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1391.html">编程真难啊</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1391.html/feed</wfw:commentRss>
			<slash:comments>104</slash:comments>
		
		
			</item>
		<item>
		<title>十个Web开发文章和教程</title>
		<link>https://coolshell.cn/articles/1387.html</link>
					<comments>https://coolshell.cn/articles/1387.html#respond</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Mon, 31 Aug 2009 09:18:41 +0000</pubDate>
				<category><![CDATA[Web开发]]></category>
		<category><![CDATA[技术读物]]></category>
		<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[正则表达式]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1387</guid>

					<description><![CDATA[<p>下面是十个在2009年8月份里出现的十个非常不错的Web开发方面的文章和教程。推荐给大家，当然，都是英文啦。如果你愿意，欢迎翻译后提交给酷壳。 1）一个简单的L...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1387.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1387.html">十个Web开发文章和教程</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>下面是十个在2009年8月份里出现的十个非常不错的Web开发方面的文章和教程。推荐给大家，当然，都是英文啦。如果你愿意，欢迎翻译后提交给<a href="https://coolshell.cn" target="_blank">酷壳</a>。</p>
<p>1）<a href="http://www.queness.com/post/530/simple-lava-lamp-menu-tutorial-with-jquery" target="_blank">一个简单的Lava 灯式的菜单</a>（使用jQuery完成）</p>
<p>2）<a href="http://www.jankoatwarpspeed.com/post/2009/08/20/Table-of-contents-using-jQuery.aspx" target="_blank">使用jQuery自动生成文章内容的目录</a>。就像是使用Word一样，设置一下标题，然后可以自动生成文章的目录。</p>
<p>3）<a href="http://www.queness.com/post/484/create-a-thumbnail-gallery-with-slick-heading-and-caption-effect-with-jquery" target="_blank">使用jQuery为图片创建图片标题和描述</a>。这是一个超Cool的效果，当你的鼠标移到图片上的时候，图片的上下会出现遮覆，上面是标题，下面是描述，相当不错的用户体验，当鼠标移开后，遮覆消失。</p>
<p><span id="more-1387"></span></p>
<p>4）<a href="http://net.tutsplus.com/videos/screencasts/a-crash-course-in-advanced-css3-effects/" target="_blank">CSS3速成教程</a>。主要讨论了CSS3的这些特性：旋转和改变大小，动画，Photoshop风格的遮罩，图片倒影，色彩渐变，转换等。有一个不错的flash视频。</p>
<p>5）<a href="http://www.hongkiat.com/blog/30-new-useful-wordpress-tricks-hacks/" target="_blank">30+相当有用的Wordpress的巧门</a>。相当相当不错的一些和Wordpress相关的插件和小巧门，非常非常地实用。</p>
<p>6）<a href="http://www.noupe.com/php/htaccess-techniques.html" target="_blank">htaccess技术的权威性指南</a>。本文给出了12个非常有用的apache的设置，可以让你更容易设置你的站点，在这篇文章的最后，还列出了一些经验上的东西。另外，你可以参考本站的《<a rel="bookmark" href="https://coolshell.cn/articles/1035.html">16个简单实用的.htaccess小贴示</a>》。</p>
<p>7）<a href="http://www.noupe.com/php/php-regular-expressions.html" target="_blank">PHP正则表达式入门</a>。一个相当不错的入门教程，写得简单易懂。</p>
<p>8）<a href="http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/" target="_blank">你需要知道的8个正则表达式</a>。正则表达式很有用，但是它具体用在什么地方呢？这篇文章给你了一票非常实用的示例。相当的不错。浏览这篇文章时别忘了看一下大家的回复，那里面也有很多不错的资源。</p>
<p>9）<a href="http://speckyboy.com/2009/08/26/20-jquery-plugins-and-tutorials-to-enhance-forms/" target="_blank">20个可以改进表单的jQuery插件</a>。都是相当实用的插件，可以让你的Web表单相当的成熟和有很好的用户体验。</p>
<p>10）<a href="http://css-tricks.com/inapproprite-uses/" target="_blank">数据库，HTML，CSS，JS不适应的用法</a>。很不错的文章，你需要记住下面的这个表格。</p>
<div style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; OVERFLOW: hidden; PADDING-TOP: 5px; BORDER-BOTTOM: #cccccc 1px solid">
<div style="display: block; float: left; width: 200px; text-align: right;">Database</div>
<div><em style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 0pt; PADDING-TOP: 0pt">is for</em>content</div>
</div>
<div style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; OVERFLOW: hidden; PADDING-TOP: 5px; BORDER-BOTTOM: #cccccc 1px solid">
<div style="display: block; float: left; width: 200px; text-align: right;">HTML</div>
<div><em style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 0pt; PADDING-TOP: 0pt">is for</em>describing and displaying content</div>
</div>
<div style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; OVERFLOW: hidden; PADDING-TOP: 5px; BORDER-BOTTOM: #cccccc 1px solid">
<div style="display: block; float: left; width: 200px; text-align: right;">CSS</div>
<div><em style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 0pt; PADDING-TOP: 0pt">is for</em>design</div>
</div>
<div style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; OVERFLOW: hidden; PADDING-TOP: 5px; BORDER-BOTTOM: #cccccc 1px solid">
<div style="display: block; float: left; width: 200px; text-align: right;">JavaScript</div>
<div><em style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 0pt; PADDING-TOP: 0pt">is for</em>functionality</div>
</div>
<p>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" ><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/8170.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/08/ajax_error-150x150.jpg" alt="一次Ajax查错的经历" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8170.html" class="wp_rp_title">一次Ajax查错的经历</a></li><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/09/image008-150x150.jpg" alt="一些文章和各种资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_title">一些文章和各种资源</a></li><li ><a href="https://coolshell.cn/articles/5160.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/08/Pagination-e1312791884744-150x150.jpg" alt="PHP分页技术的代码和示例" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5160.html" class="wp_rp_title">PHP分页技术的代码和示例</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1387.html">十个Web开发文章和教程</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1387.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
