Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Why variable can not start with a Number?

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?

I tried:

$id = 0;
$6  = $_REQUEST['6'];
$7  = $_REQUEST['7'];
$8  = $_REQUEST['8'];

$xary = array('6','7','8','9')

$oAppl->save_record("tablename", $id, "id");

which give me error.

Also can I create and use fields in Mysql with the same name?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'];

EDIT: 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...