PHP Loop to display unique message for each day

Asked
Active3 hr before
Viewed126 times

7 Answers

messagedisplay
90%

I have 24 days which have unique messages each day. How could i write the following IF statements in some sort of loop to make it more efficient: -, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Stack Overflow Public questions & answers , Meta Stack Overflow

You can do this inline like this:

$offerTxt = $ {
   'day'.$day.
   'Txt'
};

You should probably check whether the day is in a certain set, so your code would come out looking similar to this:

$daysUsed = array(1, 2, 3, 4, 5, 6);
$offerTxt = '';
if (in_array((int) $day, $daysUsed)) {
   $offerTxt = $ {
      'day'.$day.
      'Txt'
   };
}
load more v
88%

PHP Loop to display unique message for each day, what does the operator ":\" do in this scala code? Scala | 4 months ago

1.2.$offerTxt = $ {
   'day'.$day.
   'Txt'
};
3.4.$daysUsed = array(1, 2, 3, 4, 5, 6);
5. $offerTxt = '';
6.
if (in_array((int) $day, $daysUsed)) {
   7. $offerTxt = $ {
      'day'.$day.
      'Txt'
   };
   8.
}
9.
72%

$begin = new DateTime("2015-07-03");
$end = new DateTime("2015-07-09");

for ($i = $begin; $i <= $end; $i - > modify('+1 day')) {
   echo $i - > format("Y-m-d");
}
65%

The php foreach loop is used to iterate through array values. It has the following basic syntax, The foreach… loop is used to loop through arrays,It has the following syntax,Let’s now look at them separately. For loop It has the following basic syntax

Let’s now look at them separately. For loop It has the following basic syntax

< ? php
for (initialize; condition; increment) {

   //code to be executed

} ?
>
load more v
75%

So that's the story of conditional control structures. We'll discuss loops in PHP in the next section.,Today, we're going to discuss control structures and loops in PHP. I'll show you how to use all the main control structures that are supported in PHP, like if, else, for, foreach, while, and more.,Let's take a look at a few of these control structures with examples.,The following flowchart explains how a control structure works in PHP.

Let's have a look at the following example to understand how it actually works.

< ? php
$age = 50;

if ($age > 30) {
   echo "Your age is greater than 30!";
} ?
>
load more v
40%

Be sure to always use the mb_* functions instead of the old string functions (make sure the “multibyte” extension is included in your PHP build).,On the server side of our example, we simply dump the $_POST array:,In PHP there’s no char datatype; only string is available. With that in mind, incrementing the string z in PHP yields aa:,Therefore, if we run the above test using mysqlnd rather than mysql, we get a much more realistic picture of our memory utilization:

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
load more v
22%

In the following chapters you will learn how to repeat code by using loops in PHP.,Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.,In PHP, we have the following loop types:,The following chapters will explain and give examples of each loop type.

load more v