0

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!

  • 1
    In php if you define a variable in a function block, you can access this variable only in that bloke, the variable is not exists outside of that function (or maybe exists if it was defined somewhere else, but without the correct value that you assigned to it in your function). You can read more here: http://php.net/manual/en/language.variables.scope.php . One way to do what you're trying to do is to return the boolean value from `typ__1()`, and assign it to a variable you define in the foreach loop. – Avishay28 Sep 07 '18 at 11:48

2 Answers2

1

Everything is about scope of PHP variables. You should "inject" variable into function like this:

<div>some HTML here</div>

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

     return false;

   }
 ?>

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

http://php.net/manual/en/language.variables.scope.php

0

Add parameter in your function and return a value.

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

 <?php foreach (array_slice($data->something->something, 0, 5) as $day):
     $temperature = $day->temperature;
     $hot=typ__1($temperature);
     if ($hot == true) {
       $bottom = "Shorts";
     } else if ($hot == false) {
       $bottom = "Pants";
     }
     <div><?php echo $bottom ?></div>
 <?php endforeach ?>
Atal Prateek
  • 541
  • 3
  • 7
  • Thanks Atal, this seems to work! Yeah! :D One other question: Could I theoretically also put the second if-statement also into a function outside the foreach? Like: ` something->something, 0, 5) as $day): $temperature = $day->temperature; $hot=typ__1($temperature); clothes();
    `
    –  Sep 07 '18 at 12:01
  • 1
    Again you need to add parameter to your `clothes()` function. Then you can return or echo `$bottom` . – Atal Prateek Sep 07 '18 at 12:09