Home > Backend Development > PHP Tutorial > Why don't variables in php need to be specifically declared?

Why don't variables in php need to be specifically declared?

WBOY
Release: 2016-10-11 14:23:47
Original
1470 people have browsed it

As the title states, there is no need to declare variables in PHP. Does the system know what a variable is and allocate space to the variable based on $? Does it not need to declare variables because php is a weakly typed language?

Reply content:

The question is like the title, there is no need to declare variables in PHP, so does the system know what variables are and allocate space to variables based on $? Does it not require declaring variables because php is a weakly typed language?

The underlying C of PHP uses a union to store variables. There are various variable types in this union. PHP uses long type to store integers and hashtable to store arrays, because union can store various variables. , and the interpreter will automatically determine the variable type, so PHP does not need to declare the variable type, weak type

Yes, because php is a weakly typed language, there is no need to declare the variable type before it can be used. .

$ is just syntax sugar, indicating that what follows is a variable. .

If explained in terms of memory space allocation

  • When allocating memory space for weakly typed variables, this memory space can store variables of any type. When using it, you need to search all memory areas

  • The strong type is allocated in the specified memory according to the variable type, and the type cannot be converted directly. When using it, go directly to the area of ​​that type to find the value.

So generally changing the variable type in a weakly typed language can be modified directly. . However, strongly typed languages ​​cannot be modified directly, so the concept of pointers in strongly typed languages ​​is particularly important. We do not directly use the data in the memory. We create a reference variable (pointer). If you want to modify it, directly modify the memory address pointed by the pointer. Can.

.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template