Search code examples
phpoopternary

I do not understand this PHP Code, what is it doing?


How to read php

i'm learning php, I wrote this. (It is from a book)

   <?php if(!empty($data)):  ?>
        <ul>
             <?php foreach ($data as $dataprint): ?>
                  <li><?= $dataprint ?></li>
             <?php endforeach ?>
        </ul>

I am unable to understand some of the code in this section.

1. Why are there colons on line 1 and 3?
2. What does the '<?= $dataprint ?> ) and why does it not have the standard 'PHP' word?
3. why is there an equal mark in the next?


Solution

    1. colons - this is a shorthand version of statements, you should not really learn them at such an early stage. First you need to learn the full versions.

    2. <?= $variable ?> is a short version of <?php echo $variable ?> but means exactly the same.

    Also, the shordhand versions from question one are actually not considered a good practice, because when you nest them they are hard to read.