vendredi 7 septembre 2018

PHP – use outside function with if-statements inside foreach-loop

I'm new to PHP, please be gentle. What do I need to change to make this work in PHP?

 <div>some HTML here</div>

 <?php
   function typ__1() {
     if ($temperature >= 29) {
       $hot = true;
     } else {
       $hot = false;
     }
   }
 ?>

 <?php foreach (array_slice($data->something->something, 0, 5) as $day):
     $temperature = $day->temperature;
     typ__1();
     if ($hot == true) {
       $bottom = "Shorts";
     } else if ($hot == false) {
       $bottom = "Pants";
     }
     <div><?php echo $bottom ?></div>
 <?php endforeach ?>

So the main issue/question is if I'm using the function correctly. Can I write if-statements in an outside function and then use them inside a foreach-loop? The reason/goal is to shorten the foreach-loop.

(This is a reduced example, so there could be a typo somewhere in there.)

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire