blob: 888123ed01cc9f2bc777d15f860d099f5fe388ac [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>
admin05186612006-10-28 03:28:01 +000036<td><h1>Code Igniter User Guide Version 1.5.0</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
66in 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 PHPs alternative
adminb0dd10f2006-08-25 17:25:49 +000067syntax for control structures and echo statements. If you are not familiar with this syntax, it allows you to eliminate the braces from your code,
68and eliminate "echo" statements.</p>
69
admincef21062006-10-30 17:13:13 +000070<p class="important"><strong>Note:</strong> If you find that the syntax described in this page does not work on your server it might
71be that "short tags" are disabled in your PHP ini file. Code Igniter will optionally rewrite short tags on-the-fly,
72allowing you to use that syntax even if your server doesn't support it. This feature can be enabled in your
73<dfn>config/config.php</dfn> file.</p>
74
75
adminb0dd10f2006-08-25 17:25:49 +000076<h2>Alternative Echos</h2>
77
78<p>Normally to echo, or print out a variable you would do this:</p>
79
80<code>&lt;?php echo $variable; ?></code>
81
82<p>With the alternative syntax you can instead do it this way:</p>
83
84<code>&lt;?=$variable?></code>
85
adminb0dd10f2006-08-25 17:25:49 +000086
87
88<h2>Alternative Control Structures</h2>
89
admine334c472006-10-21 19:44:22 +000090<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 +000091written in a simplified format as well. Here is an example using foreach:</p>
92
93<code>
94&lt;ul><br />
95<br />
96<var>&lt;?php foreach($todo as $item): ?></var><br />
97<br />
98&lt;li><var>&lt;?=$item?></var>&lt;/li><br />
99<br />
100<var>&lt;?php endforeach ?></var><br />
101<br />
102&lt;/ul></code>
103
104<p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
105Each of the control structures listed above has a similar closing syntax:
106<var>endif</var>, <var>endfor</var>, <var>endforeach</var>, and <var>endwhile</var></p>
107
admine334c472006-10-21 19:44:22 +0000108<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 +0000109important!</p>
110
111<p>Here is another example, using if/elseif/else. Notice the colons:</p>
112
113
114<code><var>&lt;?php if ($username == 'sally'): ?></var><br />
115<br />
116&nbsp;&nbsp;&nbsp;&lt;h3>Hi Sally&lt;/h3><br />
117<br />
118<var>&lt;?php elseif ($username == 'joe'): ?></var><br />
119<br />
120&nbsp;&nbsp;&nbsp;&lt;h3>Hi Joe&lt;/h3><br />
121<br />
122<var>&lt;?php else: ?></var><br />
123<br />
124&nbsp;&nbsp;&nbsp;&lt;h3>Hi unknown user&lt;/h3><br />
125<br />
126<var>&lt;?php endif; ?></var></code>
127
128
129
130</div>
131<!-- END CONTENT -->
132
133
134<div id="footer">
135<p>
admincef21062006-10-30 17:13:13 +0000136Previous Topic:&nbsp;&nbsp;<a href="managing_apps.html">Managing Applications</a>
adminb0dd10f2006-08-25 17:25:49 +0000137&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
138<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
139<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
140Next Topic:&nbsp;&nbsp;<a href="security.html">Security</a>
141<p>
142<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>
143</div>
144
145</body>
146</html>