Skip to content
Advertisement

What is the proper way to declare variables in php?

I was using variables in my php file without declaring them. It was working perfect in old version of localhost (i.e vertrigoServ 2.22).

But when I moved to latest version of localhost (i.e xampp 3.2.1), I encountered variables declaration warnings and errors something like this:

Notice: Undefined variable: att_troops_qty in D:Installed ProgramshtdocsdashboardWarLordPHP_CodeMyAjax.php on line 1247

So I declared all the variables at the top of php file like this:

$page = "";
$att_troops_qty = "";
$def_troops_qty = "";
$ca_level = "";
$b_level = "";
$pre_buildings = "";
$created_pre_b = "";
$building_id = "";
$building_loc = "";
$ca_1_b_loc = "";
$ca_1_b_level = "";
$ca_2_b_loc = "";
$ca_2_b_level = "";

It solved the problem But I have confusion that this is not the proper way to declare variables.

Is there some more better way for variables declaration?

Advertisement

Answer

How you are declaring is perfectly alright and proper way.

$test = "";

or

$test = null;

these both are proper ways for declaring empty variables. for more info please visit http://php.net/manual/en/language.types.null.php

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement