Variable containing loop [closed] - 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);
?>

Related

PHP update my Data keeps Appending whats wrong with my code [closed]

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

Loop until variable is isset then excute a code [closed]

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

How to echo every var more than $var? [closed]

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
I have
$room=$_POST['room'];
$lastMTime=$_POST['lastMTime'];
function _getLastMsg($rName, $lMT){
$r=$rName.'.xml';
$rXML=simplexml_load_file($r) or die('Not found');
$rLMT=$rXML->lastMT;
if($lMT<$rLMT){
//some code here
}
}
How do I echo every $rXML->username that has $rLMT > $lMT?
I want to get all usernames that have been submitted after $lMT.
may be you can try this code
<?php
$var = [11, 10, 20, 30, 40];
for($i=0; $i<count($var)-1; $i++) {
if($var[$i]>$var[($i+1)]) {
echo "var $var[$i] > var ".$var[($i+1)];
}
}
?>
First of all make sure that all these vars should not be an array instead. When you have multiple variables which are handled in the same way usually it is a clear sign that they should be an array instead.
If not - you can use compact function to make variable names into array with their names and values, read the manual on it. Other way to access variable value by its name is variable variables - $$
Using the variables into your example you can do this
$i = 0;
while ($i <= 3) {
$varName = 'var'.$i;
if ($$varName > $var) {
echo $$varName;
}
$i++;
}
But that is no right way to iterate over an array. There exists a million other ways to iterate over a bunch of variables and print them, all of them are not the "right" way to do it. In your example code there is no array, where there should be one. Like this
$var = 11;
$vars = [10, 20, 30, 40];
# iterate and echo each element of $vars array)
foreach ($vars as $current) {
if ($current > $var) {
echo $var;
}
}

php initialize if not set [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find myself doing the following quite a bit:
if (isset($objs[$key]){
$objs[$key] += val;
} else {
$objs[$key] = $val;
}
Is there a better way to do that?
Another way could be:
$objs[$key] = isset($objs[$key]) ? $objs[$key] + $val : $val
Is there a better way?
The easiest way to simplify could be a function taking the array by reference and deduplicate the code.
If you are using php 7 you can also make use the new ?? operator (assuming all values are integers):
$arr[$key] = ($arr[$key] ?? 0) + $value;
However, if you know the shape of the array you should base your actions on that. Create the array using array_fill_keys or similiar in that case.
If you are always adding numbers... you could do something like this to keep a single line of code for each entry which may or may not be preferable for you to your existing solution.
$objs = array();
addvalue($objs, "dog", 2);
addvalue($objs, "dog", 5);
// $objs['dog'] will = 7
function addvalue(& $var, $key, $value){
if (isset($var[$key])){
$var[$key] += $value;
} else {
$var[$key] = $value;
}
}

${'varname_'.$x} variable in PHP [closed]

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 8 years ago.
Improve this question
Can someone please remind me how you call this type of variable
${'varname_'.$x}
Thanks!
It's called variable variables!
This should do it:
$varname = 'varname_'.$x;
echo $$varname;
variable varibales!!!
$var = "varname$x";
for($x=0;$x<10;$x++)
{
echo $$var;
}
edit ====
SHOULD BE:
for($x=0;$x<10;$x++)
{
$var = "varname$x";
echo $$var;
}
Variable variables. Your example could be used like so:
$varname_0 = 'Hello World!';
$x = 0;
echo ${'varname_'.$x}; // 'Hello World!'

Categories