<style id="zni6e"></style>
\n border=\"1\" style=\"width: 80%;\">\n \n \n \n {% for product in products %}\n \n
Product<\/td>\n Description<\/td>\n Value<\/td>\n Date<\/td>\n <\/tr>\n <\/thead>\n
{{ product.name }}<\/td>\n {{ product.description }}<\/td>\n {{ product.value }}<\/td>\n {{ product.date_register|date(\"m\/d\/Y\") }}<\/td>\n <\/tr>\n {% endfor %}\n <\/tbody>\n <\/table>\n <\/body>\n<\/html><\/pre>\n

At this point, we still have the same page, but we reduce its complexity by decoupling the context blocks. <\/p>\n

Cache<\/strong><\/p>

Environment<\/code>Objects can not only be used to load templates. If we pass using the cache<\/code> option of the associated directory, Twig will cache the compiled template, thus avoiding parsing the template in subsequent requests. The compiled template will be stored in the directory we provide. Note that this is the cache for the compiled templates, not the cache for the evaluated templates. This means that Twig will parse, compile and save the template file. All subsequent requests still require evaluation templates, but the first step is already done for you. Let's cache the template in the example by editing the bootstrap.php<\/code> file: <\/p>\n

 Hello \" . $name . \"<\/p>\"; ?><\/pre>\n

(The following content is similar to the original text, but some statement adjustments and paragraph divisions have been made, and the image position remains unchanged) <\/strong><\/p>\n

Cycle<\/strong><\/p>\n

In our example, we have seen how to loop with Twig. Basically, we use the for<\/code> tag and assign an alias to each element in the specified array. In this case, we assign an alias to the products<\/code> array. After that, we can use the product<\/code> operator to access all properties in each array element. We use the .<\/code> tag to indicate the end of the loop. We can also loop through numbers or letters using the endfor<\/code> operator. As shown below: ..<\/code>\n<\/p>\n

Hello {{ name }}<\/p><\/pre> or letter:

\n<\/p>\n

composer require twig\/twig<\/pre>This operator is just the syntax sugar of the 

function, and it works the same way as the native PHPrange<\/code> function. An equally useful option is to add conditions to the loop. Using conditions, we can filter the elements to iterate. Suppose we want to iterate over all products with a value less than 250: range<\/code>\n<\/p>\n

Conditional statement<\/strong>\n<\/p>Twig also provides conditional statements in the form of

, if<\/code>, elseif<\/code> and if not<\/code> tags. Just like in any programming language, we can use these tags to filter conditions in templates. Suppose in our example, we want to display only products with a value above 500: else<\/code>\n<\/p>\n

 'Notebook',\n        'description'   => 'Core i7',\n        'value'         =>  800.00,\n        'date_register' => '2017-06-22',\n    ],\n    [\n        'name'          => 'Mouse',\n        'description'   => 'Razer',\n        'value'         =>  125.00,\n        'date_register' => '2017-10-25',\n    ],\n    [\n        'name'          => 'Keyboard',\n        'description'   => 'Mechanical Keyboard',\n        'value'         =>  250.00,\n        'date_register' => '2017-06-23',\n    ],\n];\n\n\/\/ 渲染我們的視圖\necho $twig->render('index.html', ['products' => $products] );<\/pre>

Filter<\/strong>\n<\/p> Filters allow us to filter the information passed to the template and the format of the information displayed. Let's take a look at some of the most commonly used and important filters. A complete list of Twig filters can be found here.

\n<\/p>Date and

date_modify<\/code>\n<\/h3>

Filter formats the date to the given format. As we see in the example: date<\/code>\n<\/p>\n

\n\n    \n        \n        Twig Example<\/title>\n    <\/head>\n    <body>
<h1><a href="http://ipnx.cn/">亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱</a></h1>\n    <table> border=\"1\" style=\"width: 80%;\">\n        <thead>\n            <tr>\n                <td>Product<\/td>\n                <td>Description<\/td>\n                <td>Value<\/td>\n                <td>Date<\/td>\n            <\/tr>\n        <\/thead>\n        <tbody>\n            {% for product in products %}\n                <tr>\n                    <td>{{ product.name }}<\/td>\n                    <td>{{ product.description }}<\/td>\n                    <td>{{ product.value }}<\/td>\n                    <td>{{ product.date_register|date(\"m\/d\/Y\") }}<\/td>\n                <\/tr>\n            {% endfor %}\n        <\/tbody>\n    <\/table>\n    <\/body>\n<\/html><\/pre>We display dates in month\/day\/year format. In addition to the <p> filter, we can also use the <code>date<\/code> filter to change the date using the <code>date_modify<\/code> filter. For example, if we want to add a day to a date, we can use the following: <\/p>\n<pre class='brush:php;toolbar:false;'><!DOCTYPE html>\n<html lang=\"pt-BR\">\n    <head>\n        <meta charset=\"UTF-8\">\n        <title>Tutorial Example<\/title>\n    <\/head>\n    <body>\n        {% block content %}\n        {% endblock %}\n    <\/body>\n<\/html><\/pre>\n<h3><code>format<\/code><\/h3>\n<p>Format the given string by replacing all placeholders. For example: <\/p>\n<pre class='brush:php;toolbar:false;'>{% extends \"layout.html\" %}\n\n{% block content %}\n    <table> border=\"1\" style=\"width: 80%;\">\n        <thead>\n            <tr>\n                <td>Product<\/td>\n                <td>Description<\/td>\n                <td>Value<\/td>\n                <td>Date<\/td>\n            <\/tr>\n        <\/thead>\n        <tbody>\n            {% for product in products %}\n                <tr>\n                    <td>{{ product.name }}<\/td>\n                    <td>{{ product.description }}<\/td>\n                    <td>{{ product.value }}<\/td>\n                    <td>{{ product.date_register|date(\"m\/d\/Y\") }}<\/td>\n                <\/tr>\n            {% endfor %}\n        <\/tbody>\n    <\/table>\n{% endblock %}<\/pre>\n<h3><code>striptags<\/code><\/h3>\n<p><code>striptags<\/code> The filter removes SGML\/XML tags and replaces adjacent spaces with spaces: <\/p><pre class='brush:php;toolbar:false;'><?php echo \"<p> Hello \" . $name . \"<\/p>\"; ?><\/pre>\n<h3><code>escape<\/code><\/h3>\n<p><code>escape<\/code> is one of the most important filters. It filters the string to insert safely into the final output. By default, it uses HTML escape policy, so <\/p>\n<pre class='brush:php;toolbar:false;'><p>Hello {{ name }}<\/p><\/pre>\n<p>equivalent to <\/p>\n<pre class='brush:php;toolbar:false;'>composer require twig\/twig<\/pre>\n<p><code>js<\/code>, <code>css<\/code>, <code>url<\/code>, <code>html_attr<\/code> and <\/p> escape policies are also available. They are Javascript, CSS, URI, and HTML attribute context escape strings, respectively. <p>\n<strong><\/strong>Debug<\/p><p>\n<code>dump()<\/code> Finally, let's take a look at debugging. Sometimes we need to access all the information of the template variable. To do this, Twig has a <code>Twig_Extension_Debug<\/code> function. This function is not available by default. When creating a Twig environment, we have to add the <\/p> extension: <pre class='brush:php;toolbar:false;'><?php\n\/\/ 加載我們的自動(dòng)加載器\nrequire_once __DIR__.'\/vendor\/autoload.php';\n\n\/\/ 指定我們的Twig模板位置\n$loader = new Twig_Loader_Filesystem(__DIR__.'\/templates');\n\n\/\/ 實(shí)例化我們的Twig\n$twig = new Twig_Environment($loader);<\/pre>\n<p>\n<code>dump()<\/code>This step is necessary so that we do not accidentally leak debug information on the production server. Once the configuration is complete, we simply use the <\/p> function to dump all information about the template variables. <pre class='brush:php;toolbar:false;'><?php\nrequire_once __DIR__.'\/bootstrap.php';\n\n\/\/ 創(chuàng)建產(chǎn)品列表\n$products = [\n    [\n        'name'          => 'Notebook',\n        'description'   => 'Core i7',\n        'value'         =>  800.00,\n        'date_register' => '2017-06-22',\n    ],\n    [\n        'name'          => 'Mouse',\n        'description'   => 'Razer',\n        'value'         =>  125.00,\n        'date_register' => '2017-10-25',\n    ],\n    [\n        'name'          => 'Keyboard',\n        'description'   => 'Mechanical Keyboard',\n        'value'         =>  250.00,\n        'date_register' => '2017-06-23',\n    ],\n];\n\n\/\/ 渲染我們的視圖\necho $twig->render('index.html', ['products' => $products] );<\/pre>\n<p>\n<strong><\/strong>Conclusion<\/p>\n<p>\n<\/p>I hope this article will provide you with a solid foundation for Twig basics and start your project right away! If you want to have a deeper look at Twig, the official website provides very good documentation and references that you can check out. Do you use the template engine? What do you think of Twig? Would you compare it to popular alternatives like Blade or Smarty? <p>\n<strong><\/strong> (The following content is FAQ, the original text has been included, omitted here) <\/p>"}	</script>
	
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<script>var V_PATH="/";window.onerror=function(){ return true; };</script>
</head>

