网络学院 w3pop社区 网络资源 IT新闻

w3pop.com :: 网络学院 :: PHP :: PHP 变量

会员登陆

帐号

密码

回答

记住密码

忘记密码? 注册

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 变量


作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-28 修改:2007-06-18 浏览:8167 :: ::

Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script.
“变量”是用来存储“值”的,如:数字(number)、字符串(string)或者函数结果(function results);它可以在脚本种重复使用。


Variables in PHP
PHP变量

All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays.
所有的PHP变量的前面都包含一个“$”符号。变量可以包含字符串、数字、数组。

Below, the PHP script assigns the string "Hello World" to a variable called $txt:
下面的案例中,我们先给“txt”附一个字符串“Hello World”,然后再显示这个“txt”变量:

<html>
<body>
<?php
$txt="Hello World";
echo $txt;
?>
</body>
</html>

To concatenate two or more variables together, use the dot (.) operator:
如果你想把两个变量合二为一,你需要使用(.)操作符,具体如下:

<html>
<body>
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2 ;
?>
</body>
</html>

The output of the script above will be: "Hello World 1234".
上述例子输出的结果是:"Hello World 1234"


Variable Naming Rules
变量命名规则

  • A variable name must start with a letter or an underscore "_"
    一个变量的开头必须是一个字母或下划线“_”。
  • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
    一个变量的名称只能包含字母、数字、下划线“_”(即:a-Z, 0-9 和 _)
  • A variable name should not contain spaces. If a variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
    变量名称不能含有空格(space)。如果一个变量中需要包含多个单词,那单词之间必须以下划线“_”连接,如:($my_string);或者使用大写字母,如:($myString)

评论 (1) 1 All