Php 简明教程
PHP For C Developers
如果您有 C 编程的先验知识,学习 PHP 会变得容易得多,尤其是在基础方面。尽管 PHP 与 C 非常相似,但它捆绑了许多特定于 Web 的库,所有这些库都直接连接到您偏爱的 Web 服务器。
If you have a prior knowledge of C programming, learning PHP becomes a lot easier, especially the basics. Although PHP is a lot like C, it is bundled with a whole lot of Web-specific libraries, with everything hooked up directly to your favorite Web server.
考虑 PHP 的最简单方法是将其视为可以嵌入 HTML 文档中经过解释的 C。PHP 脚本也可以从命令行执行,很像 C 程序。
The simplest way to think of PHP is as interpreted C that you can embed in HTML documents. PHP script can also be executed from the command line, much like a C program.
语句和函数定义的语法应该很熟悉,但变量总是以 $ 作为前缀,函数不需要单独的原型。
The syntax of statements and function definitions should be familiar, except that variables are always preceded by $, and functions do not require separate prototypes.
让我们看一下 PHP 和 C 中的一些异同 −
Let us take a look at some of the similarities and differences in PHP and C −
Similarities Between C and PHP
与 C 类似,PHP 代码不区分空格,语句以分号终止。
Similar to C, PHP Code is blank insensitive, statements are terminated with semicolons.
函数调用具有相同的结构
function calls have the same structure
my_function(expression1, expression2) {
Statements;
}
大括号用于将多个语句放入块中。
Curly brackets are used to put multiple statements into blocks.
PHP 支持 C 和 C++ 样式的注释 (/* */ 和 //),还支持 Perl 和 shell 脚本样式 (#)。
PHP supports C and C++-style comments (/* */ as well as //), and also Perl and shell-script style (#).
Operators − 赋值运算符 (=, =, *= 等等)、布尔运算符 (&&, ||, !) 、比较运算符 (<,>, ⇐, >=, ==, !=) 以及基本算术运算符(, -, *, /, %)在 PHP 中的行为与 C 中的行为相同。
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 − 基本控制结构 (if, switch, while, for) 的行为如同在 C 中,包括 break 和 continue。一个值得注意的区别是,PHP 中的 switch 可以接受字符串作为 case 标识符。
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.
PHP 还具有 foreach 循环结构,用于遍历诸如数组的集合。
PHP also has foreach looping construct that traverses the collections such as arrays.
Function Names − 在您浏览文档时,您会看到许多看似与 C 函数相同的函数名称。
Function Names − As you peruse the documentation, you.ll see many function names that seem identical to C functions.
Differences Between C and PHP
Dollar Sign − 所有变量名称均以 $ 为前缀。无需在赋值前声明变量,并且它们没有固有类型。PHP 是一种动态类型语言,而 C 是一种静态类型语言。
Dollar Sign − All variable names are prefixed with a leading $. Variables do not need to be declared in advance of assignment, and they have no intrinsic type. PHP is a dynamically typed language, as against C being a statically typed language.
Types − PHP 只有两种数值类型:整数(对应于 C 中的 long)和 double(对应于 C 中的 double)。在 PHP 中,float 等同于 double。字符串的长度是任意的。PHP 中没有单独的 char 类型,如在 C 中的情况。
Types − PHP has only two numerical types: integer (corresponding to a long in C) and double (corresponding to a double in C). In PHP, float is synonymous to double. Strings are of arbitrary length. There is no separate char type in PHP, as is the case in C.
Type Conversion − C 是一种强类型语言,因为变量的类型必须在使用前声明,并且在编译时检查类型。而 PHP 是一种弱类型语言,在编译时不检查类型,类型错误通常不会在运行时发生。相反,变量和值会根据需要自动跨类型转换。
Type Conversion − C is a strongly typed language, as type of a variable must be declared before using, and the types are checked at compile time. PHP on the other hand, is a weakly typed language, 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 − 数组具有与 C 数组语法表面上相似的语法,但它们的实现方式完全不同。在 C 中,数组是相似数据类型的集合。在 PHP 数组中,项可以是不同类型的。PHP 数组实际上是关联数组或哈希,索引可以是数字或字符串。它们无需预先声明或分配。
Arrays − Arrays have a syntax superficially similar to C’s array syntax, but they are implemented completely differently. In C, an array is a collection of similar data types. In a PHP array, the items may be of different types. PHP arrays 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 Struct Type − C 中的 struct 关键字用于定义新的数据类型。PHP 中没有 struct 关键字或其等效项,部分原因是数组和对象类型一起使它变得不必要。PHP 数组的元素不必是一致的类型。
No Struct Type − The struct keyword in C is used to define a new data type. There is no struct keyword or its equivalent 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 − 指针是 C 中的一个重要概念。PHP 中没有指针可用,尽管无类型变量扮演着类似的角色。与 C 不同,PHP 支持变量引用。您还可以在某种程度上模拟函数指针,方法是函数名称可以存储在变量中并使用变量而不是文字名称进行调用。
No Pointers − Pointers are an important concept in C. There are no pointers available in PHP, although the tapeless variables play a similar role. Unlike C, 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 − 无需在定义其实现之前声明函数,只要可在当前代码文件或包含的文件的某个位置找到该定义。相反,C 函数必须在使用之前定义。
No Prototypes − Functions do not need 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. On the contrary, a C function must defined before it is used.
No main() − 在 C 程序中,main() 函数是入口点,无论它出现在代码的哪个位置。而 PHP 程序从脚本中的第一个语句开始执行
No main() − In a C program, the main() function is the entry point, irrespective of where it is present in the code. A PHP program on the other hand starts execution from the first statement in the script
Memory Management − PHP 引擎实际上是一个垃圾回收环境(引用计数),并且在小型脚本中无需执行任何取消分配。您应该自由分配新结构——例如新的字符串和对象实例。在 PHP5 中,可以为对象定义析构函数,但 C/C++ 中没有 free 或 delete 关键字。在释放内存之前,当对对象的最后一个引用消失时,调用析构函数。
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 destructor for objects, but there is are no free or delete keywords as in C/C++. Destructor are called when the last reference to an object goes away, before the memory is reclaimed.
Compilation and Linking − PHP 是解释性语言。因此,不会创建 PHP 脚本的编译版本。C 程序先被编译以获取目标代码,然后链接到必需的库以构建一个可执行文件。PHP 脚本没有单独的编译步骤。PHP 脚本无法变成自执行文件。
Compilation and Linking − PHP is an interpreted language. Hence, the compiled version of PHP script is not created. A C program is first compiled to obtain the object code, which is then linked to the required libraries to build an executable. There is no separate compilation step for PHP scripts. A PHP script cannot be turned into a self executable.
Permissiveness − 总的来说,PHP 比 C 更宽容(尤其是在其类型系统中),因此它会让您摆脱新的错误类型。意外结果比错误更常见。
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.