When to use IF with ENDIF?

1

In what circumstances should you use:

IF(CONDICAO) :

ELSE:

ENDIF;
    
asked by anonymous 19.12.2017 / 01:37

1 answer

2

Usually the {} keys are used to delimit the scope of a particular block of code.

<?php
    //sintaxe normal
    if (expressão) {
       //código php
    }

    //sintaxe alternativa, repare no uso dos : e da palavra endif
    if (expressão) :
       //codigo php
    endif
?>

Alternative Control Structures

Control structures, such as if, for, foreach, and while, can also be written in a simplified way. Here's an example using foreach:

<ul>

<?php foreach($afazeres as $item): ?>

<li><?=$item?></li>

<?php endforeach ?>

</ul>

Note that there are no keys. Instead, the end key was replaced by an endforeach. Each structure listed above has a similar closing syntax: endif, endfor, endforeach, and endwhile

Note also that instead of using a semicolon for each structure (except the last one), there is a colon. This is important!

Another example, using if / elseif / else. Notice the two points:

<?php if ($username == 'Adri Silva'): ?>

   <h3>Oi Adri Silva</h3>

<?php elseif ($username == 'Leo'): ?>

   <h3>Oi Leo</h3>

<?php else: ?>

   <h3>Oi usuário desconhecido</h3>

<?php endif; ?>

OU

<?php 

if ($username == 'Adri Silva'):

   echo "Oi Adri Silva";

elseif ($username == 'Leo'):

   echo "Oi Leo";

else:

   echo "Oi usuário desconhecido";

endif; 

?>

Alternative Syntax for Control Structures

  

Then you may wonder: is there any difference, performance, and thing and such? This < strong> post addresses this issue

    
19.12.2017 / 02:04