CI framework--using PHP alternative syntax in view files

Source: Internet
Author: User
Tags php error php foreach codeigniter

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.