-1

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?

pandaJuan
  • 91
  • 1
  • 11
Vina L
  • 49
  • 5
  • Your question is too broad. Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. – Quentin Dec 08 '17 at 10:51
  • 2. is just a [short-hand](https://stackoverflow.com/questions/2020445/what-does-mean-in-php) echo – DarkBee Dec 08 '17 at 10:51

3 Answers3

4
  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.

Michał Skrzypek
  • 699
  • 4
  • 14
  • 1
    The only addition I could add to this would be to avoid using shorthand and code it the normal method. Code indenting is always best to practice too so your code is readable. I am not a fan at all of shortcode for php (but this is personal preference). – Option Dec 08 '17 at 11:07
  • 1
    not only that, it is a foundation for MVC approach :) I agree with you. – Michał Skrzypek Dec 08 '17 at 11:09
1

Colons on lines 1 and 3 are an alternative way of doing the below code

if (!empty($data)) {
    ...
}

It makes it a lot cleaner when outputting HTML.

(See http://php.net/manual/en/control-structures.alternative-syntax.php)

<?= is a shorthand way of writing <?php echo, again it's just an alternative way of doing things.

(See http://php.net/manual/en/function.echo.php)

MCMXCII
  • 1,043
  • 4
  • 13
  • 26
0

You can write loops in php many type... 1)eg: if(condition){...code...}else{...code...} or if(condition): means, in place of "{" you can use ":" and for ending the loop use "endif" 2) Your point no 2 is written in shortcode in php for that you have to enabled shortcode in php.ini