admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!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 |
|
| 10 | <script type="text/javascript" src="../scripts/nav.js"></script>
|
| 11 | <script type="text/javascript" src="../scripts/prototype.lite.js"></script>
|
| 12 | <script type="text/javascript" src="../scripts/moo.fx.js"></script>
|
| 13 | <script type="text/javascript">
|
| 14 | window.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>
|
admin | 9fc347d | 2006-09-21 00:00:28 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 37 | <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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Alternate 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <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
|
| 66 | in 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
|
| 67 | syntax for control structures and echo statements. If you are not familiar with this syntax, it allows you to eliminate the braces from your code,
|
| 68 | and 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><?php echo $variable; ?></code>
|
| 75 |
|
| 76 | <p>With the alternative syntax you can instead do it this way:</p>
|
| 77 |
|
| 78 | <code><?=$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
|
| 81 | be 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
|
| 87 | written in a simplified format as well. Here is an example using foreach:</p>
|
| 88 |
|
| 89 | <code>
|
| 90 | <ul><br />
|
| 91 | <br />
|
| 92 | <var><?php foreach($todo as $item): ?></var><br />
|
| 93 | <br />
|
| 94 | <li><var><?=$item?></var></li><br />
|
| 95 | <br />
|
| 96 | <var><?php endforeach ?></var><br />
|
| 97 | <br />
|
| 98 | </ul></code>
|
| 99 |
|
| 100 | <p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
|
| 101 | Each 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
|
| 105 | important!</p>
|
| 106 |
|
| 107 | <p>Here is another example, using if/elseif/else. Notice the colons:</p>
|
| 108 |
|
| 109 |
|
| 110 | <code><var><?php if ($username == 'sally'): ?></var><br />
|
| 111 | <br />
|
| 112 | <h3>Hi Sally</h3><br />
|
| 113 | <br />
|
| 114 | <var><?php elseif ($username == 'joe'): ?></var><br />
|
| 115 | <br />
|
| 116 | <h3>Hi Joe</h3><br />
|
| 117 | <br />
|
| 118 | <var><?php else: ?></var><br />
|
| 119 | <br />
|
| 120 | <h3>Hi unknown user</h3><br />
|
| 121 | <br />
|
| 122 | <var><?php endif; ?></var></code>
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 | </div>
|
| 127 | <!-- END CONTENT -->
|
| 128 |
|
| 129 |
|
| 130 | <div id="footer">
|
| 131 | <p>
|
| 132 | Previous Topic: <a href="multiple_apps.html">Running Multiple Applications</a>
|
| 133 | ·
|
| 134 | <a href="#top">Top of Page</a> ·
|
| 135 | <a href="../index.html">User Guide Home</a> ·
|
| 136 | Next Topic: <a href="security.html">Security</a>
|
| 137 | <p>
|
| 138 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 139 | </div>
|
| 140 |
|
| 141 | </body>
|
| 142 | </html> |