{"id":1834,"date":"2026-03-03T10:34:23","date_gmt":"2026-03-03T02:34:23","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1834"},"modified":"2026-03-03T10:34:23","modified_gmt":"2026-03-03T02:34:23","slug":"php%e5%8e%9f%e7%94%9f%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e-%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/03\/03\/php%e5%8e%9f%e7%94%9f%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e-%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e\/","title":{"rendered":"PHP\u539f\u751f\u6a21\u677f\u5f15\u64ce \u6700\u7b80\u5355\u7684\u6a21\u677f\u5f15\u64ce"},"content":{"rendered":"\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u7528 <strong>ThinkPHP 5<\/strong>\u3001<strong>Laravel<\/strong> \u8fd9\u4e9b\u81ea\u5e26\u6a21\u677f\u7cfb\u7edf\uff0c\u53ea\u60f3\u7528 <strong>PHP \u539f\u751f\u5199\u4e00\u4e2a\u201c\u6700\u7b80\u5355\u7684\u6a21\u677f\u5f15\u64ce\u201d<\/strong>\uff0c\u5176\u5b9e\u975e\u5e38\u5bb9\u6613\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4e2a\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u4ec5 50 \u884c<br>\u2705 \u652f\u6301\u53d8\u91cf\u66ff\u6362<br>\u2705 \u652f\u6301 if<br>\u2705 \u652f\u6301 foreach<br>\u2705 \u65e0\u4f9d\u8d56<br>\u2705 \u9002\u5408\u5b66\u4e60\u539f\u7406<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u6700\u6700\u7b80\u5355\u7248\u672c\uff08\u53d8\u91cf\u66ff\u6362\u7248\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u677f\u6587\u4ef6 <code>view.html<\/code><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;h1&gt;{{title}}&amp;lt;\/h1&gt;\n&amp;lt;p&gt;\u6b22\u8fce\u4f60\uff0c{{name}}&amp;lt;\/p&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u677f\u5f15\u64ce\u7c7b <code>Template.php<\/code><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\n\nclass Template\n{\n    protected $vars = &#x5B;];\n\n    public function assign($key, $value)\n    {\n        $this-&gt;vars&#x5B;$key] = $value;\n    }\n\n    public function render($file)\n    {\n        $content = file_get_contents($file);\n\n        foreach ($this-&gt;vars as $key =&gt; $value) {\n            $content = str_replace(&#039;{{&#039;.$key.&#039;}}&#039;, $value, $content);\n        }\n\n        echo $content;\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u65b9\u5f0f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrequire &#039;Template.php&#039;;\n\n$tpl = new Template();\n$tpl-&gt;assign(&#039;title&#039;, &#039;\u6211\u7684\u9996\u9875&#039;);\n$tpl-&gt;assign(&#039;name&#039;, &#039;\u5f20\u4e09&#039;);\n$tpl-&gt;render(&#039;view.html&#039;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u5347\u7ea7\u7248\uff08\u652f\u6301 if \/ foreach\uff09<\/h1>\n\n\n\n<p>\u6211\u4eec\u505a\u4e00\u4e2a\u201c\u8d85\u8f7b\u91cf\u7f16\u8bd1\u578b\u6a21\u677f\u5f15\u64ce\u201d\u3002<\/p>\n\n\n\n<p>\u539f\u7406\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u628a\u6a21\u677f\u8bed\u6cd5\u8f6c\u6362\u6210 PHP \u4ee3\u7801\uff0c\u7136\u540e include \u6267\u884c\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u6a21\u677f\u6587\u4ef6 <code>view.html<\/code><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;h1&gt;{$title}&amp;lt;\/h1&gt;\n\n{if $age &gt;= 18}\n&amp;lt;p&gt;\u4f60\u662f\u6210\u5e74\u4eba&amp;lt;\/p&gt;\n{else}\n&amp;lt;p&gt;\u4f60\u662f\u672a\u6210\u5e74&amp;lt;\/p&gt;\n{\/if}\n\n&amp;lt;ul&gt;\n{foreach $list as $item}\n&amp;lt;li&gt;{$item}&amp;lt;\/li&gt;\n{\/foreach}\n&amp;lt;\/ul&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u6a21\u677f\u5f15\u64ce<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\n\nclass Template\n{\n    protected $vars = &#x5B;];\n    protected $cachePath = &#039;cache\/&#039;;\n\n    public function assign($key, $value)\n    {\n        $this-&gt;vars&#x5B;$key] = $value;\n    }\n\n    public function render($file)\n    {\n        $cacheFile = $this-&gt;cachePath . md5($file) . &#039;.php&#039;;\n\n        \/\/ \u5982\u679c\u7f13\u5b58\u4e0d\u5b58\u5728\u6216\u6a21\u677f\u66f4\u65b0\n        if (!file_exists($cacheFile) || filemtime($file) &gt; filemtime($cacheFile)) {\n            $content = file_get_contents($file);\n            $compiled = $this-&gt;compile($content);\n            file_put_contents($cacheFile, $compiled);\n        }\n\n        extract($this-&gt;vars);\n        include $cacheFile;\n    }\n\n    protected function compile($content)\n    {\n        $content = preg_replace(&#039;\/\\{\\$(.*?)\\}\/&#039;, &#039;&amp;lt;?php echo $1; ?&gt;&#039;, $content);\n\n        $content = preg_replace(&#039;\/\\{if (.*?)\\}\/&#039;, &#039;&amp;lt;?php if($1): ?&gt;&#039;, $content);\n        $content = preg_replace(&#039;\/\\{else\\}\/&#039;, &#039;&amp;lt;?php else: ?&gt;&#039;, $content);\n        $content = preg_replace(&#039;\/\\{\\\/if\\}\/&#039;, &#039;&amp;lt;?php endif; ?&gt;&#039;, $content);\n\n        $content = preg_replace(&#039;\/\\{foreach (.*?)\\}\/&#039;, &#039;&amp;lt;?php foreach($1): ?&gt;&#039;, $content);\n        $content = preg_replace(&#039;\/\\{\\\/foreach\\}\/&#039;, &#039;&amp;lt;?php endforeach; ?&gt;&#039;, $content);\n\n        return $content;\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 \u4f7f\u7528<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$tpl = new Template();\n\n$tpl-&gt;assign(&#039;title&#039;, &#039;\u6d4b\u8bd5\u9875\u9762&#039;);\n$tpl-&gt;assign(&#039;age&#039;, 20);\n$tpl-&gt;assign(&#039;list&#039;, &#x5B;&#039;\u82f9\u679c&#039;, &#039;\u9999\u8549&#039;, &#039;\u6a59\u5b50&#039;]);\n\n$tpl-&gt;render(&#039;view.html&#039;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u8fd0\u884c\u539f\u7406\u56fe<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u6a21\u677f\u6587\u4ef6\n   \u2193\n\u6b63\u5219\u66ff\u6362\n   \u2193\n\u751f\u6210 PHP \u6587\u4ef6\n   \u2193\ninclude \u6267\u884c\n   \u2193\n\u8f93\u51fa HTML\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u5c31\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Smarty<br>Blade<br>ThinkPHP \u6a21\u677f<\/p>\n<\/blockquote>\n\n\n\n<p>\u7684\u6838\u5fc3\u539f\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001\u5b89\u5168\u52a0\u5f3a\u7248\uff08\u9632 XSS\uff09<\/h1>\n\n\n\n<p>\u4e0a\u9762\u7684\u53d8\u91cf\u8f93\u51fa\u662f\u4e0d\u5b89\u5168\u7684\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php echo $name; ?&gt;\n\n<\/pre><\/div>\n\n\n<p>\u5e94\u8be5\u6539\u6210\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php echo htmlspecialchars($name, ENT_QUOTES, &#039;UTF-8&#039;); ?&gt;\n\n<\/pre><\/div>\n\n\n<p>\u4fee\u6539\u7f16\u8bd1\u89c4\u5219\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$content = preg_replace(\n    &#039;\/\\{\\$(.*?)\\}\/&#039;,\n    &#039;&amp;lt;?php echo htmlspecialchars($1, ENT_QUOTES, &quot;UTF-8&quot;); ?&gt;&#039;,\n    $content\n);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u518d\u8fdb\u9636\u4f60\u53ef\u4ee5\u52a0<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>\u505a\u6cd5<\/th><\/tr><\/thead><tbody><tr><td>\u6a21\u677f\u7ee7\u627f<\/td><td>block \u89e3\u6790<\/td><\/tr><tr><td>include \u5b50\u6a21\u677f<\/td><td>include \u7f16\u8bd1<\/td><\/tr><tr><td>layout \u5e03\u5c40<\/td><td>\u5148\u89e3\u6790\u5185\u5bb9\u5757<\/td><\/tr><tr><td>\u7f13\u5b58\u65f6\u95f4<\/td><td>\u5224\u65ad filemtime<\/td><\/tr><tr><td>\u81ea\u5b9a\u4e49\u51fd\u6570<\/td><td>\u6ce8\u518c\u51fd\u6570\u8868<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516d\u3001\u5982\u679c\u4f60\u53ea\u662f\u60f3\u8981\u201c\u6700\u7b80\u5355\u201d<\/h1>\n\n\n\n<p>\u5176\u5b9e\u6700\u7b80\u5355\u5c31\u662f\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nextract($data);\ninclude &#039;view.php&#039;;\n\n<\/pre><\/div>\n\n\n<p>\u76f4\u63a5\u5728 view.php \u91cc\u5199\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;h1&gt;&amp;lt;?= $title ?&gt;&amp;lt;\/h1&gt;\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u5c31\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>PHP \u672c\u8eab\u5c31\u662f\u6a21\u677f\u5f15\u64ce\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e03\u3001\u5bf9\u6bd4\u4e3b\u6d41\u6a21\u677f\u5f15\u64ce<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5f15\u64ce<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td><strong>Smarty<\/strong><\/td><td>\u7ecf\u5178\u6a21\u677f\u5f15\u64ce<\/td><\/tr><tr><td><strong>Twig<\/strong><\/td><td>\u5b89\u5168\u4e25\u683c<\/td><\/tr><tr><td><strong>Blade<\/strong><\/td><td>Laravel \u4e13\u7528<\/td><\/tr><tr><td>\u539f\u751f\u81ea\u5199<\/td><td>\u6700\u8f7b\u91cf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516b\u3001\u7ed3\u8bba<\/h1>\n\n\n\n<p>\u5982\u679c\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c0f\u9879\u76ee \u2192 \u539f\u751f include \u5c31\u591f<\/li>\n\n\n\n<li>\u4e2d\u578b\u9879\u76ee \u2192 \u81ea\u5199\u7f16\u8bd1\u578b\u6a21\u677f<\/li>\n\n\n\n<li>\u5927\u9879\u76ee \u2192 \u7528\u6210\u719f\u6a21\u677f\u5f15\u64ce<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5982\u679c\u4f60\u613f\u610f\uff0c\u6211\u53ef\u4ee5\u7ed9\u4f60\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd25 100 \u884c\u5b9e\u73b0\u4e00\u4e2a\u201c\u7c7b Smarty\u201d\u6a21\u677f\u5f15\u64ce<\/li>\n\n\n\n<li>\ud83d\udd25 \u652f\u6301\u6a21\u677f\u7ee7\u627f\u7684\u5b8c\u6574\u7248<\/li>\n\n\n\n<li>\ud83d\udd25 \u5e26\u7f13\u5b58\u4f18\u5316\u7684\u751f\u4ea7\u7ea7\u6a21\u677f<\/li>\n\n\n\n<li>\ud83d\udd25 MVC \u6846\u67b6\u6700\u5c0f\u5b9e\u73b0\uff08\u81ea\u5199 PHP \u6846\u67b6\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u662f\u60f3\u5b66\u4e60\u539f\u7406\uff0c\u8fd8\u662f\u60f3\u505a\u4e00\u4e2a\u53ef\u5546\u7528\u7684\u8f7b\u91cf\u6846\u67b6\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u4e0d\u60f3\u7528 ThinkPHP 5\u3001Laravel \u8fd9\u4e9b\u81ea\u5e26\u6a21\u677f\u7cfb\u7edf\uff0c\u53ea\u60f3\u7528 P&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/03\/03\/php%e5%8e%9f%e7%94%9f%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e-%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1834","post","type-post","status-publish","format-standard","hentry","category-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1834"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1834\/revisions"}],"predecessor-version":[{"id":1835,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1834\/revisions\/1835"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}