<body data-commit-time="2023-12-28T14:50:12+08:00" class="editor_body body2_2">
	<link rel="stylesheet" type="text/css" href="/static/csshw/stylehw.css">
<header>
    <div   id="wjcelcm34c"   class="head">
        <div   id="wjcelcm34c"   class="haed_left">
            <div   id="wjcelcm34c"   class="haed_logo">
                <a href="http://ipnx.cn/" title="" class="haed_logo_a">
                    <img src="/static/imghw/logo.png" alt="" class="haed_logoimg">
                </a>
            </div>
            <div   id="wjcelcm34c"   class="head_nav">
                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="Community" class="head_nava head_nava-template1">Community</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/article.html" title="Articles" class="languagechoosea on">Articles</a>
                            <a href="http://ipnx.cn/faq/zt" title="Topics" class="languagechoosea">Topics</a>
                            <a href="http://ipnx.cn/wenda.html" title="Q&A" class="languagechoosea">Q&A</a>
                        </div>
                    </div>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="Learn" class="head_nava head_nava-template1_1">Learn</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1_1" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/course.html" title="Course" class="languagechoosea on">Course</a>
                            <a href="http://ipnx.cn/dic/" title="Programming Dictionary" class="languagechoosea">Programming Dictionary</a>
                        </div>
                    </div>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="Tools Library" class="head_nava head_nava-template1_2">Tools Library</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1_2" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/toolset/development-tools" title="Development tools" class="languagechoosea on">Development tools</a>
                            <a href="http://ipnx.cn/toolset/website-source-code" title="Website Source Code" class="languagechoosea">Website Source Code</a>
                            <a href="http://ipnx.cn/toolset/php-libraries" title="PHP Libraries" class="languagechoosea">PHP Libraries</a>
                            <a href="http://ipnx.cn/toolset/js-special-effects" title="JS special effects" class="languagechoosea on">JS special effects</a>
                            <a href="http://ipnx.cn/toolset/website-materials" title="Website Materials" class="languagechoosea on">Website Materials</a>
                            <a href="http://ipnx.cn/toolset/extension-plug-ins" title="Extension plug-ins" class="languagechoosea on">Extension plug-ins</a>
                        </div>
                    </div>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="http://ipnx.cn/ai" title="AI Tools" class="head_nava head_nava-template1_3">AI Tools</a>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="Leisure" class="head_nava head_nava-template1_3">Leisure</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1_3" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/game" title="Game Download" class="languagechoosea on">Game Download</a>
                            <a href="http://ipnx.cn/mobile-game-tutorial/" title="Game Tutorials" class="languagechoosea">Game Tutorials</a>

                        </div>
                    </div>
                </div>
            </div>
        </div>
                    <div   id="wjcelcm34c"   class="head_search">
                <input id="key_words"  onkeydown="if (event.keyCode == 13) searchs('en')" class="search-input" type="text" autocomplete="off" name="keywords" required="required" placeholder="Block,address,transaction,news" value="">
                <a href="javascript:;" title="search"  onclick="searchs('en')"><img src="/static/imghw/find.png" alt="search"></a>
            </div>
                <div   id="wjcelcm34c"   class="head_right">
            <div   id="wjcelcm34c"   class="haed_language">
                <a href="javascript:;" class="layui-btn haed_language_btn">English<i class="layui-icon layui-icon-triangle-d"></i></a>
                <div   class="wjcelcm34c"   id="dropdown-template" style="display: none;">
                    <div   id="wjcelcm34c"   class="languagechoose">
                                                <a href="javascript:setlang('zh-cn');" title="簡體中文" class="languagechoosea">簡體中文</a>
                                                <a href="javascript:;" title="English" class="languagechoosea">English</a>
                                                <a href="javascript:setlang('zh-tw');" title="繁體中文" class="languagechoosea">繁體中文</a>
                                                <a href="javascript:setlang('ja');" title="日本語" class="languagechoosea">日本語</a>
                                                <a href="javascript:setlang('ko');" title="???" class="languagechoosea">???</a>
                                                <a href="javascript:setlang('ms');" title="Melayu" class="languagechoosea">Melayu</a>
                                                <a href="javascript:setlang('fr');" title="Fran?ais" class="languagechoosea">Fran?ais</a>
                                                <a href="javascript:setlang('de');" title="Deutsch" class="languagechoosea">Deutsch</a>
                                            </div>
                </div>
            </div>
            <span id="wjcelcm34c"    class="head_right_line"></span>
                            <div style="display: block;" id="login" class="haed_login ">
                    <a href="javascript:;"  title="Login" class="haed_logina ">Login</a>
                </div>
                <div style="display: block;" id="reg" class="head_signup login">
                    <a href="javascript:;"  title="singup" class="head_signupa">singup</a>
                </div>
            
        </div>
    </div>
