1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Alternate PHP Syntax for View Files : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
11 <script type="text/javascript" src="../nav/nav.js"></script>
12 <script type="text/javascript" src="../nav/prototype.lite.js"></script>
13 <script type="text/javascript" src="../nav/moo.fx.js"></script>
14 <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
16 <meta http-equiv='expires' content='-1' />
17 <meta http-equiv= 'pragma' content='no-cache' />
18 <meta name='robots' content='all' />
19 <meta name='author' content='ExpressionEngine Dev Team' />
20 <meta name='description' content='CodeIgniter User Guide' />
25 <!-- START NAVIGATION -->
26 <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27 <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
44 <a href="../index.html">User Guide Home</a> ›
47 <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
50 <!-- END BREADCRUMB -->
55 <!-- START CONTENT -->
58 <h1>Alternate PHP Syntax for View Files</h1>
60 <p>If you do not utilize CodeIgniter's <a href="../libraries/parser.html">template engine</a>, you'll be using pure PHP
61 in your View files. To minimize the PHP code in these files, and to make it easier to identify the code blocks it is recommended that you use
62 PHPs alternative syntax for control structures and short tag echo statements. If you are not familiar with this syntax, it allows you to eliminate the braces from your code,
63 and eliminate "echo" statements.</p>
65 <h2>Automatic Short Tag Support</h2>
67 <p><strong>Note:</strong> If you find that the syntax described in this page does not work on your server it might
68 be that "short tags" are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly,
69 allowing you to use that syntax even if your server doesn't support it. This feature can be enabled in your
70 <dfn>config/config.php</dfn> file.</p>
72 <p class="important">Please note that if you do use this feature, if PHP errors are encountered
73 in your <strong>view files</strong>, the error message and line number will not be accurately shown. Instead, all errors
74 will be shown as <kbd>eval()</kbd> errors.</p>
77 <h2>Alternative Echos</h2>
79 <p>Normally to echo, or print out a variable you would do this:</p>
81 <code><?php echo $variable; ?></code>
83 <p>With the alternative syntax you can instead do it this way:</p>
85 <code><?=$variable?></code>
89 <h2>Alternative Control Structures</h2>
91 <p>Controls structures, like <var>if</var>, <var>for</var>, <var>foreach</var>, and <var>while</var> can be
92 written in a simplified format as well. Here is an example using foreach:</p>
97 <var><?php foreach ($todo as $item): ?></var><br />
99 <li><var><?=$item?></var></li><br />
101 <var><?php endforeach; ?></var><br />
105 <p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
106 Each of the control structures listed above has a similar closing syntax:
107 <var>endif</var>, <var>endfor</var>, <var>endforeach</var>, and <var>endwhile</var></p>
109 <p>Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is
112 <p>Here is another example, using if/elseif/else. Notice the colons:</p>
115 <code><var><?php if ($username == 'sally'): ?></var><br />
117 <h3>Hi Sally</h3><br />
119 <var><?php elseif ($username == 'joe'): ?></var><br />
121 <h3>Hi Joe</h3><br />
123 <var><?php else: ?></var><br />
125 <h3>Hi unknown user</h3><br />
127 <var><?php endif; ?></var></code>
137 Previous Topic: <a href="managing_apps.html">Managing Applications</a>
138 ·
139 <a href="#top">Top of Page</a> ·
140 <a href="../index.html">User Guide Home</a> ·
141 Next Topic: <a href="security.html">Security</a>
143 <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>