Php 简明教程
PHP For PERL Developers
PERL 是一种动态类型、高级且通用的编程语言。通常认为 PERL 是实用抽取和报告语言的缩写。而 PHP 也是一种通用脚本语言。最初 PHP 曾是“个人主页”的缩写,但如今它已被公认为一个递归缩写“PHP:超文本预处理器”。
PERL is a dynamically typed, high level and general-purpose programming language. It is normally believed that PERL is an acronym for Practical Extraction and Reporting Language. PHP on the other hand is also a general-purpose scripting language. Initially PHP used to be a short for Personal Home Page’, but these days it has now been recognized as a recursive acronym ‘PHP: Hypertext Preprocessor’.
本章节概述了 PHP 与 Perl 之间的主要相似性和差异。这将帮助 Perl 开发人员非常快速地了解 PHP 并避免常见错误。
In this chapter, certain major similarities and differences in between PHP and PERL are listed. This will help PERL developers to understand PHP very quickly and avoid common mistakes.
Similarities between PERL and PHP
Perl 和 PHP 均为脚本语言。它们不用于提前在执行之前构建本机独立可执行文件。
Both Perl and PHP are scripting languages. They are not used to build native standalone executables in advance of execution.
早期的 PHP 版本受到 Perl 的启发。PHP 的基本语法与 Perl 非常相似。两者都与 C 共享许多语法特性。它们的代码对空白不敏感,每条语句都以分号结尾。
Early versions of PHP were inspired by PERL. PHP’s basic syntax is very similar to PERL. Both share a lot of syntactic features with C. Their code is insensitive to whitespace, Each statement is terminated by semicolons.
PHP 和 Perl 都使用大括号将多个语句组织成一个单一代码块。函数调用以函数名开头,后跟用括号括起来并用逗号分隔的实际参数,在两种情况下都一样。
Both PHP and PERL use curly braces to 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, in both the cases.
-
All variables in PHP look like scalar variables in PERL: a name with a dollar sign ($) in front of it.
-
Since both the languages are dynamically typed, you don’t need to declare the type of a PHP as well as a PERL variable before using it.
-
In PHP, as in PERL, variables have no intrinsic type other than the value they currently hold. You can store either number or string in same type of variable.
-
Both PHP and Perl do more interpretation of double-quoted strings ("string") than of single-quoted strings ('string').
Differences between PERL and PHP
PHP 可以嵌入 HTML 中。虽然可以从命令行运行 PHP 脚本,但它更常用作 Web 服务器上的服务器端脚本语言,并用于生成网页。
PHP can be embedded inside HTML. Although it is possible to run a PHP script from the command line, it is more popularly used as a server-side scripting language on a Web server and used for producing Web pages.
如果你习惯了用 Perl 编写 CGI 脚本,则 PHP 中的主要区别在于你不再需要使用 print 或 heredoc 语句显式打印大块静态 HTML,而只需在 PHP 代码块之外编写 HTML 本身。
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 仅有一种变量,它以美元符号 ($) 开头。该语言中的任何数据类型都可以存储在(标量或复合)这些变量中。在 Perl 中,数组变量以 @ 符号为前缀。此外,哈希变量以 % 符号为前缀。
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. In PERL, the array variable is prefixed with @ symbol. Also, the hash variable is prefixed by % symbol.
与 Perl 不同,PHP 有一种称为数组的单一数据类型,该类型可以是 indexed array 或 associative array ,与 hash in PERL 类似。
Unlike PERL, PHP has a single datatype called an array which can be an indexed array or associative array, which is similar to hash in PERL.
Function calls in PHP 看起来很像 subroutine calls in PERL 。另一方面,PHP 中的函数定义通常需要某种形式的正式参数列表,就像 C 或 Java 中一样,这与 Perl 不同。
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 case in PERL.
Perl 中的 Scope of variables 默认情况下是全局的。这意味着顶级变量在子例程中是可见的。通常,这会导致在函数中滥用全局变量。在 PHP 中,函数定义中变量的范围默认情况下是局部的。
Scope of variables in PERL is global by default. 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.
PHP 中的 No module system 亦是如此。在 PHP 中,普通代码文件与用作导入库的代码文件之间没有真正区别。
No module system in PHP as such. In PHP there is no real distinction between normal code files and code files used as imported libraries.
Break and continue 而不是 next and last − PHP 更像是 C 语言,并且使用 break 和 continue,而不是像 Perl 中的 next 和 last 语句。
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 as in PERL.
No elsif − 一个次要拼写差异:Perl 的 elsif 是 PHP 的 elseif。
No elsif − A minor spelling difference: Perl’s elsif is PHP’s elseif.
除了 Perl 样式 (#) single-line comments 之外,PHP 还提供 C 样式 multiline comments (/* comment */)和 Java 样式单行注释(// comment)。
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 没有针对正则表达式特定于内置语法,但在其“Perl 兼容”正则表达式函数中具有相同的大部分功能。
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.