Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Alternate PHP Syntax for View Files : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 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> |
| 15 | |
| 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' /> |
| 21 | |
| 22 | </head> |
| 23 | <body> |
| 24 | |
| 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> |
| 28 | <div id="masthead"> |
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 30 | <tr> |
Pascal Kriete | 1f62229 | 2011-04-07 12:06:51 -0400 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 33 | </tr> |
| 34 | </table> |
| 35 | </div> |
| 36 | <!-- END NAVIGATION --> |
| 37 | |
| 38 | |
| 39 | <!-- START BREADCRUMB --> |
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 41 | <tr> |
| 42 | <td id="breadcrumb"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | Alternate PHP Syntax |
| 46 | </td> |
| 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> |
| 48 | </tr> |
| 49 | </table> |
| 50 | <!-- END BREADCRUMB --> |
| 51 | |
| 52 | <br clear="all" /> |
| 53 | |
| 54 | |
| 55 | <!-- START CONTENT --> |
| 56 | <div id="content"> |
| 57 | |
| 58 | <h1>Alternate PHP Syntax for View Files</h1> |
| 59 | |
| 60 | <p>If you do not utilize CodeIgniter's <a href="../libraries/parser.html">template engine</a>, you'll be using pure PHP |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 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, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | and eliminate "echo" statements.</p> |
| 64 | |
| 65 | <h2>Automatic Short Tag Support</h2> |
| 66 | |
| 67 | <p><strong>Note:</strong> If you find that the syntax described in this page does not work on your server it might |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | be that "short tags" are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly, |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 69 | allowing you to use that syntax even if your server doesn't support it. This feature can be enabled in your |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | <dfn>config/config.php</dfn> file.</p> |
| 71 | |
| 72 | <p class="important">Please note that if you do use this feature, if PHP errors are encountered |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 73 | in your <strong>view files</strong>, the error message and line number will not be accurately shown. Instead, all errors |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | will be shown as <kbd>eval()</kbd> errors.</p> |
| 75 | |
| 76 | |
| 77 | <h2>Alternative Echos</h2> |
| 78 | |
| 79 | <p>Normally to echo, or print out a variable you would do this:</p> |
| 80 | |
| 81 | <code><?php echo $variable; ?></code> |
| 82 | |
| 83 | <p>With the alternative syntax you can instead do it this way:</p> |
| 84 | |
| 85 | <code><?=$variable?></code> |
| 86 | |
| 87 | |
| 88 | |
| 89 | <h2>Alternative Control Structures</h2> |
| 90 | |
| 91 | <p>Controls structures, like <var>if</var>, <var>for</var>, <var>foreach</var>, and <var>while</var> can be |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 92 | written in a simplified format as well. Here is an example using foreach:</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | |
| 94 | <code> |
| 95 | <ul><br /> |
| 96 | <br /> |
Pascal Kriete | feba2de | 2011-02-14 13:25:30 -0500 | [diff] [blame] | 97 | <var><?php foreach ($todo as $item): ?></var><br /> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | <br /> |
| 99 | <li><var><?=$item?></var></li><br /> |
| 100 | <br /> |
| 101 | <var><?php endforeach; ?></var><br /> |
| 102 | <br /> |
| 103 | </ul></code> |
| 104 | |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 105 | <p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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> |
| 108 | |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 109 | <p>Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | important!</p> |
| 111 | |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 112 | <p>Here is another example, using if/elseif/else. Notice the colons:</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | <code><var><?php if ($username == 'sally'): ?></var><br /> |
| 116 | <br /> |
| 117 | <h3>Hi Sally</h3><br /> |
| 118 | <br /> |
| 119 | <var><?php elseif ($username == 'joe'): ?></var><br /> |
| 120 | <br /> |
| 121 | <h3>Hi Joe</h3><br /> |
| 122 | <br /> |
| 123 | <var><?php else: ?></var><br /> |
| 124 | <br /> |
| 125 | <h3>Hi unknown user</h3><br /> |
| 126 | <br /> |
| 127 | <var><?php endif; ?></var></code> |
| 128 | |
| 129 | |
| 130 | |
| 131 | </div> |
| 132 | <!-- END CONTENT --> |
| 133 | |
| 134 | |
| 135 | <div id="footer"> |
| 136 | <p> |
| 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> |
| 142 | </p> |
Derek Jones | 700205a | 2011-01-28 07:44:28 -0600 | [diff] [blame] | 143 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | </div> |
| 145 | |
| 146 | </body> |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 147 | </html> |