在php中声明变量的正确方法是什么? [英] What is the proper way to declare variables in php?

查看:261
本文介绍了在php中声明变量的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php文件中使用了变量而未声明它们.在旧版本的localhost(即vertrigoServ 2.22)中,它运行得非常完美.

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

但是当我使用最新版本的localhost(即xampp 3.2.1)时,遇到了变量声明警告和类似以下错误:

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:

注意:未定义的变量:D:\ Installed中的att_troops_qty 程序\ htdocs \ dashboard \ WarLord \ PHP_Code \ MyAjax.php在第1247行

Notice: Undefined variable: att_troops_qty in D:\Installed Programs\htdocs\dashboard\WarLord\PHP_Code\MyAjax.php on line 1247

所以我像这样在php文件的顶部声明了所有变量:

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?

推荐答案

您的声明方式完全正确且正确.

How you are declaring is perfectly alright and proper way.

$test = "";

or

$test = null;

这两种都是声明空变量的正确方法. 有关更多信息,请访问 http://php.net/manual/en/language. types.null.php

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

这篇关于在php中声明变量的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