最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP Error : undefined variable - Stack Overflow

programmeradmin2浏览0评论

can anyone told me what is my error. i know it only notice. All is work well but only show this notice. How to fix this? :

this is the code :

if (isset($_GET['match']) && $_GET['match'] == 'one') {
    $checked_match_one = ' checked="checked"';
    $page_string .= SEP.'match=one';
} else {
    $_GET['match'] = 'all';
    $checked_match_all = ' checked="checked"';
    $page_string .= SEP.'match=all';
}

the error it tell me this :

Notice: Undefined variable: page_string in C:\xampp\htdocs\msigilearnv2\admin\enrollment_attendance.php on line 110
Done writing the list into excel. Download

can anyone told me what is my error. i know it only notice. All is work well but only show this notice. How to fix this? :

this is the code :

if (isset($_GET['match']) && $_GET['match'] == 'one') {
    $checked_match_one = ' checked="checked"';
    $page_string .= SEP.'match=one';
} else {
    $_GET['match'] = 'all';
    $checked_match_all = ' checked="checked"';
    $page_string .= SEP.'match=all';
}

the error it tell me this :

Notice: Undefined variable: page_string in C:\xampp\htdocs\msigilearnv2\admin\enrollment_attendance.php on line 110
Done writing the list into excel. Download
Share Improve this question edited Apr 24, 2014 at 9:19 Mark Baker 213k34 gold badges353 silver badges390 bronze badges asked Apr 2, 2014 at 8:40 airiairi 5856 silver badges22 bronze badges 1
  • Are you actually using the PHPExcel library? It isn't obvious from this code, which looks more like you're using a homebrew csv writer – Mark Baker Commented Apr 2, 2014 at 10:50
Add a ment  | 

5 Answers 5

Reset to default 8

Simple.Initialize $page_string before using it(before the if statement we can say) like

$page_string = '';
if (isset($_GET['match']) && $_GET['match'] == 'one') {
    $checked_match_one = ' checked="checked"';
    $page_string .= SEP.'match=one';
}

Since you are using the concatenation operator for the variable(for the first time),You should take care of initializing that variable to null, then only you can start any operations (including short hand operators like .=,+=,-=,etc)..

You need to initialize the $page_string variable before the if statement.

Like this..

$page_string = ''; //<--------- Here
if (isset($_GET['match']) && $_GET['match'] == 'one') {
    $checked_match_one = ' checked="checked"';
    $page_string .= SEP.'match=one';

Well, it says so right in the error message: you didn't define $page_string before you tried to read it.

The $page_string .= 'something' operation is shorthand for

$page_string = $page_string . 'something'

In other words, you're trying to read the value of $page_string (which is not defined yet), append 'something to that value, and put the result into $page_string. So, when you try to read the value of an undefined variable PHP does its automagic thing, assumes that the value is supposed to be null, but at least it sends you a notice - because it's suspicious behavior.

Why is it sending the notice? Well, take this piece of code:

$mesage = 'DO NOT ';

if ($something_foo_bar) {
    $message .= 'LAUNCH the nuclear missiles!';
}

That code probably won't do what the developer intended. Oops.

Just add $page_string = ""; in front.

发布评论

评论列表(0)

  1. 暂无评论