Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to do a while loop but something is missing I need to loop until variable is isset then execute a code something like dis :
$counter=o;
while(null!==($var)){
$counter ++;
}
if (isset($var)){
excute code ....
}
You mean like this?
$counter = 0;
while (!isset($var)) {
$counter ++;
echo $counter, PHP_EOL;
if ($counter == 10) {
$var = true;
}
}
echo 'Done', PHP_EOL;
I suggest you don't use the existance of a variable to control your logic flow - using something like a break command to kill your potentially infinite loop might be better, for example:
while (true) {
// do something
if ($someCondition) {
break;
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Can someone tell me what is wrong with my code? PHP update my Data keeps Appending
<?php
foreach($_SESSION["product"] as $index => $product)
{
if ($_SESSION["product"][$index]["item_id"] == $_POST["item-id"])
{
$found = 1;
$target_index = $index + 1;
}
}
if (true)
{
array_push($_SESSION["product"], ["item_id" => $_POST["item-id"], "item_qty" => $_POST["item-qty"]]);
}
else
{
$qty = strval(($_SESSION["product"][$target_index]["item_qty"]) + $_POST["item-qty"]);
$_SESSION["product"][$target_index]["item_qty"] = $qty;
$qty = 0;
}
}
If you are asking why it always appends to the array instead of executing the else block, it looks like you have a condition missing in the line that says:
if(true)
This will always evaluate to true no matter and as a result it will always append to your array.
You probably want to put some sort of condition there
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to do something like this:
if ($boolean == true) {
foreach ($variables as $variable) {
}
// some code that can be run either with a loop or without
if ($boolean == true) {
} // end the foreach loop
}
Or is there another way to do this without rewriting the same code twice just to satisfy all possibilities?
The conventional way is to always use a loop. If there is only one item, then you can still loop just once.
Contrived example
$values = …;
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $value) {
// Do your work
}
I'm not sure if I understand exactly what you're asking, but if you want to run a loop at least once, and continue looping for a condition then "Do While" is your answer. A do while works just like a while, but checks AFTER the first loop is run - meaning it always runs at least once.
PHP Docs for Do While
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.
Example:
$arrayofstuff = array('one ','two ','three '); // Optional array of stuff
$i=0; // Counts the loops
echo 'starting...';
do {
// Do stuff at least once here
// Array stuff if needed
if(isset($arrayofstuff[$i])) {
echo $arrayofstuff[$i]; // Uses loop # to get from array
} else {
break; // ends loop because array is empty
}
$i++;
} while (true);
For what it's worth, forcing a variable into a single value array would probably be easier to read. As you can see there's a lot going on here for such a simple task.
(Dionne Warwick voice) That's what functions are for:
function doSomething() {
//whatever
};
if ($boolean)
for($variables as $variable) doSomething();
else
doSomething();
That kind of syntax you thought about isn't valid in PHP. It's a very clever idea though. But code maintenance of one of there would bring hell on earth. Better forget it.
why not just plain:
if($boolean){
foreach($a as $b){
// do stuff
}
}else{
// do other stuff
}
What you want would be done like so...
foreach ($variables as $variable) {
// some code that can be run either with a loop or without
if ($boolean !== true) {
break;
}
}
But this is not as readable as just using an if/else statement
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my page to display an error message "lorem ipsum" if the input for $a (from the $_POST super global) is greater than a defined maximum value ($max1 and $max2). So this is what I did:
$max1 = 2.7432;
$max2 = 274.32;
$a = $_REQUEST['a'];
function maximum(){
if ($a>$max1 || $a>$max2){
echo "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
return maximum();
}
}
then I call the function here like so:
if(($fred=="fred") && ($george=="george") {
//check validity of input first by calling maximum;
maximum();
}
This is not working! What am I doing wrong?
This is a scope issue. $a, $max1, and $max2 is not in scope inside of your function. You need to pass it as a parameter for it to be available (i.e. in scope) within your function.
Also, your function doesn't work. In a nutshell you need to return the string from your function and then capture or echo it out when calling the function.
$max1 = 2.7432;
$max2 = 274.32;
$a = $_REQUEST['a'];
function maximum($a, $max1, $max2){
if ($a>$max1 || $a>$max2){
return "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
}
}
if(($fred=="fred") && ($george=="george")) {
//check validity of input first by calling maximum;
echo maximum($a, $max1, $max2);
}
Please change your function like bellow and check the comments what mistake you did
function maximum()
{
global $max1,$max2,$a;//make $a,$max1,$max2 available inside this function
if ($a > $max1 || $a > $max2) {
echo "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
//return maximum(); remove this line. Otherwise itwill go infinte loop
}
}
http://php.net/manual/en/language.variables.scope.php
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am not sure if this is even possible or if it makes sense but I want to set a variable to contain this loop:
<?php
do{
$x = $x + 1;
} while ($x<=5);
?>
Can this be accomplished with a variable variable?
I suspect you're looking to create a function:
function fun($x) {
do {
$x = $x + 1;
} while ($x <= 5);
return $x;
}
Then in your code you can simply write:
$x = fun($x);
Do you mean like this?
<?php
$varVar = rand(0,10);
for ($i=1; $i<=$varVar; $i++)
{/*the loop*/}
?>
I'm not really sure what you mean, unless you're after something similar to the block syntax of Objective-C?
I'm presuming you're new to PHP, and if so, are you sure you're not looking for functions? What you've done above will make the value of $x equal 5, if you wanted to do this with a function it would look like:
<?php
function doSomething($count) {
do {
// do something here
} while ($x <= $count);
}
doSomething(5);
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.