C# 与 PHP 对照
同一个程序分别用 C# 和 PHP 编写,取自本课程的课时。
模块与首次运行
C# 会编译出带有入口点的程序集;PHP 会执行以 <?php 开头的脚本,并使用 require_once 加载模块。C# using 指令导入的是命名空间名称,而不是 PHP 风格的源文件。本课程使用普通的 PHP CLI,而不是 ASP.NET 或 PHP Web 框架。
C#1#nullable enable2using System;3using System.Collections.Generic;4using System.Linq;5 6class Program {7 8 static void Main() {9 Console.WriteLine("hello CLI");10 }11}PHP1<?php2declare(strict_types=1);3echo "hello CLI\n";分支与优先级
C# 和 PHP 都有三元表达式和短路布尔运算符。C# 要求这些条件中的操作数必须是布尔值,而 PHP 接受类型转换。PHP 还提供了优先级较低的 and/or 写法;移植组合规则时,请使用显式分组。
C#1#nullable enable2using System;3using System.Collections.Generic;4using System.Linq;5 6class Program {7 8 static void Main() {9 int n=4;Console.WriteLine(n>3?"high":"low");10 }11}PHP1<?php2declare(strict_types=1);3$n=4; echo $n>3?"high":"low"; echo "\n";PHP 数组就是列表
C# 数组的长度固定,而 List<T>.RemoveAt 会移动后续元素的位置。PHP 的 unset 会保留剩余的数字键,因为它的数组是有序映射。如果 PHP 的返回约定要求稠密列表,请使用 array_values。
C#1#nullable enable2using System;3using System.Collections.Generic;4using System.Linq;5 6class Program {7 8 static void Main() {9 var a=new List<int>{10,20,30};a.RemoveAt(1);Console.WriteLine("0,1:"+a.Count);10 }11}PHP1<?php2declare(strict_types=1);3$a=[10,20,30]; unset($a[1]); echo implode(",",array_keys($a)),"\n";函数和返回类型
C# 方法通常属于类型,而局部函数具有词法作用域。PHP 除了支持模块中的具名独立函数,也支持方法。PHP 声明的参数类型和返回类型会在运行时检查;标量参数的严格行为取决于调用方文件。
C#1#nullable enable2using System;3using System.Collections.Generic;4using System.Linq;5 6class Program {7static string Surround(string s){return "("+s+")";}8 static void Main() {9 Console.WriteLine(Surround("note"));10 }11}PHP1<?php2declare(strict_types=1);3function surround(string $s):string{return "(".$s.")";} echo surround("note"),"\n";异常与状态
C# 和 PHP 支持按类型捕获异常和 finally,但没有 Java 风格的受检异常。PHP 的 Error 和 Exception 都实现了 Throwable 接口;C# 的异常层次结构使用 System.Exception。修改状态前先进行验证,并捕获预期的契约失败,而不是掩盖编程错误。
C#1#nullable enable2using System;3using System.Collections.Generic;4using System.Linq;5 6class Program {7 8 static void Main() {9 try{throw new InvalidOperationException("bad");}catch(InvalidOperationException){Console.Write("caught");}finally{Console.WriteLine(":done");}10 }11}PHP1<?php2declare(strict_types=1);3try {throw new RuntimeException("bad");} catch(RuntimeException $e){echo "caught";} finally{echo ":done\n";}类和带类型的属性
C# 实例字段会获得默认值,包括 bool 的默认值 false。PHP 类型化属性在读取前必须初始化,并且不会自动获得特定类型的默认值。C# 自动属性可以封装存储;PHP 公共属性并不是任意的 C# getter/setter 对。
C#1#nullable enable2using System;3using System.Collections.Generic;4using System.Linq;5 6class Program {7class Flag{public bool Enabled {get;set;}}8 static void Main() {9 Console.WriteLine(new Flag().Enabled?"on":"off");10 }11}PHP1<?php2declare(strict_types=1);3class Flag {public bool $enabled=false;} $f=new Flag(); echo $f->enabled?"on\n":"off\n";
课程大纲
10 章48 节课1 个项目48 个挑战99 道测验题集合
6 节课- 06回顾:词频统计
最终挑战
2 节课常见问题
面向 C# 开发者的 PHP 适合谁学习?
适合已经会写 C#、想学 PHP 的人。每节课都从你熟悉的 C# 代码出发,展示 PHP 如何实现同样的事情,所以你可以跳过入门基础。
需要先会 PHP 吗?
不需要。课程从你的第一个 PHP 程序开始。你需要掌握 C# 基础:变量、条件、循环、函数和集合。
面向 C# 开发者的 PHP 有多少节课?
面向 C# 开发者的 PHP 包含 48 节互动课,你可以按自己的节奏完成。
面向 C# 开发者的 PHP 包含哪些内容?
本课程分为以下几章:值与输出, 控制流, 集合, 函数和模块, and more。
面向 C# 开发者的 PHP 免费吗?
是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。