blob: 89e53fd0f07b7b98be2af2d91b3fe9c952eea869 [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">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Rick Ellis257651d2006-11-23 18:40:13 +000036<td><h1>Code Igniter User Guide Version 1.5.1</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Alternate PHP Syntax
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63<h1>Alternate PHP Syntax for View Files</h1>
64
admine334c472006-10-21 19:44:22 +000065<p>If you do not utilize Code Igniter's <a href="../libraries/parser.html">template engine</a>, you'll be using pure PHP
adminb93464d2006-10-31 00:36:32 +000066in 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
67PHPs 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 +000068and eliminate "echo" statements.</p>
69
adminb93464d2006-10-31 00:36:32 +000070<h2>Automatic Short Tag Support</h2>
71
72<p><strong>Note:</strong> If you find that the syntax described in this page does not work on your server it might
admincef21062006-10-30 17:13:13 +000073be that "short tags" are disabled in your PHP ini file. Code Igniter will optionally rewrite short tags on-the-fly,
74allowing you to use that syntax even if your server doesn't support it. This feature can be enabled in your
75<dfn>config/config.php</dfn> file.</p>
76
adminb93464d2006-10-31 00:36:32 +000077<p class="important">Please note that if you do use this feature, if PHP errors are encountered
78in your <strong>view files</strong>, the error message and line number will not be accurately shown. Instead, all errors
79will be shown as <kbd>eval()</kbd> errors.</p>
80
admincef21062006-10-30 17:13:13 +000081
adminb0dd10f2006-08-25 17:25:49 +000082<h2>Alternative Echos</h2>
83
84<p>Normally to echo, or print out a variable you would do this:</p>
85
86<code>&lt;?php echo $variable; ?></code>
87
88<p>With the alternative syntax you can instead do it this way:</p>
89
90<code>&lt;?=$variable?></code>
91
adminb0dd10f2006-08-25 17:25:49 +000092
93
94<h2>Alternative Control Structures</h2>
95
admine334c472006-10-21 19:44:22 +000096<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 +000097written in a simplified format as well. Here is an example using foreach:</p>
98
99<code>
100&lt;ul><br />
101<br />
102<var>&lt;?php foreach($todo as $item): ?></var><br />
103<br />
104&lt;li><var>&lt;?=$item?></var>&lt;/li><br />
105<br />
106<var>&lt;?php endforeach ?></var><br />
107<br />
108&lt;/ul></code>
109
110<p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
111Each of the control structures listed above has a similar closing syntax:
112<var>endif</var>, <var>endfor</var>, <var>endforeach</var>, and <var>endwhile</var></p>
113
admine334c472006-10-21 19:44:22 +0000114<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 +0000115important!</p>
116
117<p>Here is another example, using if/elseif/else. Notice the colons:</p>
118
119
120<code><var>&lt;?php if ($username == 'sally'): ?></var><br />
121<br />
122&nbsp;&nbsp;&nbsp;&lt;h3>Hi Sally&lt;/h3><br />
123<br />
124<var>&lt;?php elseif ($username == 'joe'): ?></var><br />
125<br />
126&nbsp;&nbsp;&nbsp;&lt;h3>Hi Joe&lt;/h3><br />
127<br />
128<var>&lt;?php else: ?></var><br />
129<br />
130&nbsp;&nbsp;&nbsp;&lt;h3>Hi unknown user&lt;/h3><br />
131<br />
132<var>&lt;?php endif; ?></var></code>
133
134
135
136</div>
137<!-- END CONTENT -->
138
139
140<div id="footer">
141<p>
admincef21062006-10-30 17:13:13 +0000142Previous Topic:&nbsp;&nbsp;<a href="managing_apps.html">Managing Applications</a>
adminb0dd10f2006-08-25 17:25:49 +0000143&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
144<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
145<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
146Next Topic:&nbsp;&nbsp;<a href="security.html">Security</a>
147<p>
148<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
149</div>
150
151</body>
152</html>