php do while not equal not ending - php

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

Related

Unexpected behaviour of "!print("1") || 1" in php

Example1:
if(!print("1") || 1){
echo "a";
}else{
echo "b";
}
Output
1b
The Example 1 is printing "1b" instead of "1a". According to me, inside if the final condition should be if(0 || 1) after solving !print("1").
But the Example 2 is printing "1a".
Example 2:
if((!print("1")) || 1){
echo "a";
}else{
echo "b";
}
Output
1a
Can you elaborate, why the or condition in the first statement didn't work.
The key thing here is to realise that print is not a function, and doesn't take arguments in parentheses - the parentheses aren't optional, they're just not part of the syntax at all.
When you write print("1"); the print statement has a single argument, the expression ("1"). That is if course just another way of writing "1" - you could add any number of parentheses and it wouldn't change the value.
So when you write print("1") || 1 the argument to print is the expression ("1") || 1. That expression is evaluated using PHP's type juggling rules as true || true which is true. Then it's passed to print and - completely coincidentally to what you were trying to print - is type juggled to the string "1".
The print statement is then treated as an expression returning true, and the ! makes it false, so the if statement doesn't run.
This is a good reason not to use parentheses next to keywords like print, require, and include - they give the mistaken impression of "attaching" an argument to the keyword.

PHP short if statement with continue key word and ommit the else part

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";
}
?>

compare Carbon's isSameDay method create infinite loop in For statement

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)) {
}

Difference between an If statement and While loop

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.

does the condition after && always get evaluated

I have this if statement that tests for the 2 conditions below. The second one is a function goodToGo() so I want to call it unless the first condition is already true
$value = 2239;
if ($value < 2000 && goodToGo($value)){
//do stuff
}
function goodToGo($value){
$ret = //some processing of the value
return $ret;
}
My question is about the 2 if conditions $value < 2000 && goodToGo($value). Do they both get evaluated or does the second one only get evaluated when the first one is true?
In other words, are the following 2 blocks the same?
if($value < 2000 && goodToGo($value)) {
//stuff to do
}
if($value < 2000) {
if (goodToGo($value)){
//stuff to do
}
}
No--the second condition won't always be executed (which makes your examples equivalent).
PHP's &&, ||, and, and or operators are implemented as "short-circuit" operators. As soon as a condition is found that forces the result for the overall conditional, evaluation of subsequent conditions stops.
From http://www.php.net/manual/en/language.operators.logical.php
// --------------------
// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
Yes. The two blocks are the same. PHP, like most (but not all) languages, uses short-circuit evaluation for && and ||.
The two blocks ARE same.
PHP logical operators are "lazy", they are evaluated only if they are needed.
The following code prints "Hello, world!":
<?php
$a = 10;
isset($a) || die ("variable \$a does not exist.");
print "Hello, world!"
?>
Other logical operators includes &&, and, or.
<?php
perform_action() or die ('failed to perform the action');
?>
is a popular idiom.
the second condition will only be checked if and only if first one is true, hence both statements are equivalent.
Yes, the 2 code blocks you gave are equivalent. PHP has short-circuiting, so when you use
|| and &&, any statement after the first only gets evaluated when necessary.
Always corelate your technical Language with your own language, Likewise here, If I say you in verbal conversation, its just like :You are asking= "if I am hungry '&&' I am eating Pizza" is similar to "If I am hungry then only i am eating Pizza"?
So here you can see that later phrase says that untill i am not hungry i am not eating pizza, and the former says I am humgry and I am eating pizza.
:-)

Categories