</header>

	
	<main>
		<div   id="wjcelcm34c"   class="Article_Details_main">
			<div   id="wjcelcm34c"   class="Article_Details_main1">
							<div   id="wjcelcm34c"   class="Article_Details_main1L">
					<div   id="wjcelcm34c"   class="Article_Details_main1Lmain" id="Article_Details_main1Lmain">
						<div   id="wjcelcm34c"   class="Article_Details_main1L1">Table of Contents</div>
						<div   id="wjcelcm34c"   class="Article_Details_main1L2" id="Article_Details_main1L2">
							<!-- 左側(cè)懸浮,文章定位標(biāo)題1 id="Article_Details_main1L2s_1"-->
															<div   id="wjcelcm34c"   class="Article_Details_main1L2s ">
									<a href="#code-format-code" title="<code>format</code>" ><code>format</code></a>
								</div>
																<div   id="wjcelcm34c"   class="Article_Details_main1L2s ">
									<a href="#code-striptags-code" title="<code>striptags</code>" ><code>striptags</code></a>
								</div>
																<div   id="wjcelcm34c"   class="Article_Details_main1L2s ">
									<a href="#code-escape-code" title="<code>escape</code>" ><code>escape</code></a>
								</div>
														</div>
					</div>
				</div>
							<div   id="wjcelcm34c"   class="Article_Details_main1M">
					<div   id="wjcelcm34c"   class="phpgenera_Details_mainL1">
						<a href="http://ipnx.cn/" title="Home"
							class="phpgenera_Details_mainL1a">Home</a>
						<img src="/static/imghw/top_right.png" alt="" />
												<a href="http://ipnx.cn/be/"
							class="phpgenera_Details_mainL1a">Backend Development</a>
						<img src="/static/imghw/top_right.png" alt="" />
												<a href="http://ipnx.cn/php-weizijiaocheng.html"
							class="phpgenera_Details_mainL1a">PHP Tutorial</a>
						<img src="/static/imghw/top_right.png" alt="" />
						<span>Twig - the Most Popular Stand-Alone PHP Template Engine</span>
					</div>
					
					<div   id="wjcelcm34c"   class="Articlelist_txts">
						<div   id="wjcelcm34c"   class="Articlelist_txts_info">
							<h1 class="Articlelist_txts_title">Twig - the Most Popular Stand-Alone PHP Template Engine</h1>
							<div   id="wjcelcm34c"   class="Articlelist_txts_info_head">
								<div   id="wjcelcm34c"   class="author_info">
									<a href="http://ipnx.cn/member/1468493.html"  class="author_avatar">
									<img class="lazy"  data-src="https://img.php.cn/upload/avatar/000/000/001/66ea8139b1640968.png" src="/static/imghw/default1.png" alt="Lisa Kudrow">
									</a>
									<div   id="wjcelcm34c"   class="author_detail">
																			<a href="http://ipnx.cn/member/1468493.html" class="author_name">Lisa Kudrow</a>
                                										</div>
								</div>
                			</div>
							<span id="wjcelcm34c"    class="Articlelist_txts_time">Feb 09, 2025 am	 09:07 AM</span>
														
						</div>
					</div>
					<hr />
					<div   id="wjcelcm34c"   class="article_main php-article">
						<div   id="wjcelcm34c"   class="article-list-left detail-content-wrap content">
						<ins class="adsbygoogle"
							style="display:block; text-align:center;"
							data-ad-layout="in-article"
							data-ad-format="fluid"
							data-ad-client="ca-pub-5902227090019525"
							data-ad-slot="3461856641">
						</ins>
						

					<p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173906323392943.jpg" class="lazy" alt="Twig - the Most Popular Stand-Alone PHP Template Engine "></p>
