blob: 3193c0585fc341208d39dcdbcbbf7a0b4bb6297f [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!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>
Derek Jones8917af72010-03-05 12:41:45 -060031<td><h1>CodeIgniter User Guide Version 2.0.0</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<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> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Alternate 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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<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
Barry Mienydd671972010-10-04 16:33:58 +020061in 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
Derek Allard2067d1a2008-11-13 22:59:24 +000062PHPs 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,
63and 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 Mienydd671972010-10-04 16:33:58 +020068be that "short tags" are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly,
Derek Allard2067d1a2008-11-13 22:59:24 +000069allowing 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>
71
72<p class="important">Please note that if you do use this feature, if PHP errors are encountered
73in your <strong>view files</strong>, the error message and line number will not be accurately shown. Instead, all errors
74will 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>&lt;?php echo $variable; ?></code>
82
83<p>With the alternative syntax you can instead do it this way:</p>
84
85<code>&lt;?=$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
92written in a simplified format as well. Here is an example using foreach:</p>
93
94<code>
95&lt;ul><br />
96<br />
97<var>&lt;?php foreach($todo as $item): ?></var><br />
98<br />
99&lt;li><var>&lt;?=$item?></var>&lt;/li><br />
100<br />
101<var>&lt;?php endforeach; ?></var><br />
102<br />
103&lt;/ul></code>
104
105<p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
106Each 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
109<p>Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is
110important!</p>
111
112<p>Here is another example, using if/elseif/else. Notice the colons:</p>
113
114
115<code><var>&lt;?php if ($username == 'sally'): ?></var><br />
116<br />
117&nbsp;&nbsp;&nbsp;&lt;h3>Hi Sally&lt;/h3><br />
118<br />
119<var>&lt;?php elseif ($username == 'joe'): ?></var><br />
120<br />
121&nbsp;&nbsp;&nbsp;&lt;h3>Hi Joe&lt;/h3><br />
122<br />
123<var>&lt;?php else: ?></var><br />
124<br />
125&nbsp;&nbsp;&nbsp;&lt;h3>Hi unknown user&lt;/h3><br />
126<br />
127<var>&lt;?php endif; ?></var></code>
128
129
130
131</div>
132<!-- END CONTENT -->
133
134
135<div id="footer">
136<p>
137Previous Topic:&nbsp;&nbsp;<a href="managing_apps.html">Managing Applications</a>
138&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
139<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
140<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
141Next Topic:&nbsp;&nbsp;<a href="security.html">Security</a>
142</p>
Derek Jonesd6d70e32010-03-29 10:10:27 -0500143<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2010 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000144</div>
145
146</body>
adminb0dd10f2006-08-25 17:25:49 +0000147</html>