blob: e1bc5cc8dd26e59442f68c591533e35c18819a1c [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() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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>
admin10c3f412006-10-08 07:21:12 +000036<td><h1>Code Igniter User Guide Version 1.5.0b1</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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
65<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
67syntax 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
70<h2>Alternative Echos</h2>
71
72<p>Normally to echo, or print out a variable you would do this:</p>
73
74<code>&lt;?php echo $variable; ?></code>
75
76<p>With the alternative syntax you can instead do it this way:</p>
77
78<code>&lt;?=$variable?></code>
79
80<p class="important"><strong>Note:</strong> If you find that the syntax described in this page does not work on your server it might
81be that "short tags" are disabled in your PHP ini file.</p>
82
83
84<h2>Alternative Control Structures</h2>
85
86<p>Controls structures, like <var>if</var>, <var>for</var>, <var>foreach</var>, and <var>while</var> can be
87written in a simplified format as well. Here is an example using foreach:</p>
88
89<code>
90&lt;ul><br />
91<br />
92<var>&lt;?php foreach($todo as $item): ?></var><br />
93<br />
94&lt;li><var>&lt;?=$item?></var>&lt;/li><br />
95<br />
96<var>&lt;?php endforeach ?></var><br />
97<br />
98&lt;/ul></code>
99
100<p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
101Each of the control structures listed above has a similar closing syntax:
102<var>endif</var>, <var>endfor</var>, <var>endforeach</var>, and <var>endwhile</var></p>
103
104<p>Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is
105important!</p>
106
107<p>Here is another example, using if/elseif/else. Notice the colons:</p>
108
109
110<code><var>&lt;?php if ($username == 'sally'): ?></var><br />
111<br />
112&nbsp;&nbsp;&nbsp;&lt;h3>Hi Sally&lt;/h3><br />
113<br />
114<var>&lt;?php elseif ($username == 'joe'): ?></var><br />
115<br />
116&nbsp;&nbsp;&nbsp;&lt;h3>Hi Joe&lt;/h3><br />
117<br />
118<var>&lt;?php else: ?></var><br />
119<br />
120&nbsp;&nbsp;&nbsp;&lt;h3>Hi unknown user&lt;/h3><br />
121<br />
122<var>&lt;?php endif; ?></var></code>
123
124
125
126</div>
127<!-- END CONTENT -->
128
129
130<div id="footer">
131<p>
132Previous Topic:&nbsp;&nbsp;<a href="multiple_apps.html">Running Multiple Applications</a>
133&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
134<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
135<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
136Next Topic:&nbsp;&nbsp;<a href="security.html">Security</a>
137<p>
138<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>
139</div>
140
141</body>
142</html>