PHP for C Experts

PHP for C Experts is easiest to understand as interpreted C that can be put into HTML documents. The language itself is similar to C, but variables are not typed, there are a lot of Web-specific libraries built in, and everything is connected directly to your favorite Web server.

The syntax of statements and function definitions should be familiar, except that variables are always preceded by $ and functions don’t need separate prototypes.

Here, we’ll talk about how PHP and C are the same and different.

 

Similarities

Syntax: PHP syntax is, for the most part, the same as C syntax. Code is not affected by blanks, statements end with semicolons, function calls look the same (my function(expression1, expression2)), and curly braces ( and ) group statements into blocks. PHP can handle comments in the C and C++ styles (/* */ and //) as well as the Perl and shell-script style (#).

Operators

In PHP, the assignment operators (=, +=, *=, etc.), Boolean operators (&&, ||,! ), comparison operators (,>, =, >=, ==,!=), and basic arithmetic operators (+, -, *, /, %) all work the same way as they do in C.

Control structures

The basic control structures (if, switch, while, and for) work the same way as they do in C. They also support break and continue. One difference that stands out is that PHP’s switch can use strings as case identifiers.

Function names

As you read the documentation, you’ll notice that many of the function names sound just like C function names.

Differences

Dollar signs

All variables are shown with a $ at the beginning. Variables don’t need to be declared before they’re used, and they don’t have their own type.

Types

PHP only has two types of numbers: integer, which is like a long in C, and double (corresponding to a double in C). String lengths can be anything. There is no one kind of person.

Type conversion

Types are not checked when the code is being compiled, and type errors rarely happen when the code is being run. Variables and values are automatically changed from one type to another as needed.

Arrays

The syntax of arrays is similar to the syntax of arrays in C, but they are used in completely different ways. They are actually associative arrays or hashes, and the index can be a number or a string. They don’t have to be declared or given out ahead of time.

No structure type

PHP doesn’t have structures, in part because arrays and objects make it unnecessary. Each item in a PHP array doesn’t have to be the same type.

No pointers

In PHP, there are no pointers, but the tapeless variables work in a similar way. PHP does support variable references. You can also do some things that function pointers do. For example, you can store function names in variables and then call the function by using the variable instead of the name.

No prototypes

Functions don’t have to be declared before their implementation is defined, as long as the definition can be found somewhere in the current code file or included files.

Memory management

The PHP engine is a garbage-collected (reference-counted) environment, so you don’t have to do any deallocation in small scripts. You should be able to make new structures, like strings and object instances, whenever you want. Objects can have a “destructor” defined in PHP5, but there is no “free” or “delete” command. When the last reference to an object goes away, before the memory is freed up, the destructor is called.

Compilation and linking

PHP scripts don’t need to be put together in a separate step.

Permissiveness

In general, PHP is more forgiving than C, especially in the way it handles types, so you can get away with making different kinds of mistakes. Errors happen less often than unexpected results.

People also search

Leave a Comment

Scroll to Top