为什么变量不能以数字开头? [英] Why variable can not start with a Number?
问题描述
我正在PHP中工作,我必须按顺序定义变量以保存在Mysql中.在我的情况下,字段名和变量名必须相同.
I am working in PHP, I have to define variables in sequence to save in Mysql. Both field name and variable name must be same in my case.
我可以这样声明一个变量吗
Can I declare a variable like this
$1 OR $2 etc
如果不是,为什么不是?如果是,为什么是?
If not why not and if yes why yes?
我尝试过:
$id = 0;
$6 = $_REQUEST['6'];
$7 = $_REQUEST['7'];
$8 = $_REQUEST['8'];
$xary = array('6','7','8','9')
$oAppl->save_record("tablename", $id, "id");
这给了我错误.
我还可以在Mysql中创建和使用同名的字段吗?
Also can I create and use fields in Mysql with the same name?
推荐答案
这是PHP的设计方式.您不能使用数字开头变量名.
This is how PHP was designed. You can't start a variable name with a number.
您可以做的是使用下划线:
What you can do is use underscore:
$_6 = $_REQUEST['6'];
因为变量在PHP中以 $ 开头,所以变量可能以数字开头,但这会使大多数人感到困惑,因为没有其他语言允许变量以数字开头(或至少我什么都不知道.
since variables start with $ in PHP, is would be possible to have variables starting with numbers, but that would be a bit confusing to most people since there is no other language that allows variables starting with numbers (or at least I don't know any).
但是让我们想象一下允许以数字开头的变量.
But let's imagine variables starting with numbers are allowed.
您能想象一个同事对您说: 23等于74吗?这有点令人困惑.听到 n23等于74 更有意义.您知道 n23 是一个变量,而不必明确地说出来.
Can you imagine a coworker saying to you: 23 equals 74? That is a bit confusing. Hearing n23 equals 74 makes more sense. You know n23 is a variable without having to explicitly say that.
这篇关于为什么变量不能以数字开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!