blob: 418d2e6eb4eff010a1fb9bb06aa2e7e65b8c9a44 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###################################
2Alternate PHP Syntax for View Files
3###################################
4
5If you do not utilize CodeIgniter's :doc:`template
6engine <../libraries/parser>`, you'll be using pure PHP in your
7View files. To minimize the PHP code in these files, and to make it
8easier to identify the code blocks it is recommended that you use PHPs
9alternative syntax for control structures and short tag echo statements.
10If you are not familiar with this syntax, it allows you to eliminate the
11braces from your code, and eliminate "echo" statements.
12
13Automatic Short Tag Support
14===========================
15
16.. note:: If you find that the syntax described in this page does not
17 work on your server it might be that "short tags" are disabled in your
18 PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly,
19 allowing you to use that syntax even if your server doesn't support it.
Andrey Andreev16a704c2012-11-09 17:25:00 +020020 This feature can be enabled in your *config/config.php* file.
Derek Jones8ede1a22011-10-05 13:34:52 -050021
22Please note that if you do use this feature, if PHP errors are
23encountered in your **view files**, the error message and line number
24will not be accurately shown. Instead, all errors will be shown as
Andrey Andreev16a704c2012-11-09 17:25:00 +020025``eval()`` errors.
Derek Jones8ede1a22011-10-05 13:34:52 -050026
27Alternative Echos
28=================
29
30Normally to echo, or print out a variable you would do this::
31
32 <?php echo $variable; ?>
33
34With the alternative syntax you can instead do it this way::
35
36 <?=$variable?>
37
38Alternative Control Structures
39==============================
40
41Controls structures, like if, for, foreach, and while can be written in
Andrey Andreev16a704c2012-11-09 17:25:00 +020042a simplified format as well. Here is an example using ``foreach``::
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Derek Jonesaf8da302011-10-05 17:40:07 -050044 <ul>
45
46 <?php foreach ($todo as $item): ?>
47
Andrey Andreev16a704c2012-11-09 17:25:00 +020048 <li><?=$item?></li>
Derek Jonesaf8da302011-10-05 17:40:07 -050049
50 <?php endforeach; ?>
51
52 </ul>
Derek Jones8ede1a22011-10-05 13:34:52 -050053
54Notice that there are no braces. Instead, the end brace is replaced with
Derek Jonesaf8da302011-10-05 17:40:07 -050055``endforeach``. Each of the control structures listed above has a similar
56closing syntax: ``endif``, ``endfor``, ``endforeach``, and ``endwhile``
Derek Jones8ede1a22011-10-05 13:34:52 -050057
58Also notice that instead of using a semicolon after each structure
59(except the last one), there is a colon. This is important!
60
Derek Jonesaf8da302011-10-05 17:40:07 -050061Here is another example, using ``if``/``elseif``/``else``. Notice the colons::
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Andrey Andreev16a704c2012-11-09 17:25:00 +020063 <?php if ($username === 'sally'): ?>
Derek Jonesaf8da302011-10-05 17:40:07 -050064
Andrey Andreev16a704c2012-11-09 17:25:00 +020065 <h3>Hi Sally</h3>
Derek Jonesaf8da302011-10-05 17:40:07 -050066
Andrey Andreev16a704c2012-11-09 17:25:00 +020067 <?php elseif ($username === 'joe'): ?>
Derek Jonesaf8da302011-10-05 17:40:07 -050068
Andrey Andreev16a704c2012-11-09 17:25:00 +020069 <h3>Hi Joe</h3>
Derek Jonesaf8da302011-10-05 17:40:07 -050070
71 <?php else: ?>
72
Andrey Andreev16a704c2012-11-09 17:25:00 +020073 <h3>Hi unknown user</h3>
Derek Jonesaf8da302011-10-05 17:40:07 -050074
Andrey Andreev16a704c2012-11-09 17:25:00 +020075 <?php endif; ?>