What I'm trying to do is transfer the contents of the array difference() to the array tempStore. The error I am getting is 'Fatal error: Can't use function return value in write context in' on line 4 ($tempStore($x) = $difference($x);). What am i doing wrong here and how do i fix it?
$difference = array("","","","","","","");
for ($x = 1; $x <= 6; $x++){
$tempStore($x) = $difference($x);
}
for ($x = 1; $x <= 6; $x++){
echo $tempStore($x);
}
You just need to use [] instead of ()
for ($x = 1; $x <= 6; $x++){
$tempStore[$x] = $difference[$x];
}
Related
Hello I would like to know if there is away and if this is valid programming to increment the value of array inside echo. Example code is:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_1'])/3600))."</td>";
}
Where I would like to display $row['day_1'], $row['day_2'], $row['day_3'] etc. if there is no way this to be achieved, is there a way to increment the same predefined array with the results of $row like
$time_01[0] = $row['day_1'];
$time_01[1] = $row['day_2'];
So after that to loop through the time_01[] array ?
Least amount of changes: Use the $x you already have:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row["day_$x"])/3600))."</td>";
}
or
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_' . $x])/3600))."</td>";
}
or if you want to put it in another array first, for whatever reason:
for ($x = 1; $x <= $number; $x++) {
$time_01[] = $row['day_' . $x];
}
This really is basic stuff by the way. I do recommend that you follow some basic tutorials about variables, expressions, string concatenation and more.
How to return array in php ? Actually I want to return whole value of $x[] insted of last index of $x[]. Please help me...
<?php
function top() {
require './php/connection.php';
$sql = "SELECT * FROM tbl_add";
$query = mysqli_query($connect, $sql);
$n = 0;
while ($result = mysqli_fetch_assoc($query)) {
$a[$n] = $result['add_id'];
$n = $n + 1;
}
$n = $n - 1;
for ($j = 0; $j < $n; $j++) {
for ($i = 0; $i < $n - 1 - $j; $i++) {
if ($a[$i] > $a[$i + 1]) {
$tmp = $a[$i];
$a[$i] = $a[$i + 1];
$a[$i + 1] = $tmp;
}
}
}
for ($i = 0; $i <= $n; $i++) {
echo $a[$i] . '<br>';
}
$j = 1;
for ($i = 0; $i <= 5; $i++) {
$r = $a[$i];
$sql = "SELECT * FROM tbl_add WHERE add_id='$r'";
$query = mysqli_query($connect, $sql);
$result = mysqli_fetch_assoc($query);
if ($result) {
$x[] = $result['mail'];
return $x[];
}
}
}
?>
return $x[]; is invalid syntax.
In expression $x[] = $result['mail'];, $x[] doesn't mean "the last element of $x". It is just a courtesy of PHP that spares the programmer of writing $x[count($x)]1 instead.
Returning an array is as easy as return $x; (given $x is an array).
Btw, there is no place in your code where $x is initialized as array. You just add values to some variable that doesn't exist, using the array syntax. PHP helps you and creates an array first and stores it in the $x variable but this practice is strongly discouraged. You should add $x = array(); somewhere before you use $x for the first time (outside the loop, of course). For example, you can put it before the line for ($i = 0; $i <= 5; $i++) {.
`
1 This statement is not entirely correct. However, if the values are added to the array using only the $x[] = ... syntax (as it happens in the posted code) then it is correct.
You have to return $x
Then when you call this function $data = top();
Now you get return data of function top to variable name data
// the code below will return $x as it is, independent of what it is. Array, integer, string etc..
Return $x;
// if you need to return two values use:
Return array($x, $y);
// again bit variables are returned as they are.
To call the function and get the values/array use:
$array = top();
Var_dump($array); //should be $x from your function
So I have a list with variables (auto-generated), something like:
$won3 = 1;
$time3 = 4;
$won6 = 0;
$time6 = 5;
$won4 = 0;
$time4 = 5;
(...)
but with many more variables. Now I want to make a table with all the variables, so I used a for-loop, but $won1 has to be the first in the table, then $won2 etc...
But how can I recall this $won1 in a for-loop? I tried:
for ($X = 0, $X < $Y, $X++){
echo '$won'.$X;
}
but this does not do the job. Anyone knows how I can solve this?
Thanks in advance.
First... In for loops you can't use ,
for ($X = 0, $X < $Y, $X++){
Try this:
for ($X = 0; $X < $Y; $X++){
.
And you have to choices...
for ($X = 0; $X < $Y; $X++){
$var = 'won' . $X;
echo $$var;
echo ${'won' . $X};
}
I'm working on another developers old code and there are tons of Notice: Undefined index errors when data is being set using the += operator. Essentially the index is not set yet in the array so the error is generated.
Example:
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
$myArray['test'] += 1;
}
Will generate an error on the first run since the test index is not set yet.
I know I can remove this error with the following code:
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
if ( ! isset($myArray['test']) )
{
$myArray['test'] = $myValue;
}
else
{
$myArray['test'] += $myValue;
}
}
However, there are around 50 of things like this to change. Is it worth writing all these isset statements or is there a better way I am not aware of?
EDIT: I should note that the array indexes aren't always the same and sometimes aren't set so I can't preset the index in this circumstance to avoid the error.
This is a bit shorter, but perhaps still a bit complicated if you have many edits.
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
isset($myArray['test']) ? $myArray['test'] += $myValue : $myArray['test'] = $myValue;
}
You could also write a global function (not tested)..
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
increment($myArray['test'], $myValue);
}
function increment(&$var, $inc){
$var = isset($var) ? $var += $inc : $var = $inc
}
If you are using PHP 7 and above, you can use the Null Coalescing Operator to make this code cleaner.
$myArray = [];
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
$myArray['test'] = $myValue + ($myArray['test'] ?? 0);
}
The benefit here is not only that the code is cleaner, but you're also being more explicit about the default value (0)
Old/Deprecaded/Unrecommended but the shortest solution is
#$myArray['test'] += $myValue;
echo $xml->SLOT1->Effect;
echo $xml->SLOT2->Effect;
echo $xml->SLOT3->Effect;
Is there a way to simplify this by using a for loop? I tried this but it echos nothing:
for ($x = 1; $x <= 3; $x++) {
echo $xml->SLOT[$x]->Effect;
}
You can use
$xml->{"SLOT".$x}->Effect;
for ($x = 1; $x <= 3; $x++) {
echo $xml->{SLOT.$x}->Effect;
}