I am having trouble with:
Notice: Undefined variable: detail in
/var/www/html/premysl/php/web_f7mysql.php on line 43
The mentioned line contains some condition with $detail == NULL, which (variable $detail) should be extracted via extract($_GET) written in different file.
Here's the thing - whole project is hosted in root of server with PHP 5.4.13 and works perfectly. Anyway, it doesn't run on a different server with PHP 5.3.3 (cli).
Unfortunately it is necessary to make it work on the second one. I guess there's some "compatibility" issue, maybe different settings.
Do you have any sugestions what to focus on?
Your line that reads like this:
$detail == NULL
Should be changed to combine isset and !empty like this:
isset($detail) && !empty($detail)
Also using extract($_GET) is a bit ridiculous. Instead just do this:
$detail = (isset($_GET['detail']) && !empty($_GET['detail'])) ? $_GET['detail'] : null;
Also as far as the actual error goes:
Notice: Undefined variable: detail in /var/www/html/premysl/php/web_f7mysql.php on line 43
Then just stick this line within whatever function is causing this issue in web_f7mysql.php:
global $detail;
Related
I have upgraded my server to use PHP version 7.3 from 5.6
I ran compatibility check from PHPStorm for all my files, and it only showed me only weak warning for few of the codes but since I am no longer using those script I ignored it, but it does not point out anything wrong on this code.
But after migration, I found that this piece of code is not working as required. I have this code to get state short code when I pass the 2 digit country code.
$jsonitemfile = file_get_contents('countries-states.json');
$conobjitems = json_decode($jsonitemfile);
$findBystatename = function($constatename) use ($conobjitems) {
foreach ($conobjitems->$_POST['Contact0Country']->states as $short => $outputstate) {
if ($outputstate->name == $constatename) return $short;
}
return false;
};
$p2 = 'New York';
echo $findBystatename($p2) ?: $p2;
When I run this code I get the following error
Notice: Array to string conversion in /home/domain.com/public_html/code/code_check.php on line 27
Notice: Undefined property: stdClass::$Array in /home/domain.com/public_html/code/code_check.php on line 27
Notice: Trying to get property 'states' of non-object in /home/domain.com/public_html/code/code_check.php on line 27
Warning: Invalid argument supplied for foreach() in /home/domain.com/public_html/code/code_check.php on line 27
The same code is working on PHP 5.6 without throwing any error.
Although I fixed the code by doing the below changes
$findBystatename = function($constatename) use ($conobjitems) {
$p1 = $_POST['Contact0Country'];
foreach ($conobjitems->$p1->states as $short => $outputstate) {
if ($outputstate->name == $constatename) return $short;
}
return false;
};
For my learning curve can anyone please advise why its not working? And even with the updated code, if I define $p1 above the function, it start to throw the same error.
Also is there a way the original code start working on PHP 7.3 without updating the code?
$conobjitems->$_POST['Contact0Country']->state is ambiguous. It could mean the state property of an object defined by $conobjitems->$_POST['Contact0Country'] (as you and PHP 5.6 assume), or it could mean the state property of an object defined by $_POST['Contact0Country'] and then whatever that resolves to as a property on $conobjitems, which seems to be what modern PHP considers it.
Obviously a compiler/run time environment cannot treat a line as meaning two things, so it has to pick one and move on, or error out and fail. Historically PHP has been lenient in what it copes with, but as the language has improved and got faster some of that leniency has been removed in favour of making the coder be specific instead of ambiguous (which also helps make the code readable)
If this is just a toy example - this won't work, but your piece of code can be written as:
$jsonitemfile = file_get_contents('countries-states.json');
$conobjitems = json_decode($jsonitemfile);
$country=$conobjitems->$_POST['Contact0Country'];
$states=$country->states;
$findBystatename = function($constatename) use ($states) {
foreach ($states as $short => $state) {
if ($state->name == $constatename) return $short;
}
return false;
};
$p2 = 'New York';
echo $findBystatename($p2) ?: $p2;
$country could be skipped but it makes the code more readable, likewise separating the state list into $states before injecting into the closure makes it clear to the reader and PHP what you want to do.
By the way, since you seem to choose the country by user-input (which could be manipulated, even if you're using a drop down), you could also get an error, warning or exception when you create $country if that country doesn't exist in the $conobjitems set. You could use property_exists first to make sure the country is found.
This line of code worked perfectly before I upgraded, now it doesn't:
123: $thisRow[] = displayR(${"sR{$i}S1"}, ${"nR{$i}S1P"});
I get this error:
Undefined variable: sR1S1 in script.php on line 123
A workaround is to manually assign them first
$s = ${"sR{$i}S1"};
$n = ${"nR{$i}S1P"};
$thisRow[] = displayR($s, $n);
Is there anyway to get it working in a single line as before? Does anyone know why it no longer works?
I should add that I've tried using var_dump(${"sR{$i}S1"}); on the previous line and it IS defined, and has the value that I expect it to.
I can't find any mention of this behaviour on google or SO, I wonder if it's a bug rather than intended.
I believe this is to be an opcache bug.
Disabling opcache on the file in question resumed the expected behaviour.
I submitted a (fairly useless) bug report at:
https://bugs.php.net/bug.php?id=69159
Which has apparently been fixed and closed:
http://git.php.net/?p=php-src.git;a=commitdiff;h=a29b64fc1029b4121e3e9ff20901c35ad600b4da
I was working on my site yesterday and all the content was appearing fine. Then today, I refreshed and something has broken.
I'm getting this error on the homepage:
Notice: Use of undefined constant Page - assumed 'Page' in includes\survey_inside.php on line 19
Notice: Use of undefined constant Listing - assumed 'Listing' in includes\survey_inside.php on line 20
When I look on line 19 the code is:
$strPage = $_REQUEST[Page];
if($_REQUEST[mode]=="Listing"){
Like I said, everything worked fine yesterday, and I didn't change these lines. I've been looking around for several hours and am stuck. Any thoughts on how to proceed with figuring out what happened?
Thanks!
When you want to access an array like $_REQUEST, you have to use some key (string value). In this case you are calling $_REQUEST[Page] and $_REQUEST[mode].
Since Page and mode are not constant values, you have to use it as a string, so:
$strPage = $_REQUEST['Page'];
if ($_REQUEST['mode'] == "Listing") { }
I am doing project in php.I used ajax code to upload the image.
It done nicely, everything is working but display notice and i want 2 remove this notice...
Notice is as follows:
Notice: Undefined variable: errorList in C:\xampp\htdocs\ajax\scripts\ajaxupload.php on line 18 Notice:
Undefined variable: errorList in
C:\xampp\htdocs\ajax\scripts\ajaxupload.php on line 132
i am not understanding what is the meaning of message...
How to remove this Notice?
To remove the notice you must either check is this variable $errorList defined and if it is, then check ajaxupload.php on line 18 and ajaxupload.php on line 132 does it see it there or is it out of the scope.
The other thing you could do is place at the beginning of PHP file ini_set( 'display_errors', 0 );, but this really bad idea since Notice is made to show what you are doing wrong.
Simply define the variables as null before using them, so that by default they will be null until given a value from elsewhere. Variables defined inside an if block or by user input sometimes don't get defined, right? So for them, a default is needed sso that they at least exist even if they aren't given a value.
$errorList = null;
I am using XAMPP 1.7.2 (PHP 5.3) to devlope on winxp localhost.
There is a function works quite well. Comes from CodeIgniter Module.
function get_cal_eventTitle($year, $month){
$this->db->where('eventYear =', $year);
$this->db->where('eventMonth =', $month);
$this->db->select('eventTitle');
$query = $this->db->get('tb_event_calendar');
$result = $query->result();
foreach ($result as $row)
{
$withEmptyTitle = $row->eventTitle;
//echo $withEmptyTitle.'<br>';
$noEmptyTitle = str_replace(" ","%20",$withEmptyTitle);
$withUrlTitle = '<a href='.base_url().'index.php/calendar/singleEvent/'.$year.'/'.$month.'/'.$noEmptyTitle.'/'.'>'.$withEmptyTitle.'</a>';
//echo $withUrlTitle.'<br>';
$row->eventTitle = $withUrlTitle;
}
return $result;
}
When I upload my code to remote server(PHP 5.2.9). It show error as this,
withEmptyTitle undefined variable
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: withUrlTitle
Filename: models/calendar_model.php
Line Number: 54 // $withEmptyTitle = $row->eventTitle;
But When I enable the comment for the line echo $withEmptyTitle.'<br>';. It works well on remote server.
Assuming withEmptyTitle echo to Apr running event here.
I don't know why? Could you give me some suggestion to fix this issue? Appreciated for your input.
What you're seeing is probably not an error, but a warning.
PHP may be throwing the warning because you're using a variable that hasn't been initialised. It sounds likely that your local development PHP installation has warning messages supressed, whereas your live server has them enabled. (In fact, best practice would be to have it the other way round!)
In this case, it's possible that $withEmptyTitle = $row->eventTitle; is not initialising the $withEmptyTitle variable, if the eventTitle property is returning as unset. It would then fall following line and throw the warning when you try to use the variable in the str_replace() call.
You could avoid this warning by:
Switching warning message off in the PHP.ini
Doing ini_set() to switch it off during the program.
using isset($withEmptyTitle) to check that the variable is set prior to actually using it.
Ensuring that $row does actually contain an eventTitle property (in the context of your program, if it's missing, it may imply bad data or incorrect database table setup? In any case, you could change the SQL query to use IFNULL() or at least ensure the field is explicitly queried.
[EDIT]
I see you've edited the question. In particular, I note the following:
Line Number: 54 // $withEmptyTitle = $row->eventTitle;
I note the // .... does that imply that the line is commented out?? Have you somehow got a copy on the server which has that line commented? That would certainly explain the fact that you're getting warnings!