Home > Backend Development > PHP Problem > Is php a static or dynamic language?

Is php a static or dynamic language?

青灯夜游
Release: 2023-03-15 20:58:01
Original
3820 people have browsed it

php is a dynamic language. PHP is a dynamic, weakly typed scripting language. It determines the data type at runtime and does not need to declare it in advance before using the variable. The variable will be automatically created when it is assigned a value for the first time. The data type in PHP can be ignored. When assigning a value to a variable, you do not need to consider its type. The variable only has a certain type after it is assigned a certain value.

Is php a static or dynamic language?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php is a dynamic language.

php is a dynamically typed and weakly typed scripting language; there is no need to declare variables in advance before using them. The variables will be automatically created when they are assigned a value for the first time, and when assigning a value to a variable, it does not need to be declared in advance. Its type needs to be considered. A variable only has a certain type after it is assigned a certain value.

<?php
$a = 1;
$b = "2";
$c = [1,3,4];
var_dump($a);
var_dump($b);
var_dump($c);
?>
Copy after login

Is php a static or dynamic language?

Therefore, automatic type conversion will occur during variable calculation.

In PHP, automatic type conversion usually occurs when variables of different types are mixed for operation. If the variable types involved in the operation are of different types, they need to be converted to the same type first, and then the operation is performed.

Usually only the four scalar types (integer, float, string, boolean) and NULL will be automatically converted during operations. Note that automatic type conversion does not change the type of the variables themselves, only the way these variables are evaluated.

Although automatic type conversion is automatically completed by the system, during mixed operations, automatic type conversion also needs to follow the direction of increasing data length to ensure that the accuracy is not reduced. The rules are shown below.

Is php a static or dynamic language?

  • When a Boolean value is involved in the operation, TRUE will be converted into an integer type 1, and FALSE will be converted into an integer type 0, and then participate in the operation.

  • When a NULL value is involved in the operation, the NULL value will be converted into an integer type 0 before the operation is performed.

  • When there are values ​​of integer type and float type involved in the operation, the integer type value will be converted to float type first and then the operation will be performed.

  • When string and numeric (integer, float) values ​​are involved in the operation, the string type will be converted to a number first and then participate in the operation. The converted number is a numeric string starting from the string. If the numeric string starting from the string does not have a decimal point, it will be converted to an integer type value. If there is a decimal point, it is converted to a float type value. For example: the string "123ab" is converted to the integer 123, the string "123.45ab" is converted to the floating point number 123.45, and the string "abc" is converted to the integer 0.

【Example】The following uses a simple example to demonstrate automatic type conversion in PHP.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;100abc&#39;;
$str += 5;
echo &#39;此时 $str 的类型为:&#39;.gettype($str).&#39;, 值为:&#39;.$str.&#39;<br>&#39;;
$str += 3.14;
echo &#39;此时 $str 的类型为:&#39;.gettype($str).&#39;, 值为:&#39;.$str.&#39;<br>&#39;;
$str = null + &#39;C语言中文网&#39;;
echo &#39;此时 $str 的类型为:&#39;.gettype($str).&#39;, 值为:&#39;.$str.&#39;<br>&#39;;
?>
Copy after login

Is php a static or dynamic language?

Extended knowledge: Introduction and differences between dynamic languages ​​and static languages

Dynamic languages ​​(weakly typed languages) are runtime In a language that only determines the data type, there is no need to declare the type of a variable before using it. Usually the value of the variable is the type of the value to which it is assigned. Such as Php, Asp, JavaScript, Python, Perl, etc.

$a = 1;
$b = "2";
$c = [1,3,4];
Copy after login

Static language (strongly typed language) is a language in which the data type of the variable can be determined at compile time. Most static languages ​​require that the data type must be determined before using the variable. Such as Java, C, C, C#, etc.

String s="hello";    //String 类型的变量
boolean b=true;    //boolean 类型的变量
int i=0;    //int 类型的变量
Copy after login

Weakly typed language is a language in which data types can be ignored. It is the opposite of a strongly typed language, where a variable can be assigned values ​​of different data types. A variable's type is determined by its context, which is more efficient.

Strongly typed language is a language that must force the data type to be determined. Once a variable is assigned a certain data type, if it is not forced to convert, it will always be this data type. The type of a variable is determined when it is declared, which is safer.

Difference:

  • Due to the mandatory declaration of data types, the static language allows the development tool (IDE) to have a strong ability to judge the code. When implementing complex business logic and When developing large-scale commercial systems and applications with long lifecycles, developers can rely on powerful IDEs to develop more efficiently and safely.

  • Dynamic language thinking is not constrained and can be used at will, focusing more on the product itself; focus on thinking about business logic implementation, and the thinking process is the implementation process.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Is php a static or dynamic language?. For more information, please follow other related articles on the PHP Chinese website!

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