<p><strong>Twig: A popular PHP template engine</strong></p>
<p>Twig is a popular PHP template engine developed by Sensio Labs. It simplifies PHP code and adds features such as security and debugging. Twig acts on both frontend and backend of the project, and can be viewed from two perspectives: Twig for template designers and Twig for developers. Twig uses a core object named <code>Environment</code> to store configurations, extensions, and load templates from a file system or elsewhere. Twig supports nested templates (blocks), avoiding duplication of elements in templates, and can cache compiled templates to speed up subsequent requests. Twig supports conditional statements, loops and filters to control the display of information in templates and provides debugging capabilities to dump all information about template variables. </p>
<p><em>This article was peer-reviewed by Wern Ancheta. Thanks to all the peer reviewers of SitePoint for getting SitePoint content to its best!</em></p>
<hr>
<p>Twig is PHP's template engine. But isn't PHP itself a template engine? Yes, not! Although PHP was originally used as a template engine, it did not develop, and although we can still use it as a template engine, which version of "Hello world" do you prefer: </p>
<pre class='brush:php;toolbar:false;'><?php echo "<p> Hello " . $name . "</p>"; ?></pre>
<p>or </p>
<pre class='brush:php;toolbar:false;'><p>Hello {{ name }}</p></pre>
<p>PHP is a verbose language that is amplified when trying to output HTML content. Modern template systems will eliminate partial verboseness and add quite a bit of functionality to it. Features such as security and debugging capabilities are the backbone of modern template engines. Today, we will focus on Twig. </p>
<p><img src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/article/000/000/000/173906323392943.jpg"  class="lazy" alt="Twig - the Most Popular Stand-Alone PHP Template Engine " /></p>
<p>Twig is a template engine created by Sensio Labs (the development company of Blackfire and Symfony). Let's take a look at its main advantages and how to use it in your project. </p>
<p><strong>Installation</strong></p>
<p>There are two ways to install Twig. We can use the tar packages available on their website, or use Composer as we have been doing. </p>
<pre class='brush:php;toolbar:false;'>composer require twig/twig</pre>
<p><em>We assume you are running an environment where PHP is set up and Composer is installed globally. The best way is to use Homestead Improved – it allows you to start using it in 5 minutes on the exact same machine we use so we can be on the same page. If you want to learn more about the PHP environment, we have an excellent paid book about this here for purchase. </em></p>
<p>We need to clarify something before we can continue. As a template engine, Twig acts on both frontend and backend of the project. So we can look at Twig from two different perspectives: Twig for template designers and Twig for developers. On the one hand, we prepare all the data we need; on the other hand, we present all of this data. </p>
<p><strong>Basic Usage</strong></p><p>To illustrate the basic usage of Twig, let's create a simple project. First, we need to bootstrap Twig. Let's create a <code>bootstrap.php</code> file with the following content: </p>
<pre class='brush:php;toolbar:false;'><?php echo "<p> Hello " . $name . "</p>"; ?></pre>
<p>Twig uses a core object named <code>Environment</code>. Instances of this type are used to store configurations, extensions, and load templates from file systems or other locations. After our Twig instance boots, we can go ahead and create a <code>index.php</code> file where it loads some data and passes it to the Twig template. </p>
<pre class='brush:php;toolbar:false;'><p>Hello {{ name }}</p></pre>
<p> This is a simple example; we are creating an array containing products, such as our mechanical keyboard, which we can use in templates. Then we use the <code>render()</code> method, which accepts the template name (this is a file in the template folder we defined earlier) and the data we want to pass to the template. To complete our example, let's go to our <code>/templates</code> folder and create a <code>index.html</code> file. First, let's look at the template itself. </p>
<pre class='brush:php;toolbar:false;'>composer require twig/twig</pre>
<p>Open <code>index.php</code> in your browser (visit localhost or homestead.app, depending on how you set up the host and server) should now display the following screen: </p>
<p><img src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/article/000/000/000/173906323463936.jpg"  class="lazy" alt="Twig - the Most Popular Stand-Alone PHP Template Engine " /></p>
<p> But let's go back and take a closer look at our template code. There are two types of separators: <code>{{ ... }}</code> is used to print the results of an expression or operation, while <code>{% ... %}</code> is used to execute statements such as conditional statements and loops. These delimiters are the main language structure of Twig, which Twig uses to "inform" the template it must render the Twig element. </p>
<p><strong> (The following content is similar to the original text, but some statement adjustments and paragraph divisions have been made, and the image position remains unchanged) </strong></p>
<p><strong>Layout</strong></p>
<p> To avoid duplicating elements (such as headers and footers) in templates, Twig allows us to nest templates in templates, which are called blocks. To illustrate this, let's separate the actual content from the HTML definition in the example. Let's create a new HTML file and name it <code>layout.html</code>:</p>
<pre class='brush:php;toolbar:false;'><?php
// 加載我們的自動(dòng)加載器
require_once __DIR__.'/vendor/autoload.php';

// 指定我們的Twig模板位置
$loader = new Twig_Loader_Filesystem(__DIR__.'/templates');

// 實(shí)例化我們的Twig
$twig = new Twig_Environment($loader);</pre>
<p>We created a block called <code>content</code>. We mean that each template extending from <code>layout.html</code> can implement a <code>content</code> block, which will be displayed at that location. This way, we can reuse the layout multiple times without rewriting it. In this case, the <code>index.html</code> file now looks like this: </p>
<pre class='brush:php;toolbar:false;'><?php
require_once __DIR__.'/bootstrap.php';

// 創(chuàng)建產(chǎn)品列表
$products = [
    [
        'name'          => 'Notebook',
        'description'   => 'Core i7',
        'value'         =>  800.00,
        'date_register' => '2017-06-22',
    ],
    [
        'name'          => 'Mouse',
        'description'   => 'Razer',
        'value'         =>  125.00,
        'date_register' => '2017-10-25',
    ],
    [
        'name'          => 'Keyboard',
        'description'   => 'Mechanical Keyboard',
        'value'         =>  250.00,
        'date_register' => '2017-06-23',
    ],
];

