I am looping though 2 dates, and print out all the day in between.
below is my For Loop using carbon. the loop won't stop even when isSameDay return true.
$check_in = new Carbon($check_in);
$check_out = new Carbon($check_out);
for(
$check_in;
$check_in->addDay();
$check_in->isSameDay($check_out)
) {
print_r($check_in->day);
}
I tested the following code
print_r($check_in->addDay(4)->isSameDay($check_out));
// 12/20 + 4 day = 12/24 return true.
so I refer back to for loop's documentation
The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.
In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.
At the end of each iteration, expr3 is evaluated (executed).
so my order is wrong it should be
for($check_in; $check_in->lte($check_out); $check_in->addDay(1)) {
}
Related
trying to use a do while loop but it never finishes. Can't figure out why
<?php
$a = "2018-11-28T14:36:27+00:00";
$b = "2018-11-28T14:43:27+00:00.";
if ($a !== $b) {
echo "not equal";
}
else {
echo "equal";
}
do {
echo "a";
} while ($a !== $b);
do while statement :
"The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once."
additionnal explaination from php manual
"do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately). "
because do while body is exectued at least one time it will echo "a" but your condition is always true and you don't change your values into do while body; then you will get an infinite loop
I have a for loop like
for ($x=1; $x<=5; $x++){
($x == 3)? continue : true;
//some code here
}
now on execution I am getting error
PHP Parse error: syntax error, unexpected 'continue' (T_CONTINUE) in
/var/www/html/all.php on line 21
Now, this leave me with 2 questions:
Can I use continue key word inside short if statement?
For the else part of the short if, can binary values like true or false be used, and if not then how can I use short if statement if I have nothing to do for the else part.
continue is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.
cond ? a : b means use value a if cond is true else use value b. If a is continue, there's no value there.
true does result in a value (true), so yes, it can be used as part of an expression.
You cannot use continue inside short if-statement. Short if-statements is for returning values, like this
$val = $bool ? $one : $two;
Now, $val will have either the value of $one or the value of $two, depending of the truth value of $bool.
continue is no value, so it cannot be used in short if-statement. Use normal if-statement for this operation.
In this case, I would have done it like this:
for ($x=1; $x<=5; $x++){
if($x == 3) continue;
//some code here
}
You can use continue key word inside if statement like this; (according to PHP documentation)
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
?>
I'am working in CodeIgniter, and want to create a dynamic breadcrumbs by using $this->uri->segment($i) function.
How should I write correctly for() statement if I want to test the 2nd expression that is not equal to FALSE ? It gives me an infinite loop and I don't know why.
here is my code:
for($i = 1; $i !== FALSE; $i++){
var_dump($this->uri->segment($i));
}
For exemple, first 3 reccursion should output a different strings, starting from 4th reccursion, it gives me false but it's not working here, know someone why ?
Your $i variable is an integer, and will never be equal to FALSE.
Maybe you are looking for comparing $this->uri->segment($i)?
for($i = 1; $this->uri->segment($i) !== FALSE; $i++){
var_dump($this->uri->segment($i));
}
I read this Manual by PHP.com about While loops.
I don't understand the purpose of While loops in PHP.
It looks exactly like an if statement to me.
What is the difference between an if statement and a while loop?
How do while loops work, what do they do, and when should I use them?
For example, can't this:
$i = 1;
while ($i <= 10) {
echo $i++;
}
be done like this?:
$i = 1;
if ($i <= 10) {
echo $i++;
}
An if statement checks if an expression is true or false, and then runs the code inside the statement only if it is true. The code inside the loop is only run once...
if (x > y)
{
// this will only happen once
}
A while statement is a loop. Basically, it continues to execute the code in the while statement for however long the expression is true.
while (x > y)
{
// this will keep happening until the condition is false.
}
When to use a while loop:
While loops are best used when you don't know exactly how many times you may have to loop through a condition - if you know exactly how many times you want to test a condition (e.g. 10), then you'd use a for loop instead.
A while loop will run as many times as it needs to while a condition is true, i.e., until that condition is false.
An if statement will execute once if a condition is true.
A great way to understand concepts like this when you're just learning a language is to try them out:
<?php
$i = 1;
while ($i <= 10) {
echo $i++;
}
echo "\n";
$i = 1;
if ($i <= 10) {
echo $i++;
}
This results in:
12345678910
1
if command is only run in one condition in one time and its execute in only one statement in one time
while loop is manly use in infinite time for looping a statement while is executed in many statement in one time
Here's an example:
Suppose you want a script that loops through an array and make one beep sound for each element of the array.
A WHILE loop would generate no beeps for an empty array.
An FOR loop will always run at least once, so an empty array would generate one beep.
here is what i'm trying to achieve:
if $x is either of these 3 values: 100, 200 or 300 - do something
I'm doing this:
if($x==("100"||"200"||"300"))
{
//do something
}
but //do something is executed even if $x is 400
I noticed that this works:
if($x=="100"||$x=="200"||$x=="300")
{
//do something
}
How is the first block of code different from the second block of code? What am I doing wrong?
The reason why your code isn't working is because the result of the expression:
('100' || '200' || '300')
is always TRUE because the expression contains at least one truthy value.
So, the RHS of the expression is TRUE, while the LHS is a truthy value, therefore the entire expression evaluates to TRUE. The reason why this is happening is because of the == operator, which does loose comparison. If you used ===, the resulting expression would always be FALSE. (unless of course the value of $x is false-y.)
Let's analyze this:
Assuming $x equal '400':
($x == ('100'||'200'||'300'))
// ^ ^
// true true
Make sense now?
Bottom line here is: This is the wrong way of comparing 3 values against a common variable.
My suggestion is that you use in_array:
if(in_array($x, array('100', '200', '300')) {
//do something...
}
you can take all values in array it is working Perfectly.
$x=400;
if(in_array($x, array('100', '200', '300'))) {
echo $x.'is in array';
} else {
echo $x.'is not in array';
}