0

I'm using a portable WAMP package "USB web server". It is giving error if i try to echo some variable directly, for example

<?php
echo $abc;  
?>

The error i get is: "Notice: Undefined variable: abc "

This will work fine:

<?php
$abc = "foo";
echo $abc;  
?>

Is it possible to fix this error? I am bound to use portable WAMP package as I do no thave administrator privileges, so i can not install any other package.

4
  • 1
    It is not clear what you want. Obviously you cannot echo a variable before it was defined, as it does not exist. This is not an error, this is normal behaviour. So why do you do echo $abc? Commented May 25, 2011 at 12:43
  • @Felix Kling, Actually I wrote code on another PC where it was not giving any notice if i print without defining the variables, but now i copied the files to the other computer and i'm getting lots of these messages. is there anyway to turn the notices off? Commented May 25, 2011 at 12:47
  • As long as you haven't assigned anything to a variable, it contains NULL, which will become the "" empty string if printed out. The notice is just a by-product for debugging purposes, because typically that's not what you want. So, why do you want it? Commented May 25, 2011 at 12:47
  • check out Nanne's answer. But again, what you wrote there is just bad code. Prefer fixing it rather than ignoring errors. Either define the variable or test for its existence with empty() or isset() before echoing it. Commented May 25, 2011 at 12:48

9 Answers 9

2

Well, apart from why I don't know why you would want to echo something that is not defined, you're not getting an error, you're getting a notice.

If you want to do this, you've got to ignore notices in your production environment, like you can read here works like this:

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
Sign up to request clarification or add additional context in comments.

5 Comments

It's good practice though to prefer fixing your code than ignoring the problems. Only do this if you have to, in production. But before doing this, do your best to fix your code. The above example is just bad code.
I agree. I also do not see why you would echo something that's not set.
I was trying to do that if there is any value in session, print it, otherwise dont print anything.
Session values can be found in the session superglobal: if (isset( $_SESSION['abc'])) { echo $_SESSION['abc']; }
@sabba, that's OK. Just use empty() instead of relying on undefined variables. if(!empty($_SESSION['var'])) { echo $_SESSION['var']; }.
1

What should it print if it is not defined?

You can check if it is defined before you try to print it

if(isset($abc))
{
    echo $abc;
}

You can also suppress the error by appending an @ sign in front of the line

@echo $abc;

But that is not recommended and can be slower.

Comments

1

to remove notice and warning write

error_reporting(E_ALL ^ E_NOTICE); at the top of the page

Comments

0

The variable is indeed not defined. What exactly would you like to happen?

If you don't want to see the error, then define it.

<?php
$abc = '';
echo $abc;
?>

Comments

0

Well, the answer is to fix the code so you aren't accessing variables that have not been defined. If you need to prevent it from printing errors which is not the correct solution (*), then you can switch off notices in your error reporting:

error_reporting(E_ALL ^ E_NOTICE);

The correct solution is to fix the code.

Comments

0

Check if it exists before echoing it

if( isset($abc) ) { echo $abc }

Comments

0

you could of course also just check if the variable exists.

<?php if (isset($myvar)) echo $myvar; ?>

cu Roman

Comments

0

You could also suppress the warning on the echo command. Disabling warnings in general is dangerous.

<?php
    @echo $abc;
?>

Comments

0

One of the earliest principle you learn in programming is to declare your variables. Therefore, to fix this error, declare your variables.

However, it may happen in multiple cases that you don't know if the variable has been defined or not, but still want to avoid the error.

In those cases, check for its existence first:

// print $abc if set, or print an empty string/nothing
echo isset($abc) ? $abc : '';
isset($abc) && print $abc;
if (isset($abc)) echo $abc;

The three lines above will give the exact same result one another.

Comments

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.