While loop in php with assignment operator

Asked
Active3 hr before
Viewed126 times

7 Answers

assignment
90%

I'm wondering what does this while statement do? it has an assignment operator within it, so as long as $info gets assigned a value other than false, this code will execute?,Quite, yes. Even there is an assignment operator within that expression, the expression itself still stands for a value. In this case the result of the whole expression is equal to the assignment to $info. In other words: The expression is the same as $info or the expression has been assigned to $info - the last variant is perhaps the best description.,For each record $info will be populated with the current row, until it reaches the end of the result set when it will be set to false (which should stop the while loop).,Making statements based on opinion; back them up with references or personal experience.

great answer from hakre. what is said is that

while ($info = mysql_fetch_array($data_jurisdiction))

will execute in the same way as this

while (mysql_fetch_array($data_jurisdiction) == true)

or even this

$info = mysql_fetch_array($data_jurisdiction);
if ($info == true)
load more v
88%

The while loop executes a block of code while a condition is true.,In the above example, $i hold the value=1, now check the condition while value of ($i<=5). it means it execute the code five times. it print the statement line by line.

Syntax

	while (condition)

	{

	   code to be executed;

	}
<?php
	
$i=1;
	
while($i<=5)
	  
{
	  
echo "The number is " . $i . "<br>";
	  
$i++;
	  
}
  
?>
< ? php

$i = 1;

$sum = 0;

while ($i <= 100)

{

   $sum = $sum + $i;

   $i++;

}

echo "Sum= ".$sum;

?
>
<?php
	
@$num=$_GET['num'];
	
$sum=0;
	
$rem=0;
	
$len=0;
	
while((int)$num!=0)
	 
{
	   
$len++;
	   
$rem=$num%10;
	  
$sum=$sum+$rem;
	  
 $num=$num/10;
	   
}	
	
echo "Length of given digit= " . $len."<br/>";
	
echo "Sum of given digit= " . $sum;
  
?>
	
 <body>
 	
<form>
	 
 Enter Your digit <input type="text" name="num"><br/>
	  
 <input type="submit" value="find the sum">
	
</form>

 </body>		  

load more v
72%

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example: , Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. , The new operator returns a reference automatically, as such assigning the result of new by reference is an error. , The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

Parse error: syntax error, unexpected 'new'(T_NEW) in …
65%

The do-while loop is very similar to the while loop, but it does things in reverse order. The mechanism of the while loop is - while a condition is true, perform a certain action. The mechanism of the do-while loop is - perform a certain action while a condition is true.,In the above code, a variable named num is initialized with the value of 5. The condition in the do-while loop is that while num is less than 25, 5 should be added to num. Once the value of num is greater than 25, the loop will stop executing.,Info About Advertisement Terms and Conditions Guest Post Rule , Terms and Conditions

Syntax:

do {
   // execute this code;
}

while (condition);
load more v
75%

Since PHP evaluates the expression before each iteration, the while loop is also known as a pretest loop.,The while doesn’t require curly braces if you have one statement in the loop body:,If the expression evaluates to false before the first iteration starts, the loop ends immediately.,Then, PHP re-evaluates the expression again. If it’s still true, PHP executes the statement again. However, if the expression is false, the loop ends.

The while statement executes a code block as long as an expression is true. The syntax of the while statement is as follows:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   } < ? php

while (expression) {
   statement;
}
Code language: HTML, XML(xml)
load more v
40%

[... S] o as long as $ info gets a value other than false, will this code be executed?,while $ info gets a value other than false, will this code be executed?,I am wondering what does this do while statement? it has an assignment operator inside it, so if $ info gets a value other than false, will this code be executed?,Therefore, as soon as there are no more lines, the assignment will turn into:

the code I'm looking for does this ...

while ($info = mysql_fetch_array($data_jurisdiction)) { //some stuff } 
load more v
22%

I'm wondering what does this while statement do? it has an assignment operator within it, so as long as $info gets assigned a value other than false, this code will execute?,[... S]o as long as $info gets assigned a value other than false, this code will execute?,Quite, yes. Even there is an assignment operator within that expression, the expression itself still stands for a value. In this case the result of the whole expression is equal to the assignment to $info. In other words: The expression is the same as $info or the expression has been assigned to $info - the last variant is perhaps the best description.,Your while called funktion twice, which probably isn't what you intend - it would read a character twice, check the first one and return the second one. Remember, your funktion is an action, so each time you "invoke" the action (for example by using <- funktion ... in the do notation), the action is run again. So it should rather be something like

the code I'm looking at does this...

while ($info = mysql_fetch_array($data_jurisdiction)) {
   //some stuff
}
load more v

Other "assignment-undefined" queries related to "While loop in php with assignment operator"