{"id":23094,"date":"2024-11-21T10:19:32","date_gmt":"2024-11-21T02:19:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23094\/"},"modified":"2024-11-21T10:19:32","modified_gmt":"2024-11-21T02:19:32","slug":"thinkphp%e6%a8%a1%e6%9d%bf%e6%89%a7%e8%a1%8c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23094\/","title":{"rendered":"thinkphp\u6a21\u677f\u6267\u884c\u65b9\u6cd5"},"content":{"rendered":"<p>thinkphp\u662f\u4e00\u6b3e\u6d41\u884c\u7684php\u6846\u67b6\u3002\u5b83\u62e5\u6709\u5f3a\u5927\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u8be5\u6a21\u677f\u5f15\u64ce\u53ef\u5b9e\u73b0php\u63a7\u5236\u5668\u4e0ehtml\u89c6\u56fe\u7684\u5206\u79bb\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecdthinkphp\u4e2d\u6a21\u677f\u5f15\u64ce\u5e38\u7528\u7684\u51e0\u79cd\u6267\u884c\u65b9\u6cd5\u3002<\/p>\n<p>\u4e00\u3001 \u4f20\u9012\u53d8\u91cf<\/p>\n<p>\u4f20\u9012\u53d8\u91cf\u662f\u6a21\u677f\u5f15\u64ce\u7684\u57fa\u7840\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06PHP\u4e2d\u7684\u53d8\u91cf\u5728\u6a21\u677f\u4e2d\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<p>\u5728PHP\u63a7\u5236\u5668\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf $name\uff1a<\/p>\n<pre>$name = 'John Doe';\n$this-&amp;gt;assign('name', $name);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728HTML\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6a21\u677f\u6807\u7b7e\u5c06\u53d8\u91cf\u8f93\u51fa\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\n\n    &lt;title&gt;Hello &lt;?php echo ($name); ?&gt;\n&lt;\/title&gt;&lt;h1&gt;Hello &lt;?php echo ($name); ?&gt;\n&lt;\/h1&gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u6a21\u677f\u5f15\u64ce\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u5373 $name \u4e3a\u6a21\u677f\u6807\u7b7e\u4e2d\u7684\u53d8\u91cf\u540d\uff0c($name) \u4e3a\u8f93\u51fa\u8be5\u53d8\u91cf\u7684\u8bed\u6cd5\u3002<\/p>\n<p>\u4e8c\u3001 \u5faa\u73af\u8f93\u51fa<\/p>\n<p>\u5faa\u73af\u8f93\u51fa\u662f\u6a21\u677f\u5f15\u64ce\u4e2d\u5e38\u7528\u7684\u8bed\u6cd5\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 foreach \u5faa\u73af\u8f93\u51faPHP\u4e2d\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5728PHP\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4\uff1a<\/p>\n<pre>$data = array(\n    array('name'=&amp;gt;'John Doe', 'age'=&amp;gt;20),\n    array('name'=&amp;gt;'Jane Doe', 'age'=&amp;gt;18)\n);\n$this-&amp;gt;assign('data', $data);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728HTML\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 foreach \u5faa\u73af\u4f9d\u6b21\u8f93\u51fa\u6570\u7ec4\uff1a<\/p>\n<pre>\n\n    &lt;title&gt;Student List&lt;\/title&gt;&lt;h1&gt;Student List&lt;\/h1&gt;\n    <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> &#8211; <\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u6a21\u677f\u5f15\u64ce\u4e2d\u7684\u4e24\u4e2a\u8bed\u53e5\uff0cforeach \u548c endforeach\uff0c\u5728\u5faa\u73af\u8bed\u53e5\u4e2d\u4f7f\u7528 $item \u4ee3\u8868\u5faa\u73af\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u4e09\u3001 \u6761\u4ef6\u5224\u65ad<\/p>\n<p>\u9664\u4e86\u5faa\u73af\u8bed\u53e5\uff0c\u6a21\u677f\u5f15\u64ce\u8fd8\u652f\u6301\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u3002\u53ef\u4ee5\u6839\u636ePHP\u4e2d\u53d8\u91cf\u7684\u503c\u8f93\u51fa\u4e0d\u540c\u7684HTML\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<p>\u5728PHP\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf $is_valid\uff1a<\/p>\n<pre>$is_valid = true;\n$this-&amp;gt;assign('is_valid', $is_valid);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728HTML\u89c6\u56fe\u4e2d\uff0c\u4f7f\u7528 if-else \u5224\u65ad\u53d8\u91cf\u503c\uff0c\u5e76\u8f93\u51fa\u4e0d\u540c\u7684HTML\u5185\u5bb9\uff1a<\/p>\n<pre>\n\n    &lt;title&gt;Welcome&lt;\/title&gt;&lt;?php if($is_valid): ?&gt;&lt;h1&gt;Welcome&lt;\/h1&gt;\n    &lt;?php else: ?&gt;&lt;h1&gt;Access Denied&lt;\/h1&gt;\n    &lt;?php endif; ?&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u6a21\u677f\u5f15\u64ce\u4e2d\u7684 if-else \u8bed\u53e5\u548c endif \u7ed3\u675f\u8bed\u53e5\uff0c\u6839\u636e\u53d8\u91cf $is_valid \u7684\u503c\u8f93\u51fa\u4e0d\u540c\u7684\u6807\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u662fThinkPHP\u6a21\u677f\u5f15\u64ce\u4e2d\u5e38\u7528\u7684\u51e0\u79cd\u6267\u884c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002\u6a21\u677f\u5f15\u64ce\u7684\u4f7f\u7528\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\uff0c\u5efa\u8bae\u5f00\u53d1\u4eba\u5458\u591a\u52a0\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u6a21\u677f\u6267\u884c\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>thinkphp\u662f\u4e00\u6b3e\u6d41\u884c\u7684php\u6846\u67b6\u3002\u5b83\u62e5\u6709\u5f3a\u5927\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u8be5\u6a21\u677f\u5f15\u64ce\u53ef\u5b9e\u73b0php\u63a7\u5236\u5668\u4e0ehtml\u89c6\u56fe\u7684\u5206\u79bb\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecdthinkphp\u4e2d\u6a21\u677f\u5f15\u64ce\u5e38\u7528\u7684\u51e0\u79cd\u6267\u884c\u65b9\u6cd5\u3002 \u4e00\u3001 \u4f20\u9012\u53d8\u91cf \u4f20\u9012\u53d8\u91cf\u662f\u6a21\u677f\u5f15\u64ce\u7684\u57fa\u7840\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06PHP\u4e2d\u7684\u53d8\u91cf\u5728\u6a21\u677f\u4e2d\u8f93\u51fa\u3002\u4f8b\u5982\uff1a \u5728PHP\u63a7\u5236\u5668\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf $name\uff1a $name = &#8216;John Doe&#8217;; $this-&amp;gt;assign(&#8216;name&#8217;, $name); \u767b\u5f55\u540e\u590d\u5236 \u5728HTML\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6a21\u677f\u6807\u7b7e\u5c06\u53d8\u91cf\u8f93\u51fa\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b &lt;title&gt;Hello &lt;?php echo ($name); ?&gt; &lt;\/title&gt;&lt;h1&gt;Hello &lt;?php echo ($name); ?&gt; &lt;\/h1&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u4f7f\u7528\u4e86\u6a21\u677f\u5f15\u64ce\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u5373 $name \u4e3a\u6a21\u677f\u6807\u7b7e\u4e2d\u7684\u53d8\u91cf\u540d\uff0c($name) \u4e3a\u8f93\u51fa\u8be5\u53d8\u91cf\u7684\u8bed\u6cd5\u3002 \u4e8c\u3001 \u5faa\u73af\u8f93\u51fa \u5faa\u73af\u8f93\u51fa\u662f\u6a21\u677f\u5f15\u64ce\u4e2d\u5e38\u7528\u7684\u8bed\u6cd5\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 foreach \u5faa\u73af\u8f93\u51faPHP\u4e2d\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a \u5047\u8bbe\u6211\u4eec\u5728PHP\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4\uff1a $data = array( array(&#8216;name&#8217;=&amp;gt;&#8217;John Doe&#8217;, &#8216;age&#8217;=&amp;gt;20), array(&#8216;name&#8217;=&amp;gt;&#8217;Jane Doe&#8217;, &#8216;age&#8217;=&amp;gt;18) ); $this-&amp;gt;assign(&#8216;data&#8217;, $data); \u767b\u5f55\u540e\u590d\u5236 \u5728HTML\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 foreach \u5faa\u73af\u4f9d\u6b21\u8f93\u51fa\u6570\u7ec4\uff1a &lt;title&gt;Student List&lt;\/title&gt;&lt;h1&gt;Student [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23094","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23094"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23094\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}