// 渲染我們的視圖
echo $twig->render('index.html', ['products' => $products] );</pre>
<p>Twig also allows us to render only single blocks. To do this, we need to load the template first and then render the block. </p>
<pre class='brush:php;toolbar:false;'><!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <title>Twig Example</title>
    </head>
    <body>
    <table> border="1" style="width: 80%;">
        <thead>
            <tr>
                <td>Product</td>
                <td>Description</td>
                <td>Value</td>
                <td>Date</td>
            </tr>
        </thead>
        <tbody>
            {% for product in products %}
                <tr>
                    <td>{{ product.name }}</td>
                    <td>{{ product.description }}</td>
                    <td>{{ product.value }}</td>
                    <td>{{ product.date_register|date("m/d/Y") }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    </body>
</html></pre>
<p>At this point, we still have the same page, but we reduce its complexity by decoupling the context blocks. </p>
<p><strong>Cache</strong></p><p><code>Environment</code>Objects can not only be used to load templates. If we pass using the <code>cache</code> option of the associated directory, Twig will cache the compiled template, thus avoiding parsing the template in subsequent requests. The compiled template will be stored in the directory we provide. Note that this is the cache for the compiled templates, not the cache for the evaluated templates. This means that Twig will parse, compile and save the template file. All subsequent requests still require evaluation templates, but the first step is already done for you. Let's cache the template in the example by editing the <code>bootstrap.php</code> file: </p>
<pre class='brush:php;toolbar:false;'><?php echo "<p> Hello " . $name . "</p>"; ?></pre>
<p><strong> (The following content is similar to the original text, but some statement adjustments and paragraph divisions have been made, and the image position remains unchanged) </strong></p>
<p><strong>Cycle</strong></p>
<p> In our example, we have seen how to loop with Twig. Basically, we use the <code>for</code> tag and assign an alias to each element in the specified array. In this case, we assign an alias to the <code>products</code> array. After that, we can use the <code>product</code> operator to access all properties in each array element. We use the <code>.</code> tag to indicate the end of the loop. We can also loop through numbers or letters using the <code>endfor</code> operator. As shown below: <code>..</code>
</p>
<pre class='brush:php;toolbar:false;'><p>Hello {{ name }}</p></pre> or letter: <p>
</p>
<pre class='brush:php;toolbar:false;'>composer require twig/twig</pre>This operator is just the syntax sugar of the <p> function, and it works the same way as the native PHP<code>range</code> function. An equally useful option is to add conditions to the loop. Using conditions, we can filter the elements to iterate. Suppose we want to iterate over all products with a value less than 250: <code>range</code>
</p>
<pre class='brush:php;toolbar:false;'><?php
// 加載我們的自動(dòng)加載器
require_once __DIR__.'/vendor/autoload.php';

// 指定我們的Twig模板位置
$loader = new Twig_Loader_Filesystem(__DIR__.'/templates');

// 實(shí)例化我們的Twig
$twig = new Twig_Environment($loader);</pre><p>Conditional statement<strong></strong>
</p>Twig also provides conditional statements in the form of <p>, <code>if</code>, <code>elseif</code> and <code>if not</code> tags. Just like in any programming language, we can use these tags to filter conditions in templates. Suppose in our example, we want to display only products with a value above 500: <code>else</code>
</p>
<pre class='brush:php;toolbar:false;'><?php
require_once __DIR__.'/bootstrap.php';

// 創(chuàng)建產(chǎn)品列表
$products = [
    [
        'name'          => 'Notebook',
        'description'   => 'Core i7',
        'value'         =>  800.00,
        'date_register' => '2017-06-22',
    ],
    [
        'name'          => 'Mouse',
        'description'   => 'Razer',
        'value'         =>  125.00,
        'date_register' => '2017-10-25',
    ],
    [
        'name'          => 'Keyboard',
        'description'   => 'Mechanical Keyboard',
        'value'         =>  250.00,
        'date_register' => '2017-06-23',
    ],
];

// 渲染我們的視圖
echo $twig->render('index.html', ['products' => $products] );</pre><p>Filter<strong></strong>
</p> Filters allow us to filter the information passed to the template and the format of the information displayed. Let's take a look at some of the most commonly used and important filters. A complete list of Twig filters can be found here. <p>
</p>Date and <h3><code>date_modify</code>
</h3><p> Filter formats the date to the given format. As we see in the example: <code>date</code>
</p>
<pre class='brush:php;toolbar:false;'><!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <title>Twig Example</title>
    </head>
    <body>
    <table> border="1" style="width: 80%;">
        <thead>
            <tr>
                <td>Product</td>
                <td>Description</td>
                <td>Value</td>
                <td>Date</td>
            </tr>
        </thead>
        <tbody>
            {% for product in products %}
                <tr>
                    <td>{{ product.name }}</td>
                    <td>{{ product.description }}</td>
                    <td>{{ product.value }}</td>
                    <td>{{ product.date_register|date("m/d/Y") }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    </body>
</html></pre>We display dates in month/day/year format. In addition to the <p> filter, we can also use the <code>date</code> filter to change the date using the <code>date_modify</code> filter. For example, if we want to add a day to a date, we can use the following: </p>
<pre class='brush:php;toolbar:false;'><!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <title>Tutorial Example</title>
    </head>
    <body>
        {% block content %}
        {% endblock %}
    </body>
</html></pre>
<h3 id="code-format-code"><code>format</code></h3>
<p>Format the given string by replacing all placeholders. For example: </p>
<pre class='brush:php;toolbar:false;'>{% extends "layout.html" %}

{% block content %}
    <table> border="1" style="width: 80%;">
        <thead>
            <tr>
                <td>Product</td>
                <td>Description</td>
                <td>Value</td>
                <td>Date</td>
            </tr>
        </thead>
        <tbody>
            {% for product in products %}
                <tr>
                    <td>{{ product.name }}</td>
                    <td>{{ product.description }}</td>
                    <td>{{ product.value }}</td>
                    <td>{{ product.date_register|date("m/d/Y") }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}</pre>
<h3 id="code-striptags-code"><code>striptags</code></h3>
<p><code>striptags</code> The filter removes SGML/XML tags and replaces adjacent spaces with spaces: </p><pre class='brush:php;toolbar:false;'><?php echo "<p> Hello " . $name . "</p>"; ?></pre>
<h3 id="code-escape-code"><code>escape</code></h3>
<p><code>escape</code> is one of the most important filters. It filters the string to insert safely into the final output. By default, it uses HTML escape policy, so </p>
<pre class='brush:php;toolbar:false;'><p>Hello {{ name }}</p></pre>
<p>equivalent to </p>
<pre class='brush:php;toolbar:false;'>composer require twig/twig</pre>
<p><code>js</code>, <code>css</code>, <code>url</code>, <code>html_attr</code> and </p> escape policies are also available. They are Javascript, CSS, URI, and HTML attribute context escape strings, respectively. <p>
<strong></strong>Debug</p><p>
<code>dump()</code> Finally, let's take a look at debugging. Sometimes we need to access all the information of the template variable. To do this, Twig has a <code>Twig_Extension_Debug</code> function. This function is not available by default. When creating a Twig environment, we have to add the </p> extension: <pre class='brush:php;toolbar:false;'><?php
// 加載我們的自動(dòng)加載器
require_once __DIR__.'/vendor/autoload.php';

// 指定我們的Twig模板位置
$loader = new Twig_Loader_Filesystem(__DIR__.'/templates');

// 實(shí)例化我們的Twig
$twig = new Twig_Environment($loader);</pre>
<p>
<code>dump()</code>This step is necessary so that we do not accidentally leak debug information on the production server. Once the configuration is complete, we simply use the </p> function to dump all information about the template variables. <pre class='brush:php;toolbar:false;'><?php
require_once __DIR__.'/bootstrap.php';

// 創(chuàng)建產(chǎn)品列表
$products = [
    [
        'name'          => 'Notebook',
        'description'   => 'Core i7',
        'value'         =>  800.00,
        'date_register' => '2017-06-22',
    ],
    [
        'name'          => 'Mouse',
        'description'   => 'Razer',
        'value'         =>  125.00,
        'date_register' => '2017-10-25',
    ],
    [
        'name'          => 'Keyboard',
        'description'   => 'Mechanical Keyboard',
        'value'         =>  250.00,
        'date_register' => '2017-06-23',
    ],
];

// 渲染我們的視圖
echo $twig->render('index.html', ['products' => $products] );</pre>
<p>
<strong></strong>Conclusion</p>
<p>
</p>I hope this article will provide you with a solid foundation for Twig basics and start your project right away! If you want to have a deeper look at Twig, the official website provides very good documentation and references that you can check out. Do you use the template engine? What do you think of Twig? Would you compare it to popular alternatives like Blade or Smarty? <p>
<strong></strong> (The following content is FAQ, the original text has been included, omitted here) </p><p>The above is the detailed content of Twig - the Most Popular Stand-Alone PHP Template Engine. For more information, please follow other related articles on the PHP Chinese website!</p>


						</div>
					</div>
					<div   id="wjcelcm34c"   class="wzconShengming_sp">
						<div   id="wjcelcm34c"   class="bzsmdiv_sp">Statement of this Website</div>
						<div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div>
					</div>
				</div>

				<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5902227090019525"
     data-ad-slot="2507867629"></ins>



				<div   id="wjcelcm34c"   class="AI_ToolDetails_main4sR">


				<ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-5902227090019525"
        data-ad-slot="3653428331"
        data-ad-format="auto"
        data-full-width-responsive="true"></ins>
    


					<!-- <div   id="wjcelcm34c"   class="phpgenera_Details_mainR4">
						<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
								<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									src="/static/imghw/hotarticle2.png" alt="" />
								<h2>Hot Article</h2>
							</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottom">
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796828723.html" title="Agnes Tachyon Build Guide | A Pretty Derby Musume" class="phpgenera_Details_mainR4_bottom_title">Agnes Tachyon Build Guide | A Pretty Derby Musume</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>1 months ago</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796832397.html" title="Grass Wonder Build Guide | Uma Musume Pretty Derby" class="phpgenera_Details_mainR4_bottom_title">Grass Wonder Build Guide | Uma Musume Pretty Derby</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 weeks ago</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796833110.html" title="Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them" class="phpgenera_Details_mainR4_bottom_title">Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 weeks ago</span>
										<span>By DDD</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796831605.html" title="Uma Musume Pretty Derby Banner Schedule (July 2025)" class="phpgenera_Details_mainR4_bottom_title">Uma Musume Pretty Derby Banner Schedule (July 2025)</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 weeks ago</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796828810.html" title="NYT 'Connections' Hints For Wednesday, July 2: Clues And Answers For Today's Game" class="phpgenera_Details_mainR4_bottom_title">NYT 'Connections' Hints For Wednesday, July 2: Clues And Answers For Today's Game</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>1 months ago</span>
										<span>By DDD</span>
									</div>
								</div>
														</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
								<a href="http://ipnx.cn/article.html">Show More</a>
							</div>
						</div>
					</div> -->


											<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
								<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/hottools2.png" alt="" />
									<h2>Hot AI Tools</h2>
								</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_bottom">
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173410641626608.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undress AI Tool" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_title">
													<h3>Undress AI Tool</h3>
												</a>
												<p>Undress images for free</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411540686492.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undresser.AI Undress" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_title">
													<h3>Undresser.AI Undress</h3>
												</a>
												<p>AI-powered app for creating realistic nude photos</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411552797167.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="AI Clothes Remover" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_title">
													<h3>AI Clothes Remover</h3>
												</a>
												<p>Online AI tool for removing clothes from photos.</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411529149311.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Clothoff.io" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_title">
													<h3>Clothoff.io</h3>
												</a>
												<p>AI clothes remover</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173414504068133.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Video Face Swap" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_title">
													<h3>Video Face Swap</h3>
												</a>
												<p>Swap faces in any video effortlessly with our completely free AI face swap tool!</p>
											</div>
										</div>
																</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
									<a href="http://ipnx.cn/ai">Show More</a>
								</div>
							</div>
						</div>
					


					<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4">
						<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
								<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									src="/static/imghw/hotarticle2.png" alt="" />
								<h2>Hot Article</h2>
							</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottom">
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796828723.html" title="Agnes Tachyon Build Guide | A Pretty Derby Musume" class="phpgenera_Details_mainR4_bottom_title">Agnes Tachyon Build Guide | A Pretty Derby Musume</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>1 months ago</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796832397.html" title="Grass Wonder Build Guide | Uma Musume Pretty Derby" class="phpgenera_Details_mainR4_bottom_title">Grass Wonder Build Guide | Uma Musume Pretty Derby</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 weeks ago</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796833110.html" title="Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them" class="phpgenera_Details_mainR4_bottom_title">Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 weeks ago</span>
										<span>By DDD</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796831605.html" title="Uma Musume Pretty Derby Banner Schedule (July 2025)" class="phpgenera_Details_mainR4_bottom_title">Uma Musume Pretty Derby Banner Schedule (July 2025)</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 weeks ago</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/1796828810.html" title="NYT 'Connections' Hints For Wednesday, July 2: Clues And Answers For Today's Game" class="phpgenera_Details_mainR4_bottom_title">NYT 'Connections' Hints For Wednesday, July 2: Clues And Answers For Today's Game</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>1 months ago</span>
										<span>By DDD</span>
									</div>
								</div>
														</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
								<a href="http://ipnx.cn/article.html">Show More</a>
							</div>
						</div>
					</div>


											<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
								<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/hottools2.png" alt="" />
									<h2>Hot Tools</h2>
								</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_bottom">
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/toolset/development-tools/92" title="Notepad++7.3.1" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58ab96f0f39f7357.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Notepad++7.3.1" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/toolset/development-tools/92" title="Notepad++7.3.1" class="phpmain_tab2_mids_title">
													<h3>Notepad++7.3.1</h3>
												</a>
												<p>Easy-to-use and free code editor</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/toolset/development-tools/93" title="SublimeText3 Chinese version" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58ab97a3baad9677.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Chinese version" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/toolset/development-tools/93" title="SublimeText3 Chinese version" class="phpmain_tab2_mids_title">
													<h3>SublimeText3 Chinese version</h3>
												</a>
												<p>Chinese version, very easy to use</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/toolset/development-tools/121" title="Zend Studio 13.0.1" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58ab97ecd1ab2670.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Zend Studio 13.0.1" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/toolset/development-tools/121" title="Zend Studio 13.0.1" class="phpmain_tab2_mids_title">
													<h3>Zend Studio 13.0.1</h3>
												</a>
												<p>Powerful PHP integrated development environment</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58d0e0fc74683535.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Dreamweaver CS6" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_title">
													<h3>Dreamweaver CS6</h3>
												</a>
												<p>Visual web development tools</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/toolset/development-tools/500" title="SublimeText3 Mac version" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58d34035e2757995.png?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Mac version" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/toolset/development-tools/500" title="SublimeText3 Mac version" class="phpmain_tab2_mids_title">
													<h3>SublimeText3 Mac version</h3>
												</a>
												<p>God-level code editing software (SublimeText3)</p>
											</div>
										</div>
																	</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
									<a href="http://ipnx.cn/ai">Show More</a>
								</div>
							</div>
						</div>
										

					
					<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4">
						<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
								<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									src="/static/imghw/hotarticle2.png" alt="" />
								<h2>Hot Topics</h2>
							</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottom">
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/laravel-tutori" title="Laravel Tutorial" class="phpgenera_Details_mainR4_bottom_title">Laravel Tutorial</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/eyess.png" alt="" />
											<span>1597</span>
										</div>
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/tiezi.png" alt="" />
											<span>29</span>
										</div>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/php-tutorial" title="PHP Tutorial" class="phpgenera_Details_mainR4_bottom_title">PHP Tutorial</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/eyess.png" alt="" />
											<span>1488</span>
										</div>
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/tiezi.png" alt="" />
											<span>72</span>
										</div>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/nytminicrosswordanswe" title="nyt mini crossword answers" class="phpgenera_Details_mainR4_bottom_title">nyt mini crossword answers</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/eyess.png" alt="" />
											<span>268</span>
										</div>
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/tiezi.png" alt="" />
											<span>587</span>
										</div>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/faq/newyorktimesdailybrief" title="nyt connections hints and answers" class="phpgenera_Details_mainR4_bottom_title">nyt connections hints and answers</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/eyess.png" alt="" />
											<span>130</span>
										</div>
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/tiezi.png" alt="" />
											<span>836</span>
										</div>
									</div>
								</div>
														</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
								<a href="http://ipnx.cn/faq/zt">Show More</a>
							</div>
						</div>
					</div>
				</div>
			</div>
							<div   id="wjcelcm34c"   class="Article_Details_main2">
					<div   id="wjcelcm34c"   class="phpgenera_Details_mainL4">
						<div   id="wjcelcm34c"   class="phpmain1_2_top">
							<a href="javascript:void(0);" class="phpmain1_2_top_title">Related knowledge<img
									src="/static/imghw/index2_title2.png" alt="" /></a>
						</div>
						<div   id="wjcelcm34c"   class="phpgenera_Details_mainL4_info">

													<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796829359.html" title="php regex for password strength" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/431/639/175150999170968.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="php regex for password strength" />
								</a>
								<a href="http://ipnx.cn/faq/1796829359.html" title="php regex for password strength" class="phphistorical_Version2_mids_title">php regex for password strength</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 03, 2025 am	 10:33 AM</span>
								<p class="Articlelist_txts_p">To determine the strength of the password, it is necessary to combine regular and logical processing. The basic requirements include: 1. The length is no less than 8 digits; 2. At least containing lowercase letters, uppercase letters, and numbers; 3. Special character restrictions can be added; in terms of advanced aspects, continuous duplication of characters and incremental/decreasing sequences need to be avoided, which requires PHP function detection; at the same time, blacklists should be introduced to filter common weak passwords such as password and 123456; finally it is recommended to combine the zxcvbn library to improve the evaluation accuracy.</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796828544.html" title="How to combine two php arrays unique values?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175144789373347.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How to combine two php arrays unique values?" />
								</a>
								<a href="http://ipnx.cn/faq/1796828544.html" title="How to combine two php arrays unique values?" class="phphistorical_Version2_mids_title">How to combine two php arrays unique values?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 02, 2025 pm	 05:18 PM</span>
								<p class="Articlelist_txts_p">To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796839536.html" title="PHP Variable Scope Explained" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175269699023092.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="PHP Variable Scope Explained" />
								</a>
								<a href="http://ipnx.cn/faq/1796839536.html" title="PHP Variable Scope Explained" class="phphistorical_Version2_mids_title">PHP Variable Scope Explained</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 17, 2025 am	 04:16 AM</span>
								<p class="Articlelist_txts_p">Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796832599.html" title="How to handle File Uploads securely in PHP?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175191342169363.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How to handle File Uploads securely in PHP?" />
								</a>
								<a href="http://ipnx.cn/faq/1796832599.html" title="How to handle File Uploads securely in PHP?" class="phphistorical_Version2_mids_title">How to handle File Uploads securely in PHP?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 08, 2025 am	 02:37 AM</span>
								<p class="Articlelist_txts_p">To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796840634.html" title="Commenting Out Code in PHP" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175278584067051.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Commenting Out Code in PHP" />
								</a>
								<a href="http://ipnx.cn/faq/1796840634.html" title="Commenting Out Code in PHP" class="phphistorical_Version2_mids_title">Commenting Out Code in PHP</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 18, 2025 am	 04:57 AM</span>
								<p class="Articlelist_txts_p">There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796834881.html" title="How Do Generators Work in PHP?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175217473076928.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How Do Generators Work in PHP?" />
								</a>
								<a href="http://ipnx.cn/faq/1796834881.html" title="How Do Generators Work in PHP?" class="phphistorical_Version2_mids_title">How Do Generators Work in PHP?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 11, 2025 am	 03:12 AM</span>
								<p class="Articlelist_txts_p">AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796840616.html" title="Tips for Writing PHP Comments" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175278548014857.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Tips for Writing PHP Comments" />
								</a>
								<a href="http://ipnx.cn/faq/1796840616.html" title="Tips for Writing PHP Comments" class="phphistorical_Version2_mids_title">Tips for Writing PHP Comments</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 18, 2025 am	 04:51 AM</span>
								<p class="Articlelist_txts_p">The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/faq/1796828540.html" title="How to create an array in php?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175144687013117.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How to create an array in php?" />
								</a>
								<a href="http://ipnx.cn/faq/1796828540.html" title="How to create an array in php?" class="phphistorical_Version2_mids_title">How to create an array in php?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 02, 2025 pm	 05:01 PM</span>
								<p class="Articlelist_txts_p">There are two ways to create an array in PHP: use the array() function or use brackets []. 1. Using the array() function is a traditional way, with good compatibility. Define index arrays such as $fruits=array("apple","banana","orange"), and associative arrays such as $user=array("name"=>"John","age"=>25); 2. Using [] is a simpler way to support since PHP5.4, such as $color</p>
							</div>
													</div>

													<a href="http://ipnx.cn/be/" class="phpgenera_Details_mainL4_botton">
								<span>See all articles</span>
								<img src="/static/imghw/down_right.png" alt="" />
							</a>
											</div>
				</div>
					</div>
	</main>
	<footer>
    <div   id="wjcelcm34c"   class="footer">
        <div   id="wjcelcm34c"   class="footertop">
            <img src="/static/imghw/logo.png" alt="">
            <p>Public welfare online PHP training,Help PHP learners grow quickly!</p>
        </div>
        <div   id="wjcelcm34c"   class="footermid">
            <a href="http://ipnx.cn/about/us.html">About us</a>
            <a href="http://ipnx.cn/about/disclaimer.html">Disclaimer</a>
            <a href="http://ipnx.cn/update/article_0_1.html">Sitemap</a>
        </div>
        <div   id="wjcelcm34c"   class="footerbottom">
            <p>
                ? php.cn All rights reserved
            </p>
        </div>
    </div>
</footer>

<input type="hidden" id="verifycode" value="/captcha.html">




		<link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css?2' type='text/css' media='all' />
	
	
	
	
	

	
	






<footer>
<div class="friendship-link">
<p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p>
<a href="http://ipnx.cn/" title="亚洲国产日韩欧美一区二区三区">亚洲国产日韩欧美一区二区三区</a>

<div class="friend-links">


</div>
</div>

</footer>


<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
</body><div id="xmlo3" class="pl_css_ganrao" style="display: none;"><form id="xmlo3"></form><blockquote id="xmlo3"></blockquote><dfn id="xmlo3"></dfn><dfn id="xmlo3"></dfn><label id="xmlo3"><menu id="xmlo3"><dl id="xmlo3"><dl id="xmlo3"></dl></dl></menu></label><form id="xmlo3"></form><p id="xmlo3"></p><strong id="xmlo3"><label id="xmlo3"><i id="xmlo3"><legend id="xmlo3"></legend></i></label></strong><progress id="xmlo3"><ins id="xmlo3"><dfn id="xmlo3"><rp id="xmlo3"></rp></dfn></ins></progress><dfn id="xmlo3"></dfn><wbr id="xmlo3"><var id="xmlo3"></var></wbr><samp id="xmlo3"></samp><optgroup id="xmlo3"><sup id="xmlo3"><style id="xmlo3"><code id="xmlo3"></code></style></sup></optgroup><var id="xmlo3"><strong id="xmlo3"></strong></var><table id="xmlo3"><font id="xmlo3"></font></table><li id="xmlo3"></li><strike id="xmlo3"></strike><optgroup id="xmlo3"><delect id="xmlo3"><th id="xmlo3"><th id="xmlo3"></th></th></delect></optgroup><dl id="xmlo3"></dl><sup id="xmlo3"><dl id="xmlo3"><table id="xmlo3"><font id="xmlo3"></font></table></dl></sup><table id="xmlo3"><font id="xmlo3"></font></table><input id="xmlo3"><small id="xmlo3"><progress id="xmlo3"></progress></small></input><source id="xmlo3"></source><kbd id="xmlo3"></kbd><var id="xmlo3"><option id="xmlo3"><form id="xmlo3"><pre id="xmlo3"></pre></form></option></var><strong id="xmlo3"><mark id="xmlo3"></mark></strong><form id="xmlo3"><mark id="xmlo3"><dl id="xmlo3"><noframes id="xmlo3"></noframes></dl></mark></form><strike id="xmlo3"></strike><tbody id="xmlo3"></tbody><strong id="xmlo3"></strong><s id="xmlo3"><form id="xmlo3"><samp id="xmlo3"></samp></form></s><strong id="xmlo3"></strong><sub id="xmlo3"></sub><menuitem id="xmlo3"></menuitem><small id="xmlo3"><xmp id="xmlo3"><legend id="xmlo3"><track id="xmlo3"></track></legend></xmp></small><track id="xmlo3"></track><label id="xmlo3"><noframes id="xmlo3"><sub id="xmlo3"></sub></noframes></label><ruby id="xmlo3"><span id="xmlo3"><menu id="xmlo3"><b id="xmlo3"></b></menu></span></ruby><center id="xmlo3"><i id="xmlo3"><tr id="xmlo3"><progress id="xmlo3"></progress></tr></i></center><dd id="xmlo3"></dd><pre id="xmlo3"><table id="xmlo3"><delect id="xmlo3"><thead id="xmlo3"></thead></delect></table></pre><label id="xmlo3"></label><listing id="xmlo3"></listing><form id="xmlo3"><i id="xmlo3"><tr id="xmlo3"><div id="xmlo3"></div></tr></i></form><table id="xmlo3"><delect id="xmlo3"><p id="xmlo3"><th id="xmlo3"></th></p></delect></table><table id="xmlo3"></table><ol id="xmlo3"><form id="xmlo3"><font id="xmlo3"><thead id="xmlo3"></thead></font></form></ol><strong id="xmlo3"></strong></div>

</html>