Jump to content

variable declaration - undefined

- - - - -

  • Please log in to reply
5 replies to this topic

#1
xirochanh

xirochanh

    Newbie

  • Members
  • Pip
  • 2 posts
Hi bros,

I'm totally newbie, and this is maybe most silly question ever, but please help.
I got some PHP lines like this:

$var1 = 1;

if($var==1){

   echo 'yes';

}


I got message: "Notice: Undefined variable".
Then if I add var before $var1, error thrown is: "Parse error: syntax error, unexpected T_VAR".
If I hide notice message by statement
error_reporting (E_ALL ^ E_NOTICE);
, then no message shown, but the variable $var1 is not declared and assigned expected value - nothing is displayed.

So, what is the correct and easy way to declare a variable?
I'm running WampServer 2.1a

Thanks a lot.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
$var1 is not the same as $var
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
xirochanh

xirochanh

    Newbie

  • Members
  • Pip
  • 2 posts
with $var1, I mean any variable name, so, when I say I add var before $var1, i mean:


var $var1 = 1;

if($var==1){

   echo 'yes';

}



#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Suggestion: what is the actual code and error?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Here's an answer about your keyword var: What does PHP keyword 'var' do? - Stack Overflow

The var keyword is no longer needed in PHP. PHP is a loosely typed language, meaning you don't have to declare a variable's type before using it. That being said, the keyword var is different from the variable $var. Also $var1 is not the same as $var.

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
$var1 = 1;

if($var==1){

   echo 'yes';

}

As the warning suggests, $var is undefined. If you want to get rid of the warning, define $var.

$var1 = 1;

$var = null;

if($var==1){

   echo 'yes';

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users