.= syntax php undefined variable [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I got this error message
Undefined variable: x in ../../../../.php on line 35
I get the error on this line.
$x .= $y->getContent();
This line of code is in a foreach loop.
How do I get rid of the error message.
If I replace the .= with just = I'm not getting the correct output.
I hope I provided enough information
And what does .= do?
Thanks in advance.

.= is used (in your code) to concatenate the value of $x with result of ->getContent() call on $y and write the result back into $x.
Is like write $x = $x.$y
Of course if $x does not exists (like in your example I suppose; with "not exists" I mean that hasn't a value), regardless how you wrote your expression, this will fail. Moreover, $x and $y will be considerated strings so, please pay attention to your variables type (you can't concatenate two object, for example)

$x.=$y is a shortcut for $x=$x.$y
so if $x = 'cat' and $y = 'fish' then the result of $x.=$y is 'catfish'
As to your error, you need to create the variable $x 1st, out side the loop:
$x='';
foreach($var as $y){
$x.=$y;
}

$x = '';
foreach()
{
$x .= $y->getContent();
}
*it is must to define $x becouse you are append value in $x *

Define x before your loop :
$x = '';

The .= operator is a string operator to concatenate strings.
$x .= $yis the same as $x = $x.$y
You could read about string operator
s in the official PHP reference
If $x don't exist you can't concatenate it with $y, this is the reason of the error message.

Related

Unusual string collision in PHP [duplicate]

This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 4 months ago.
I have an array of lower case hex strings. For some reason, in_array is finding matches where none exist:
$x = '000e286592';
$y = '0e06452425';
$testarray = [];
$testarray[] = $x;
if (in_array($y, $testarray)) {
echo "Array element ".$y." already exists in text array ";
}
print_r($testarray);exit();
The in_array comes back as true, as if '0e06452425' is already in the array. Which it is not. Why does this happen and how do I stop it from happening?
Odd. This has something to do with the way that PHP compares the values in "non-strict" mode; passing the third argument (strict) as true no longer results in a false positive:
if (in_array($y, $testarray, true)) {
echo "Array element ".$y." already exists in text array ";
}
I'm still trying to figure out why, technically speaking, this doesn't work without strict checking enabled.
You can pass a boolean as third parameter to in_array() to enable PHP strict comparison instead of the default loose comparison.
Try this code
$x = '000e286592';
$y = '0e06452425';
$testarray = [];
$testarray[] = $x;
if (in_array($y, $testarray, true)) {
echo "Array element ".$y." already exists in text array ";
}
print_r($testarray);exit();

warning a non-numeric value encountered in php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
save data that an error occurred, but the data is still stored.
how do you get rid of these errors?
$query1 = "SELECT * FROM t_ref_pokja WHERE status='1' ORDER BY id";
$hasil1 = mysqli_query($koneksi,$query1);
if ($num_rows1 = mysqli_num_rows($hasil1)) {
while($list1 = mysqli_fetch_array($hasil1)){
$no = $list1['id'];
$a= "wisuda_$no";
$b= "RT_$no";
$c= "OPR_$no";
$d= "PI_$no";
$e= "Strat_$no";
$f= "pagu_$no";
$g= "save_$no";
$nama=$_POST[$f];
$wisuda1 = $_POST[$a];
$wisuda = str_replace(",", "",$wisuda1);
$rt1 = $_POST[$b];
$rt = str_replace(",", "",$rt1);
$opr1 = $_POST[$c];
$opr = str_replace(",", "",$opr1);
$pi1 = $_POST[$d];
$pi = str_replace(",", "",$pi1);
$strat1 = $_POST[$e];
$strat = str_replace(",", "",$strat1);
$save = $_POST[$g]; //line 66
$total =($wisuda+$rt+$opr+$pi+$strat); //line 67
$tahun = date("Y");
if($save == "save"){
enter image description here
please help me
First, these are notices and warnings rather than errors. As you have seen, code execution will continue in these scenarios whereas script execution would terminate in case of an error.
There are at least two different issues here:
You are accessing an array index that does not exist(line 66)
You are using arithmetic operators on non-numeric values(line 67)
For 1, you should check that the key exists in the $_POST array before accessing it. I am assuming since it is absent in the request, the "save" parameter is optional. Since it is not present, null will be assigned to $save, so presumably this is the desired value for later logic. You can simply add a condition using array_key_exists or isset to prevent the notice. If you are you using php 7 or later you can use the "null coalesce" operator(??).
$save = array_key_exists($g, $_POST)?$_POST[$g]:null;
OR
$save = $_POST[$g]??null;
For 2, I would need more information about the values and intentions thereof. Judging by the addition operator and comma replacement, it appears that you are accepting numeric values that are formatted in some way. PHP will implicitly convert numeric strings to numbers in cases such as this. You may need to remove further formatting. Perhaps intval or floatval would be applicable depending on the input data. Ultimately, you need to extract the numbers that you need from the strings and convert them to numeric values before performing addition(unless you are attempting string concatenation, in which case see the . operator)

Two variable variables in one variable

I have a for-loop ($x++) within another for-loop ($i++) , and I want both $x AND $i to be part of a variable variable:
${'name'.$x.'place'.$i.''} = ...;
Such that I get:
$name1place1
$name1place2
$name1place3
$name2place1
$name2place2
$name3place1 etc. etc.
However, setting variables in the way quoted above does NOT work for me (i.e. with single quotations and two variable variables). I get the error "Notice: Undefined variable [...]".
The following works:
${"name$x"} = ...;
(using double quotations and just one variable variable.)
How can I set variable variables with both $x and $i within the variable name? Thank you!
You can do this by using curly braces within your variable name assignment to separate $x from the place:
$x = 4;
$i = 5;
${"name{$x}place{$i}"} = "test";
echo $name4place5;
Output:
test
However it would really make a lot more sense to just use an array:
$name[$x][$i] = "test2";
echo $name[$x][$i];
Demo on 3v4l.org

Dynamic variable in PHP [duplicate]

This question already has answers here:
PHP - concatenate or directly insert variables in string
(15 answers)
Closed 6 years ago.
I have a question about dynamic variables. (I have trouble searching because I have a hard time describing my problem)
In this example:
$x = 1;
$var = "A$x";
echo $var; //prints 'A1'
Now my question is, is there a way to combine "computation" without adding another variable?
What I want to do is:
$x = 1;
$var = "A($x+1)";
echo $var; //I want to output to be 'A2' but it gives 'A(1+1)'
I know that this works:
$var = "A".($x+1)
But this is not applicable to the program that I am doing. $var is initiated on the beginning of the program and will be used at the end waiting for any value of $x.
You need to concatenate your output.
$x = 1;
$var = "A". ($x + 1);
echo $var;
In your example the "+1" is inside the quotes and thus is a literal string.

What does "isset($x) ? $y : $z" mean? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What are the PHP operators "?" and ":" called and what do they do?
Reference - What does this symbol mean in PHP?
I know what isset means in PHP. But I have seen syntax like isset($x) ? $y : $z. What does it mean?
That is a Ternary Operator, also called the "conditional expression operator" (thanks Oli Charlesworth). Your code reads like:
if $x is set, use $y, if not use $z
In PHP, and many other languages, you can assign a value based on a condition in a 1 line statement.
$variable = expression ? "the expression was true" : "the expression was false".
This is equivalent to
if(expression){
$variable="the expression is true";
}else{
$variable="the expression is false";
}
You can also nest these
$x = (expression1) ?
(expression2) ? "expression 1 and expression 2 are true" : "expression 1 is true but expression 2 is not" :
(expression2) ? "expression 2 is true, but expression 1 is not" : "both expression 1 and expression 2 are false.";
That statement won't do anything as written.
On the other hand something like
$w = isset($x) ? $y : $z;
is more meaningful. If $x satisfies isset(), $w is assigned $y's value. Otherwise, $w is assigned $z's value.
It's shorthand for a single expression if/else block.
$v = isset($x) ? $y : $z;
// equivalent to
if (isset($x)) {
$v = $y;
} else {
$v = $z;
}
It means if $x variable is not set , then value of $y is assigned to $x , else value of $z is assigned to $x.

Categories