PHP 如何知道(或者确实知道)它使用什么类型的变量?

发布于 2024-09-30 01:17:12 字数 648 浏览 1 评论 0原文

我没有用很多语言做过太多编程,但我知道在 C(++) 中,你必须声明一个变量类型(intchar 等)。

当然,在 PHP 中您不必这样做。您可以从 $str = "something"; 开始,然后再 $str = array("something" => "smells"); 她很高兴。

PHP是如何编译的?它如何知道变量类型是什么?它甚至关心吗?

这个问题与我正在做的任何事情无关。我只是好奇。

编辑。

我需要稍微澄清一下这个问题。

在 C 中,如果我说:

int y;

它为 y 保留 x 字节数。如果y溢出,那就是坏消息了。

PHP 不具有这种性质(至少我不认为具有)。

$i = "a number";
$i = 8;
$i = 1000000000000000000;

对于语言来说都是一样的。它如何知道要预订多少?或者我正在将苹果与橙子进行比较?如果我的理解是错误的,你有什么好的主题可以让我阅读以更好地理解吗?

I haven't done much programing in many languages, but I know in C(++), you have to declare a variable type (int,char,etc).

In PHP you, of course, don't have to do that. You can start with $str = "something"; then later $str = array("something" => "smells"); and she is happy.

How does PHP compile? How does it know what the variable type is going to be? Does it even care?

This question has no relevance to anything I am doing. I am just curious.

EDIT.

I need to clarify this question a little bit.

In C, if I say:

int y;

It reserves x amount of bytes for y. If y overflows, bad news.

PHP doesn't have this nature (at least I don't think it does).

$i = "a number";
$i = 8;
$i = 1000000000000000000;

It's all the same to the language. How does it know how much to reserve? Or am I comparing Apples to Oranges? If I am going about this wrong, do you have any good topics that I can read to better understand?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝礼 2024-10-07 01:17:13

由于您有 C 经验,请考虑将 PHP 变量如下所示:

typedef struct {
 int varType;
 void* data;
} variable_t;

当您创建一个新变量时,它将创建一个 variable_t,并为其指定一个类型标记(假设 1 表示 int,2 表示 int)字符串),并将其存储在列表中的某个位置(按范围,按名称引用)。实际内容将存储在*data中。当再次访问该变量时,可以从 int varType 确定类型,并对 void* data 采取适当的操作,例如将其用作 int 或字符串。

想象一下 PHP 代码片段:

 $data = 12;
 $data2 = $data + 1;
 $data = "Hello";

生成如下指令(伪 Cish):

Line 1:
variable_t* var = new_variable(TYPE_INT, data);
store_variable("data", var);

Line 2:
variable_t* var = get_variable("data2");
if (var->varType == TYPE_INT)
   int value = 1 + *(int*)var->data);
else
   error("Can't add non int");
var = new_variable(TYPE_INT, value);
store_variable("data2", var);

Line 3:
variable_t* var = get_variable("data");
if (var)
  destroy_variable(var);
// Do like line 1, but for TYPE_STRING

这种类型的增强数据以字节码、编译或直接解释语言工作。

有关不同虚拟机实现(本地分配、堆分配、注册虚拟机等)的更多详细信息。如果你真的想了解虚拟机是如何工作的,最好的参考是Lua。非常干净的语言,非常干净的字节码,以及非常干净的虚拟机。 PHP 可能是您应该查看的最后一个实现。

Since you have C experience, consider a PHP variable to be like the following:

typedef struct {
 int varType;
 void* data;
} variable_t;

when you create a new variable, it will create a variable_t, give it a type tag (lets say 1 for int, 2 for string), and store it in a list somewhere (by scope, reference by name). The actual contents will be stored in *data. When the variable is again accessed, the type can be determined from int varType, and the appropiate action taken on void* data, such as using it as an int or string.

Imagine that the PHP snippet:

 $data = 12;
 $data2 = $data + 1;
 $data = "Hello";

produces instructions like the following (pseudo-Cish):

Line 1:
variable_t* var = new_variable(TYPE_INT, data);
store_variable("data", var);

Line 2:
variable_t* var = get_variable("data2");
if (var->varType == TYPE_INT)
   int value = 1 + *(int*)var->data);
else
   error("Can't add non int");
var = new_variable(TYPE_INT, value);
store_variable("data2", var);

Line 3:
variable_t* var = get_variable("data");
if (var)
  destroy_variable(var);
// Do like line 1, but for TYPE_STRING

