blob: a8e860396f6bcdfbc5459db0507d83dd397624c4 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>Alternate PHP Syntax for View Files : CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00007
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
admin17a890d2006-09-27 20:42:42 +000011<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000012<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000013<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000014<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000015
adminb0dd10f2006-08-25 17:25:49 +000016<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
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>
Rick Ellis7cdef032008-08-26 18:44:54 +000027<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>
adminb0dd10f2006-08-25 17:25:49 +000028<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Rick Ellis25949532008-08-26 19:48:08 +000031<td><h1>CodeIgniter User Guide Version 1.7</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</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">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Alternate PHP Syntax
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<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>
adminb0dd10f2006-08-25 17:25:49 +000048</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
Derek Allardd2df9bc2007-04-15 17:41:17 +000060<p>If you do not utilize CodeIgniter's <a href="../libraries/parser.html">template engine</a>, you'll be using pure PHP
adminb93464d2006-10-31 00:36:32 +000061in 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
62PHPs 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,
adminb0dd10f2006-08-25 17:25:49 +000063and eliminate "echo" statements.</p>
64
adminb93464d2006-10-31 00:36:32 +000065<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
Derek Allardd2df9bc2007-04-15 17:41:17 +000068be that "short tags" are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly,
admincef21062006-10-30 17:13:13 +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
adminb93464d2006-10-31 00:36:32 +000072<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
admincef21062006-10-30 17:13:13 +000076
adminb0dd10f2006-08-25 17:25:49 +000077<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
adminb0dd10f2006-08-25 17:25:49 +000087
88
89<h2>Alternative Control Structures</h2>
90
admine334c472006-10-21 19:44:22 +000091<p>Controls structures, like <var>if</var>, <var>for</var>, <var>foreach</var>, and <var>while</var> can be
adminb0dd10f2006-08-25 17:25:49 +000092written 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 />
Derek Allardbc538942007-04-03 11:42:31 +0000101<var>&lt;?php endforeach; ?></var><br />
adminb0dd10f2006-08-25 17:25:49 +0000102<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
admine334c472006-10-21 19:44:22 +0000109<p>Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is
adminb0dd10f2006-08-25 17:25:49 +0000110important!</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>
admincef21062006-10-30 17:13:13 +0000137Previous Topic:&nbsp;&nbsp;<a href="managing_apps.html">Managing Applications</a>
adminb0dd10f2006-08-25 17:25:49 +0000138&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>
Derek Allardc6441282007-07-04 23:54:32 +0000142</p>
Derek Jones07870432008-02-13 03:49:26 +0000143<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000144</div>
145
146</body>
147</html>