PHP for C Developers
The simplest way to think of PHP is as interpreted C that you can
embed in HTML documents. The language itself is a lot like C, except
with untyped variables, a whole lot of Web-specific libraries built in,
and everything hooked up 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
do not require separate prototypes.
Here we will put some similarities and diferences in PHP and C
Similarities:
- Syntax: Broadly speaking, PHP syntax is the same as in C:
Code is blank insensitive, statements are terminated with semicolons,
function calls have the same structure (my_function(expression1,
expression2)), and curly braces ({ and }) make statements into blocks.
PHP supports C and C++-style comments (/* */ as well as //), and also
Perl and shell-script style (#).
- Operators: The assignment operators (=, +=, *=, and so
on), the Boolean operators (&&, ||, !), the comparison
operators (<,>, <=, >=, ==, !=), and the basic arithmetic
operators (+, -, *, /, %) all behave in PHP as they do in C.
- Control structures: The basic control structures (if,
switch, while, for) behave as they do in C, including supporting break
and continue. One notable difference is that switch in PHP can accept
strings as case identifiers.
- Function names: As you peruse the documentation, you.ll see many function names that seem identical to C
functions.
Differences:
- Dollar signs: All variables are denoted with a leading $. Variables do not need to be declared in advance of
assignment, and they have no intrinsic type.
- Types: PHP has only two numerical types: integer (corresponding to a long in C) and double (corresponding
to a double in C). Strings are of arbitrary length. There is no separate character type.
- Type conversion: Types are not checked at compile time, and type errors do not typically occur at runtime
either. Instead, variables and values are automatically converted across types as needed.
- Arrays: Arrays have a syntax superficially similar to C's
array syntax, but they are implemented completely differently. They are
actually associative arrays or hashes, and the index can be either a
number or a string. They do not need to be declared or allocated in
advance.
- No structure type: There is no struct in PHP, partly
because the array and object types together make it unnecessary. The
elements of a PHP array need not be of a consistent type.
- No pointers: There are no pointers available in PHP,
although the typeless variables play a similar role. PHP does support
variable references. You can also emulate function pointers to some
extent, in that function names can be stored in variables and called by
using the variable rather than a literal name.
- No prototypes: Functions do not need to be declared before their implementation is defined, as long as the
function definition can be found somewhere in the current code file or included files.
- Memory management: The PHP engine is effectively a
garbage-collected environment (reference-counted), and in small scripts
there is no need to do any deallocation. You should freely allocate new
structures - such as new strings and object instances. IN PHP5, it is
possible to define destructors for objects, but there is no free or
delete. Destructors are called when the last reference to an object
goes away, before the memory is reclaimed.
- Compilation and linking: There is no separate compilation step for PHP scripts.
- Permissiveness: As a general matter, PHP is more
forgiving than C (especially in its type system) and so will let you
get away with new kinds of mistakes. Unexpected results are more common
than errors.
his chapter will list out major similarities and differences in
between PHP and PERL. This will help PERL developers to understand PHP
very quickly and avoid common mistakes.
Similarities:
- Compiled scripting languages: Both Perl and PHP are
scripting languages.This means that they are not used to produce native
standalone executables in advance of execution.
- Syntax: PHP's basic syntax is very close to Perl's, and
both share a lot of syntactic features with C. Code is insensitive to
whitespace, statements are terminated by semicolons, and curly braces
organize multiple statements into a single block. Function calls start
with the name of the function, followed by the actual arguments
enclosed in parentheses and separated by commas.
- Dollar-sign variables: All variables in PHP look like scalar variables in Perl: a name with a dollar sign ($) in front of it.
- No declaration of variables: As in Perl, you don.t need to declare the type of a PHP variable before using it.
- Loose typing of variables: As in Perl, variables in PHP
have no intrinsic type other than the value they currently hold. You
can store iether number or string in same type of variable.
- Strings and variable interpolation: Both PHP and Perl do more interpretation of double-quoted strings ("string") than of singlequoted strings ('string').
Differences:
- PHP is HTML-embedded: Although it is possible to use PHP
for arbitrary tasks by running it from the command line, it is more
typically connected to a Web server and used for producing Web pages.
If you are used to writing CGI scripts in Perl, the main difference in
PHP is that you no longer need to explicitly print large blocks of
static HTML using print or heredoc statements and instead can simply
write the HTML itself outside of the PHP code block.
- No @ or % variables: PHP has one only kind of variable,
which starts with a dollar sign ($). Any of the datatypes in the
language can be stored in such variables, whether scalar or compound.
- Arrays versus hashes: PHP has a single datatype called an array that plays the role of both hashes and arrays/lists in Perl.
- Specifying arguments to functions: Function calls in PHP
look pretty much like subroutine calls in Perl. Function definitions in
PHP, on the other hand, typically require some kind of list of formal
arguments as in C or Java which is not the csse in PERL.
- Variable scoping in functions: In Perl, the default scope
for variables is global. This means that top-level variables are
visible inside subroutines. Often, this leads to promiscuous use of
globals across functions. In PHP, the scope of variables within
function definitions is local by default.
- No module system as such: In PHP there is no real distinction between normal code files and code files used as imported libraries.
- Break and continue rather than next and last: PHP is more like C langauge and uses break and continue instead of next and last statement.
- No elsif: A minor spelling difference: Perl's elsif is PHP's elseif.
- More kinds of comments: In addition to Perl-style (#)
single-line comments, PHP offers C-style multiline comments (/* comment
*/ ) and Java-style single-line comments (// comment).
- Regular expressions: PHP does not have a built-in syntax specific to regular expressions, but has most of the
same functionality in its "Perl-compatible" regular expression functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16