Home > Article > Backend Development > What is the difference between php and c
The difference between php and c: 1. PHP is a general open source scripting language, while C language is a process-oriented, abstract general programming language; 2. PHP is mainly suitable for the field of Web development , and C language is widely used in underlying development; 3. The PHP engine is actually a garbage collection mechanism and so on.
Recommended: "PHP Video Tutorial"
Comparison of PHP and C Language
The simplest way to put it is to treat PHP as C, which can be embedded in HTML documents. PHP is much like the C language itself, except there are no typed variables, many networking feature libraries are built in, and everything connects directly to your favorite web server. The syntax of
statements and function definitions should be familiar, except that variables are always defined after $ and functions do not require separate prototypes.
Here we will introduce some similarities and differences between PHP and C:
Similarities
Syntax: Generally speaking, PHP The syntax is the same as C. The C language code is concise, compact, flexible and convenient, and the statement is terminated with a semicolon. Function calls have the same structure (my_function(expression1 expression2)), using block braces ({and}). PHP supports C and C-style comments (/ / / /), as well as Perl and Shell scripting style (#).
Operators: assignment operators (=, =, =, etc.), Boolean operators (&&, | |,!) comparison operators (<,>, <=, >= , ==, !=) and basic arithmetic operators ( , -, , /, %), these C operation operators are the same as PHP.
Control structure: The basic control structure (if, switch, while, for) is basically the same as the C language, including the use of break and continue keywords. One obvious difference is that switch in PHP can accept strings as identifiers.
Function names: When you read the documentation carefully, you will see many function names that are the same as C functions.
Difference
$ symbol: All variables are represented by a $ symbol. Variables do not need to be declared before calling, and there is no essential type difference between them.
Type: PHP has only two numeric types: integer (corresponding to C's long) and double precision type (corresponding to C's double), strings of any length. There are no separate character types.
Type conversion: Types are not checked at compile time, and type errors also usually do not occur at runtime. Instead, variables and values are type-converted automatically.
Array: Array is similar to the syntax of C array at the syntax level, but their implementation is completely different. They are actually associative arrays or hashes, and the indices can be numbers or strings. They do not need to be declared or allocated in advance.
Structure types: There are no structures in PHP, partly because they don't necessarily all go together as array and object types. The elements of a PHP array can be of inconsistent types.
No pointers: Although untyped variables play a similar role, there are no pointers in PHP. PHP does not support variable references. You can also emulate function pointers to a certain extent, where function names can be stored in variables and used variables instead of literal names.
No prototype: The function does not need to be defined before the implementation is announced, as long as the code in the current file where the function is defined can be found.
Memory management: The PHP engine is actually a garbage collection mechanism (using reference counting), which does not require any recycling when running in a small script. You should be free to allocate new structures - such as new strings and object instances. In PHP5, you can define object destructors. When PHP decides that your script is no longer related to the object, PHP destructors will be called. PHP calls them to destroy an object from memory and reclaim it in memory.
Compiling and linking: There is no separate step to compile PHP scripts.
Licensing: In general, PHP is more permissive than C (especially its type system), so it will make it easier for you to avoid new type errors. Unexpected results are more common than errors.
The above is the detailed content of What is the difference between php and c. For more information, please follow other related articles on the PHP Chinese website!