If you do not use the CodeIgniter template engine, then you can only use the pure PHP syntax in the view file. To streamline the view file to make it more readable, it is recommended that you use PHP's alternative syntax when writing control structures or ECHO statements. If you're not familiar with this syntax, here's how to use this syntax to eliminate curly braces and echo statements in your code.
Auto Short tag Support
If you find that the syntax described on this page does not work on your server, it is possible that the "short tag" is disabled in your php.ini file. CodeIgniter can dynamically rewrite all the short tags so that you can use the short tag syntax even if your server does not support it. This feature can be enabled in the config/config.php file.
Note that if you use this feature, the error message and line number will not be displayed correctly when a PHP error occurs in your view file, as all errors are displayed as eval () errors.
Echo substitution syntax
In general, you will use the following method to print a variable:
<?php echo $variable;?>
Using alternative syntax, you can write this:
<?= $variable?>
Alternative syntax for control structures
Control structures such as if, for, foreach, and while can also be written in a streamlined format. The following is an example of foreach :
< ul > <? php foreach ($todo as $item): ?> < li > <? = $item ?> </ li > <? php Endforeach; ?> </ ul >
Note that there are no parentheses here. All the closing brackets are replaced with Endforeach . The control structures mentioned above also have this similar closing sign:endif , endfor ,Endforeach , and endwhile .
It is also important to note that each branch structure is followed by a colon instead of a semicolon (except for the last one), which is a significant point!
Here is another example, using the if/ElseIf/Else , note the location of the colon:
<?php if ($username = = = ' Sally '):?> <H3>Hi Sally</H3><?php elseif ($username = = = ' Joe '):?> <H3>Hi Joe</H3><?php Else:?> <H3>Hi Unknown user</H3><?PHP endif;?>
CI framework--using PHP alternative syntax in view files