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
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 Commented Apr 2, 2014 at 10:50

5 Answers 5

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';
}
Sign up to request clarification or add additional context in comments.

Comments

5

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)..

Comments

3

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';

Comments

3

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.

Comments

2

Just add $page_string = ""; in front.

3 Comments

use single quotes when the string does not contain variables to speed up PHP parsing.
@Raptor the quotes dont affect the speed of php. Its just a myth.
The same with things like echo $var1, $var2; is supposedly faster than concatenating: echo $var1 . $var2; but you'd save a millisecond on a million iterations, it's just completely negligible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.