PHP Undefined variable [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed last month.
When I test an easy PHP program , I found that there is a strange problem.
The code is in fllow:
<?php
$a = "abc";
function Test()
{
global $a;
$b .= $a."e";
return $b;
}
echo Test();
As I know,it is legal.But Visual Studio Code tells me that the $b is Undefined variable.
At the same time, the program can run successfully with the output:abce
Anybody can help me? I want to know the reason.

It's because you used .= instead of just =. That means the RHS gets appended to the LHS, which means it's expected to have been set before. If you didn't expect $b to exist before, then you should have just used =.

Related

How to fix "Notice: Undefined variable" error [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
I am looking at my 'WP_DEBUG' errors and the following two errors/notices pop up in a number of PHP files:
Notice: Undefined variable: defaultUI in...
Notice: Undefined variable: compileShortcodeUI in...
I have checked all of the the PHP files and lines specifically and every single one of these refers to this same bit of code:
$compileShortcodeUI .= "<div class='whatInsert whatInsert_".$shortcodeName."'>".$defaultUI."</div>";
What do I need to change to remove these errors?
You have to define the variable initially.
$compileShortcodeUI = ''; // null or any default value
$defaultUI = ''; // null or any default value
$compileShortcodeUI .= "<div class='whatInsert whatInsert_".$shortcodeName."'>".$defaultUI."</div>";

How to get rid of undefined index in this situation [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP check value against multiple values with OR-operator
(3 answers)
Closed 4 years ago.
Hi,
I have this code:
if($_GET['s']=="page1" || $_GET['s']=="page2" || $_GET['s']=="page3") {
dosomething();
}
and I get this error: : Undefined index: s in
Which I can dismiss only by adding this line:
$_GET['s']="";
but then this wont execute the code correctly since $_GET['s'] is not supposed to have any initial value. How do I fix this other than disabling the notices and errors?
Thank you.
You can check your $_GET['s']
if(isset($_GET['s'])) {
// your code goes here...
}
isset() is used to check if the index exists.

PHP Notice:what is the pro [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I've been getting this error. Any fix??
PHP Notice: Undefined offset: 1 in line 402
if ($argv[1] == '--cover')
{
$Modules = new Modules;
$Modules->cover();
die();
}
The line is if ($argv[1] == '--cover')
An offset is undefined if it doesn't exist in the array.
Try this:
if(isset($argv[1]) && $argv[1] == '--cover'){
$Modules = new Modules;
$Modules->cover();
die();
This error means that the $argv[1] is undefined or null. Make sure this variable is properly assign or have a value before making a condition.
using print_r($argv); before making a condition will help you if the variable have a value or not.

Do I have to check every variable is set before echoing it? [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am currently in a process of writing my PHP script, and I am doing it with
E_ALL set in php and I can not allow ANY errors, even
PHP Notice: Undefined variable
And I have many places where i ECHO $myvariable without checking if it's empty, which is causing error mentioned above.
Is it just me or it's extremely stupid to do this for every variable that can be undefined:
if(!empty($myvariable)) {
echo $myvariable;
}
Is this only way to avoid these errors?
EDIT:
Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here.
Three possible solutions:
1st: initialize your var at the very beginning of your code (or method, or class, whatever you're doing)...
$var = "";
// Do stuff with $var
echo $var;
2nd: use a ternary operator
echo (isset($var)) ? $var : "";
3rd: use #
#echo($var);
It is a notice which gives you an insight that your code could produce unexpected results since you did not assign a value (constant or calculated).
You can avoid this error by checking if it's set using the isset function or set a value for that variable at the top of your script.

Undefined index with $_POST variable [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
with this code:
$tstUsername = getValue($_POST['tstUsername']);
$tstPassword = getValue($_POST['tstPassword']);
if ($tstUsername !== false && $tstPassword !== false) {
echo "New string added to database!";
haydayshops_mysql_query($conn,"INSERT INTO table_accounts (username, password) VALUES >('$tstUsername','".sha1($tstPassword)."')");
}
The code works fine, but when i put the error mode on, i see this nasty errors:
Notice: Undefined index: tstUsername in ...
Undefined index: checkPassword in ...
You get these errors because you're trying to access to key of an array which doesn't exists. Here, you access to $_POST['tstUsername'] but it doesn't seems to exists in the $_POST data. Same case with a key named checkPassword somewhere else in your code.
To avoid that, use isset() function to check if they are declared before trying to access to the value.

Categories