This chapter will show how PHP and PERL are alike and how they are different. This will make it easy for PERL developers to learn PHP and avoid making common mistakes.
Similarities
- Compiled scripting languages
- Perl and PHP are both languages for writing scripts.
- This means that they are not used to make native executables that run on their own before they are run.
Syntax
The basic syntax of PHP is very similar to that of Perl, and both of them have a lot in common with the syntax of C. Whitespace doesn\’t affect the code, semicolons end statements, and curly braces group several statements into a single block. The name of the function comes first, followed by the arguments, which are put in parentheses and separated by commas.
Dollar-sign variables
In PHP, all variables look like scalar variables in Perl: they have a name followed by a dollar sign ($).
No declaration of variables
Like Perl, you don\’t have to say what kind of thing a PHP variable is before you use it.
Loose typing of variables
Like Perl, PHP variables only have the type of the value they are currently holding. The same type of variable can hold either a number or a string.
Strings and the interpolation of variables
Double-quoted strings (\”string\”) are more easily interpreted by PHP and Perl than single-quoted strings (\’string\’).
Differences
PHP is HTML-embedded
PHP can be used for any task as long as it is run from the command line, but it is usually connected to a Web server and used to make Web pages. If you\’re used to writing CGI scripts in Perl, the main difference between PHP and Perl is that you don\’t have to use print or heredoc statements to explicitly print large blocks of static HTML. Instead, you can just write the HTML outside of the PHP code block.
No @ or% variables
PHP only has one kind of variable, and it starts with a $. Scalar or compound variables can store any type of data that the language supports.
Hashes vs. arrays
In PHP, there is only one type of data called an array, which works like both hashes and arrays/lists in Perl.
Specifying arguments to functions
Calls to functions in PHP look a lot like calls to subroutines in Perl. On the other hand, function definitions in PHP usually need a list of formal arguments, like in C or Java. This is not the case in PERL.
Scope of variables in functions
By default, the scope of variables in Perl is set to \”global.\” This means that subroutines can see variables at the top level. This often means that globals are used all over the place in different functions. In PHP, variables inside of function definitions have a local scope by default.
There is no module system
In PHP, there isn\’t much difference between regular code files and files that are used as libraries.
Break and continue rather than next and last
PHP is more like C, and instead of next and last statement, it uses break and continue.
No elsif
The only difference in spelling is that Perl\’s elsif is PHP\’s elseif.
More kinds of comments
PHP has single-line comments in the style of Perl (#) as well as multiline comments in the style of C (/* comment */) and single-line comments in the style of Java (/ comment).
Regular expressions
PHP doesn\’t have a built-in syntax for regular expressions, but its \”Perl-compatible\” regular expression functions can do most of the same things.