PHP ─ PHP for PERL Developers
This 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 either 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 single-quoted 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 language
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.