This type of augmented data works in bytecoded, compiled, or direct interpreted languages.

There are more details in regards to different virtual machine implementations (local allocation, heap allocation, register VMs, etc). If you actually want to understand how virtual machines work, the best reference is Lua. Very clean language, very clean bytecode, and very clean virtual machine. PHP is probably the last implementation you should look at.

九命猫 2024-10-07 01:17:13

PHP 并不真正编译——它被解释(成操作码)。

几乎如果您尝试对某种无法完成的数据类型执行某些操作,则会出现错误。没有类型检查。

PHP doesn't really compile -- it is interpretted (into op-codes).

Pretty much if you try to do something on a certain data type that can't be done, it'll give you an error. There is no type checking.

软的没边 2024-10-07 01:17:13

它无法编译。它是一种解释语言,就像Javascript一样。

It doesn't compile. It is an interpreted language, like Javascript.

风吹雪碎 2024-10-07 01:17:13

我意识到这是一个老问题,但这里有一些关于 PHP 如何处理 OP 提出的问题的更具体信息。

这是 PHP 参考中您想要开始的页面:

Introduction to变量

我知道链接不是首选,但该链接应该是稳定的,而且我不想批量复制 PHP 参考文档。以下是亮点。

OP:PHP 如何知道(或确实)使用什么类型的变量?

PHP 是用 C 编写的,并使用 C struct typedef(调用 zval)以及 C 联合 typedef(调用 zval_value 来表示所有变量)。

typedef struct _zval_struct {
    zvalue_value value;        /* variable value */
    zend_uint refcount__gc;    /* reference counter */
    zend_uchar type;           /* value type */
    zend_uchar is_ref__gc;     /* reference flag */
} zval;

引擎试图通过提供一组统一且直观的宏来访问结构的各个字段,从而掩盖可以是任何类型的变量概念的复杂性。”

“PHP 是一种动态、松散类型的语言,它使用写时复制和引用计数。”引用计数和写时复制 (COW) 是 PHP 使用的两个强大的概念,我不会在这里详细讨论,但值得一读。

弱类型隐含了引擎在执行时倾向于将变量转换或强制转换为所需类型的倾向。引用计数是引擎可以推断变量何时不再具有任何引用的方法。用户代码,因此能够释放与变量关联的结构。”

zval_value 是一个联合体,可以表示变量可能持有的所有类型。” “

... 变量可以是一种类型,变量数据由变量中的相应字段表示zval_value 联合。zval 本身保存类型、引用计数和一个标志来指示变量是否是引用。

PHP如何编译?

“编译”是一个宽泛的词,可以有不同的含义,PHP 并不进行传统意义上的编译。它确实进行了某种预编译,将源代码转换为操作码,这些操作码是处理器可以执行的指令。这些操作码会被缓存,从而防止 PHP 必须解析频繁调用的脚本。

它如何知道变量类型是什么?它甚至关心吗?

正如上面已经引用的,这是 PHP 引擎的“偏好在执行时将变量转换或强制为所需的类型”。基本上,PHP 在创建变量时总是存储它确定的变量类型,但是当引用变量时,PHP 根据使用变量的上下文再次确定类型是什么。

PHP 是弱类型的,因此引擎提供了用于将变量从一种类型转换为另一种类型的 API 函数。

引擎有一组宏,用于处理 zval 来转换变量的类型,因此你通常不必处理这个问题。

如果您想查看 zval 在实际中的样子,可以使用以下命令转储它们:

debug_zval_dump($variableName);

I realize this is an old question but here is some more specific information on how PHP handles the questions asked by the OP.

This is the page from the PHP reference that you'd want to start with:

Introduction to Variables

I know linking isn't preferred but that link should be stable and I don't want to wholesale copy PHP reference documentation. Here are the highlights.

OP: How does PHP know what type of variables it uses (or does it)?

PHP is written in C and uses a C struct typedef which it calls a zval along with a C union typedef which is calls a zval_value to represent all variables.

typedef struct _zval_struct {
    zvalue_value value;        /* variable value */
    zend_uint refcount__gc;    /* reference counter */
    zend_uchar type;           /* value type */
    zend_uchar is_ref__gc;     /* reference flag */
} zval;

"The engine attempts to cover up the complexity of the concept of a variable that can be any type by providing a uniform and intuitive set of macros for accessing the structures various fields."

"PHP is a dynamic, loosely typed language, that uses copy-on-write and reference counting." Reference Counting and Copy-on-write (COW) are two powerful concepts PHP uses which I won't go into here but are worth reading about.

"Weak typing is implicit of the engine's preference to convert, or coerce variables into the required type at execution time. Reference counting is the means by which the engine can deduce when a variable no longer has any references in the users code, and so is able to free the structures associated with the variable."

"The zval_value is a union which can represent all types a variable may hold."

" ... a variable can be of one type, the variable data is represented by the appropriate field in the zval_value union. The zval itself holds the type, reference count and a flag to indicate if a variable is a reference."

How does PHP compile?

"Compile" is a broad word that can have different meanings and PHP doesn't compile in the traditional sense. It does do a sort of pre-compilation which converts the source code into opcodes which are instructions that can be executed by the processor. These opcodes are cached which prevents PHP from have to parse frequently called scripts.

How does it know what the variable type is going to be? Does it even care?

As already quoted above it is the PHP engine's "preference to convert, or coerce variables into the required type at execution time." Baiscally PHP does always store what it determines a variable's type to be when it's created but when a variable is referenced PHP makes another determination of what the type is based on the context in which it is being used.

"PHP is weakly typed, as such the engine provides API functions for converting variables from one type to another."

The engine has a set of macros it uses for working with the zvals to convert a variable's type so that you usually don't have to deal with that.

If you want to see what zvals look like in action they can be dumped with:

debug_zval_dump($variableName);
很糊涂小朋友 2024-10-07 01:17:13

“PHP 是如何编译的?它怎么知道变量类型是什么?它关心吗?

这个问题与我正在做的任何事情都没有关系。我只是好奇。”

PHP 是一种解释性语言,它不能编译。

PHP 不知道变量的类型,因为变量的类型是由上次分配给该变量的值的类型决定的。

您可以这样做:

$foo = 5;
var_dump($foo);
$foo = "5";
var_dump($foo);
$foo = array();
$foo[] = 0;
$foo[] = 1;
$foo[] = 2;
var_dump($foo);

如您所见,每当将值分配给 foo 时,类型都可能会更改。

PHP 并不关心变量的类型,因为它是一种编程语言,它没有感情。

编辑:

“或者我正在将苹果与橙子进行比较?”

是的,你是。 在这里您可以了解有关该语言的更多信息。

EDIT2:

PHP 是一种带有解释器但没有编译器的脚本语言。这意味着在 PHP 中您只能收到运行时错误。如果我们考虑类型,那么它是一种自由语言,因为您有权对变量执行任何操作,并且它们的类型将根据它们的使用情况进行修改。

这些链接可能对您有用:

http://www.gidforums.com/t-11866。 html

http:// /www.webmaster-talk.com/coding-forum/186350-fundamental-differences- Between-php-cc.html

PHP 和 C 之间的变量作用域差异:块作用域不完全相同?

注意,PHP在服务器上执行,如果你想创建客户端事件,我的建议是Javascript。

"How does PHP compile? How does it know what the variable type is going to be? Does it even care?

This question has no relevance to anything I am doing. I am just curious."

PHP is an interpreted language and it doesn't compile.

PHP doesn't know what type the variable is going to be, because the type of the variable is determined by the type of the value which was assigned last time to that variable.

You can do this:

$foo = 5;
var_dump($foo);
$foo = "5";
var_dump($foo);
$foo = array();
$foo[] = 0;
$foo[] = 1;
$foo[] = 2;
var_dump($foo);

As you can see, whenever a value is assigned to foo, the type might be changed.

PHP doesn't care about the type of your variable, because it's a programming language and it doesn't have feelings.

EDIT:

"Or am I comparing Apples to Oranges?"

Yes, you are. Here you can learn more about the language.

EDIT2:

PHP is a scripting language with an interpreter and without a compiler. This means that in PHP you can only get runtime errors. It's a liberal language if we consider types, because you have the right to do anything with your variables and their types will be modified according to the usage of them.

These links might be useful for you:

http://www.gidforums.com/t-11866.html

http://www.webmaster-talk.com/coding-forum/186350-fundamental-differences-between-php-c-c.html

Variable scope difference between PHP and C: block scope is not exactly the same?

Note, that PHP is executed on the server, if you want to create client events, Javascript is my suggestion.

黑色毁心梦 2024-10-07 01:17:13

PHP 现在支持类型提示,您可以在函数定义中包含“字符串”或“数组”等内容,这些内容在解析脚本时会被捕获以指示存在类型不匹配。

PHP now supports type hinting, you can include things like 'string' or 'array' in function definitions that are caught as the scripts are parsed to indicate there is a type mismatch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文