PHP和Java之间有什么区别?

时间:2022-09-25 12:23:55

What are the main differences between PHP and Java that someone proficient in PHP but learning Java should know about?

PHP和Java之间的主要区别是什么?

Edit: I mean differences in the syntax of the languages, i.e their data types, how they handle arrays & reference variables, and so forth :)

编辑:我的意思是语言的语法不同。e他们的数据类型,他们如何处理数组和引用变量,等等)

4 个解决方案

#1


45  

Not an exhaustive list, and I'm PHP developer who did a tour of Java a while back so Caveat Emptor.

这并不是一个详尽的列表,我是PHP开发人员,我也曾对Java做过一段时间的研究,所以我的客户非常谨慎。

Every variable in Java needs to be prepended with a data type. This includes primitive types such as boolean, int, double and char, as well as Object data-types, such as ArrayList, String, and your own objects

Java中的每个变量都需要使用数据类型进行前缀。这包括原始类型,如布尔、int、double和char,以及对象数据类型,如ArrayList、String和您自己的对象

int  foo    = 36;
char bar    = 'b';
double baz  = 3.14;
String speech = "We hold these truths ...";
MyWidget widget = new MyWidget(foo,bar,baz,speech);

Every variable can only hold a value of its type. Using the above declarations, the following is not valid

每个变量只能保存其类型的值。使用上述声明,以下内容无效

foo = baz

Equality on objects (not on primitive types) checks for object identity. So the following un-intuitively prints false. Strings have an equality method to handle this.

对象的平等(而不是原始类型)检查对象的身份。因此,下面的代码不直观地输出为false。字符串有一个相等的方法来处理这个。

//see comments for more information on what happens 
//if you use this syntax to declare your strings
//String v1 = "foo";
//String v2 = "foo";

String v1 = new String("foo");
String v2 = new String("foo");

if(v1 == v2){
    pritnln("True");
}
else{
    println("False");
}

Arrays are your classic C arrays. Can only hold variables of one particular type, need to be created with a fixed length

数组是典型的C数组。只能保存一个特定类型的变量,需要创建一个固定长度的变量吗


To get around this, there's a series of collection Objects, one of which is named ArrayList that will act more like PHP arrays (although the holds one type business is still true). You don't get the array like syntax, all manipulation is done through methods

为了解决这个问题,有一系列集合对象,其中一个名为ArrayList,它的行为更像PHP数组(尽管hold one type business仍然是true)。没有像语法一样的数组,所有操作都是通过方法完成的。

//creates an array list of strings
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("My First Item"); 

ArrayLists still have numeric keys. There's another collection called HashMap that will give you a dictionary (or associative array, if you went to school in the 90s) like object.

arraylist还有数字键。还有一个叫做HashMap的集合,它会给你一个字典(或者关联数组,如果你在90年代上学的话),比如object。


ArrayLists and other collections are implemented with something called generics (the <String>). I am not a Java programmer, so all I understand about Generics is they describe the type of thing an Object will operate on. There is much more going on there.

arraylist和其他集合是用泛型实现的( )。我不是Java程序员,所以我对泛型的理解是它们描述了对象将要操作的对象的类型。还有更多的事情要做。


Java has no pointers. However, all Objects are actually references, similar to PHP 5, dissimilar to PHP 4. I don't think Java has the (depreciated) PHP &reference &syntax.

Java没有指针。但是,所有对象实际上都是引用,类似于PHP 5,与PHP 4不同。我不认为Java有(贬值的)PHP和引用和语法。


All method parameters are passed by value in Java. However, since all Objects are actually references, you're passing the value of the reference when you pass an object. This means if you manipulate an object passed into a method, the manipulations will stick. However, if you try something like this, you won't get the result you expect

所有方法参数都通过Java中的值传递。但是,由于所有对象实际上都是引用,所以在传递对象时传递引用的值。这意味着,如果您操作传递给方法的对象,那么操作将继续。但是,如果您尝试这样的方法,您将无法得到您期望的结果。

public void swapThatWontWork(String v1, String v2)
{
  String temp = var1;
  var1 = var2;
  var2 = temp;
}

It's as good a time as any to mention that methods need to have their return type specified, and bad things will happen if an method returns something it's not supposed to. The following method returns an int

这是一个需要指定方法返回类型的好时机,如果一个方法返回了它不应该返回的东西,那么就会发生糟糕的事情。下面的方法返回一个int类型

public int fooBarBax(int v1){
}

If a method is going to throw an exception, you have to declare it as such, or the compiler won't have anything to do with it.

如果一个方法要抛出一个异常,您必须声明它本身,否则编译器不会与它有任何关系。

public int fooBarBax(int v1) throws SomeException,AnotherException{
   ...
}

This can get tricky if you're using objects you haven't written in your method that might throw an exception.

如果您使用的对象没有在方法中编写,可能会抛出异常,那么这可能会很棘手。


You main code entry point in Java will be a method to a class, as opposed to PHPs main global entry point

Java中的主代码入口点将是类的方法,而不是PHPs的主全局入口点


Variable names in Java do not start with a sigil ($), although I think they can if you want them to

Java中的变量名不以sigil($)开头,尽管我认为如果您希望它们这样做,它们可以这样做


Class names in Java are case sensitive.

Java中的类名是区分大小写的。


Strings are not mutable in Java, so concatenation can be an expensive operation.

在Java中字符串不是可变的,所以连接可能是一个昂贵的操作。


The Java Class library provides a mechanism to implement threads. PHP has no such mechanism.

Java类库提供了一种实现线程的机制。PHP没有这种机制。


PHP methods (and functions) allow you have optional parameters. In java, you need to define a separate method for each possible list of parameters

PHP方法(和函数)允许您具有可选参数。在java中,需要为每个可能的参数列表定义一个单独的方法

public function inPHP($var1, $var2='foo'){}

public void function inJava($var1){
    $var2 = "foo";
    inJava($var1,$var2);
}
public void function inJava($var1,$var2){

}

PHP requires an explicit $this be used when an object calls its own methods methods. Java (as seen in the above example) does not.

当对象调用自己的方法方法时,PHP需要一个显式的$。Java(如上面的示例所示)没有。


Java programs tend to be built from a "program runs, stays running, processes requests" kind of way, where as PHP applications are built from a "run, handle the request, stop running" kind of way.

Java程序往往是由“程序运行、保持运行、处理请求”之类的方式构建的,而PHP应用程序是由“运行、处理请求、停止运行”之类的方式构建的。

#2


14  

I think these two languages (as well as their runtime systems) are too different to list all differences. Some really big ones that come to my head:

我认为这两种语言(以及它们的运行时系统)差异太大,无法列出所有的差异。一些非常大的问题浮现在我的脑海中:

  • Java is compiled to bytecode, PHP is interpreted (as Alan Storm pointed out, since PHP 4, it’s not, but it still behaves as if it was);
  • Java被编译成字节码,PHP被解释(正如Alan Storm所指出的,因为PHP 4不是,但它仍然表现得好像它是);
  • Java is strong and statically typed, while PHP is rather weakly and dynamically typed;
  • Java是强类型和静态类型,而PHP是弱类型和动态类型;
  • PHP is mostly used to dynamically generate Web pages. Java can do that too, but can do anything else as well (like Applets, mobile phone software, Enterprise stuff, desktop applications with and without GUI, 3d games, Google Web Toolkit...); and
  • PHP主要用于动态生成Web页面。Java也可以做到这一点,但也可以做其他任何事情(比如applet、手机软件、企业产品、带GUI和不带GUI的桌面应用程序、3d游戏、谷歌Web Toolkit…);和
  • add your favourite difference here
  • 在这里加入你最喜欢的不同之处

You will notice most differences when it’s time to, but what’s most important:

当你需要的时候,你会注意到大多数的不同,但最重要的是:

  • PHP offers OOP (object-oriented programming) as an option that is ignored in most projects. Java requires you to program the OOP way, but when learning Java with a background in a not-so-OOP-language, it’s really easy to mess things up and use OOP the wrong way (or you might call it the sub-optimum way or the inefficient way...).
  • PHP提供了OOP(面向对象编程)作为大多数项目中都忽略的选项。Java要求您编写OOP方法,但是当您以不那么OOP语言的背景学习Java时,很容易把事情搞糟,用错误的方式使用OOP(或者您可能把它称为次优方法或低效方法…)。

#3


6  

  • Java is strongly-typed. PHP isn't;
  • Java是强类型的。PHP不;
  • PHP does a lot of implicit type conversion, which can actually be problematic and is why PHP5 has operators like === and !==. Java's implicit type conversion is primarily limited to auto-boxing of primitive types (PHP has no primitive types). This often comes up.
  • PHP进行了大量隐式类型转换,这实际上是有问题的,这也是为什么PHP5中有=== =和!=这样的操作符。Java的隐式类型转换主要局限于基本类型的自动装箱(PHP没有基本类型)。这经常出现。

Consider:

考虑:

$val = 'a';
if (strpos('abcdefghij', $val)) {
  // do stuff
}

which is incorrect and will have the block not executed because the return index of 0 is converted to false. The correct version is:

这是不正确的,并且不会执行该块,因为返回索引0被转换为false。正确的版本是:

$val = 'a';
if (strpos('abcdefghij', $val) !== false) {
  // do stuff
}

Java conditional statements require an explicit boolean;

Java条件语句需要显式的布尔值;

  • PHP variables and arrays are all prepended by $ and otherwise indistinguishable;
  • PHP变量和数组都以$开头,否则无法区分;
  • The equivalent of PHP associative arrays is PHP Maps (eg HashMap). Associative arrays are ordered on insertion order and can be used like ordinary arrays (on the values). Theres one Map implementation that maintains insertion order in Java but this is the exception rather than the norm;
  • PHP关联数组的等效值是PHP映射(如HashMap)。关联数组是按插入顺序排列的,可以像普通数组一样使用(在值上)。在Java中有一个维护插入顺序的映射实现,但这是一个例外,而不是规范;
  • $arr['foo'] = 'bar' insert or update an element in an associative array. Java must use Map.put() and Map.get();
  • $arr['foo'] = 'bar'插入或更新关联数组中的元素。Java必须使用Map.put()和Map.get();
  • PHP5 has the equivalent of function pointers and anonymous functions (using create_function()); 5.3 introduces closures at the language level. Java must use inner classes for both, which is somewhat more verbose. Moreover, inner classes are limited in how they can access variables from the outer scope (read Java Closures on JavaPapers), making them not as powerful as true closures.
  • PHP5具有等价的函数指针和匿名函数(使用create_function()));5.3在语言级别引入闭包。Java必须同时使用内部类,这有点冗长。此外,内部类在如何从外部范围(在javapap上读取Java闭包)访问变量方面受到限制,这使得它们不如真正的闭包强大。
  • Variable declaration is optional in PHP;
  • PHP中变量声明是可选的;
  • Use of global variables within functions requires explicit use of the global keyword in PHP;
  • 在函数中使用全局变量需要在PHP中显式地使用全局关键字;
  • POST/GET parameters are, unless configured otherwise (register_globals()) automatically result in global variables of the same name. They can alternatively be accessed via the $_POST global variable (and $_SESSION for session variables) whereas support for these things comes from a JEE add-on called the servlets API via objects like HttpServletRequest and HttpSession;
  • POST/GET参数是,除非配置其他参数(register_globals())),否则会自动生成同名的全局变量。它们也可以通过$_POST全局变量(以及会话变量$_SESSION)进行访问,而对这些内容的支持来自于一个名为servlets API的JEE扩展,其对象包括HttpServletRequest和HttpSession;
  • Function declaration in PHP uses the function keyword whereas in Java you declare return types and parameter types;
  • PHP中的函数声明使用Function关键字,而Java中声明返回类型和参数类型;
  • PHP function names can't normally * whereas Java allows method overloading as long as the different method signatures aren't ambiguous;
  • PHP函数名通常不会冲突,而Java允许方法重载,只要不同的方法签名没有歧义;
  • PHP has default values for function arguments. Java doesn't. In Java this is implemented using method overloading.
  • PHP具有函数参数的默认值。Java没有。在Java中,这是通过方法重载实现的。
  • PHP supports the missing-method pattern, which is confusingly called "overloading" in the PHP docs.
  • PHP支持缺失方法模式,这在PHP文档中被混淆地称为“重载”。

Compare:

比较:

function do_stuff($name = 'Foo') {
  // ...
}

to

void doStuff() {
  doStuff("Foo");
}

void doStuff(String what) {
  // ...
}
  • String constants in PHP are declared using single or double quotes, much like Perl. Double quotes will evaluate variables embedded in the text. All Java String constants use double quotes and have no such variable evaluation;
  • PHP中的字符串常量使用单引号或双引号进行声明,这很像Perl。双引号将计算嵌入文本中的变量。所有Java字符串常量使用双引号,没有这样的变量评估;
  • PHP object method calls use the -> operator. Java uses the . operator;
  • PHP对象方法调用使用->操作符。Java使用。运营商;
  • Constructors in Java are named after the class name. In PHP they are called __construct();
  • Java中的构造函数以类名命名。在PHP中,它们被称为__construct();
  • In Java objects, this is implicit and only used to be explicit about scope and in certain cases with inner classes. In PHP5, $this is explicit;
  • 在Java对象中,这是隐式的,只用于显示范围,在某些情况下使用内部类。在PHP5中,这是显性的;
  • Static methods in Java can be called with either the . operator on an instance (although this is discouraged it is syntactically valid) but generally the class name is used instead.
  • Java中的静态方法可以用实例上的操作符(虽然不建议这样做,但语法上是有效的),但是通常使用类名。

These two are equivalent:

这两个是等价的:

float f = 9.35f;
String s1 = String.valueOf(f);
String s2 = "My name is Earl".valueOf(f);

but the former is preferred. PHP uses the :: scope resolution operator for statics;

但前者更受青睐。PHP使用::范围解析算子用于静态;

  • Method overriding and overloading is quite natural in Java but a bit of a kludge in PHP;
  • 方法重写和重载在Java中是很自然的,但在PHP中就有点复杂了;
  • PHP code is embedded in what is otherwise largely an HTML document, much like how JSPs work;
  • PHP代码嵌入到其他大部分HTML文档中,就像jsp是如何工作的;
  • PHP uses the . operator to append strings. Java uses +;
  • PHP使用。操作符添加字符串。Java使用+;
  • Java 5+ methods must use the ellipsis (...) to declare variable length argument lists explicitly. All PHP functions are variable length;
  • Java 5+方法必须使用省略号(…)显式地声明变量长度参数列表。所有PHP函数都是可变长度;
  • Variable length argument lists are treated as arrays inside method bodies. In PHP you have to use func_get_args(), func_get_arg() and/or func_num_args();
  • 变量长度参数列表被视为方法体中的数组。在PHP中,必须使用func_get_args()、func_get_arg()和/或func_num_args();
  • and no doubt more but thats all that springs to mind for now.
  • 毫无疑问还有更多,但这就是我现在想到的。

#4


1  

  • you could use JavaDoc tool to autogenerate documentation on your software. But you need to write comments in specific way.

    您可以使用JavaDoc工具在您的软件上自动生成文档。但是你需要以特定的方式写评论。

  • you can't run PHP on mobile phones :) There are a lot of run time environments and platforms. That means you need to think in advance which libraries there could be missing or which limitations there could be (screen size, memory limits, file path delimiter "/" or "\" e.g).

    您不能在移动电话上运行PHP:)有许多运行时环境和平台。这意味着您需要预先考虑哪些库可能会丢失,或者有哪些限制(屏幕大小、内存限制、文件路径分隔符“/”或“\”例如)。

#1


45  

Not an exhaustive list, and I'm PHP developer who did a tour of Java a while back so Caveat Emptor.

这并不是一个详尽的列表,我是PHP开发人员,我也曾对Java做过一段时间的研究,所以我的客户非常谨慎。

Every variable in Java needs to be prepended with a data type. This includes primitive types such as boolean, int, double and char, as well as Object data-types, such as ArrayList, String, and your own objects

Java中的每个变量都需要使用数据类型进行前缀。这包括原始类型,如布尔、int、double和char,以及对象数据类型,如ArrayList、String和您自己的对象

int  foo    = 36;
char bar    = 'b';
double baz  = 3.14;
String speech = "We hold these truths ...";
MyWidget widget = new MyWidget(foo,bar,baz,speech);

Every variable can only hold a value of its type. Using the above declarations, the following is not valid

每个变量只能保存其类型的值。使用上述声明,以下内容无效

foo = baz

Equality on objects (not on primitive types) checks for object identity. So the following un-intuitively prints false. Strings have an equality method to handle this.

对象的平等(而不是原始类型)检查对象的身份。因此,下面的代码不直观地输出为false。字符串有一个相等的方法来处理这个。

//see comments for more information on what happens 
//if you use this syntax to declare your strings
//String v1 = "foo";
//String v2 = "foo";

String v1 = new String("foo");
String v2 = new String("foo");

if(v1 == v2){
    pritnln("True");
}
else{
    println("False");
}

Arrays are your classic C arrays. Can only hold variables of one particular type, need to be created with a fixed length

数组是典型的C数组。只能保存一个特定类型的变量,需要创建一个固定长度的变量吗


To get around this, there's a series of collection Objects, one of which is named ArrayList that will act more like PHP arrays (although the holds one type business is still true). You don't get the array like syntax, all manipulation is done through methods

为了解决这个问题,有一系列集合对象,其中一个名为ArrayList,它的行为更像PHP数组(尽管hold one type business仍然是true)。没有像语法一样的数组,所有操作都是通过方法完成的。

//creates an array list of strings
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("My First Item"); 

ArrayLists still have numeric keys. There's another collection called HashMap that will give you a dictionary (or associative array, if you went to school in the 90s) like object.

arraylist还有数字键。还有一个叫做HashMap的集合,它会给你一个字典(或者关联数组,如果你在90年代上学的话),比如object。


ArrayLists and other collections are implemented with something called generics (the <String>). I am not a Java programmer, so all I understand about Generics is they describe the type of thing an Object will operate on. There is much more going on there.

arraylist和其他集合是用泛型实现的( )。我不是Java程序员,所以我对泛型的理解是它们描述了对象将要操作的对象的类型。还有更多的事情要做。


Java has no pointers. However, all Objects are actually references, similar to PHP 5, dissimilar to PHP 4. I don't think Java has the (depreciated) PHP &reference &syntax.

Java没有指针。但是,所有对象实际上都是引用,类似于PHP 5,与PHP 4不同。我不认为Java有(贬值的)PHP和引用和语法。


All method parameters are passed by value in Java. However, since all Objects are actually references, you're passing the value of the reference when you pass an object. This means if you manipulate an object passed into a method, the manipulations will stick. However, if you try something like this, you won't get the result you expect

所有方法参数都通过Java中的值传递。但是,由于所有对象实际上都是引用,所以在传递对象时传递引用的值。这意味着,如果您操作传递给方法的对象,那么操作将继续。但是,如果您尝试这样的方法,您将无法得到您期望的结果。

public void swapThatWontWork(String v1, String v2)
{
  String temp = var1;
  var1 = var2;
  var2 = temp;
}

It's as good a time as any to mention that methods need to have their return type specified, and bad things will happen if an method returns something it's not supposed to. The following method returns an int

这是一个需要指定方法返回类型的好时机,如果一个方法返回了它不应该返回的东西,那么就会发生糟糕的事情。下面的方法返回一个int类型

public int fooBarBax(int v1){
}

If a method is going to throw an exception, you have to declare it as such, or the compiler won't have anything to do with it.

如果一个方法要抛出一个异常,您必须声明它本身,否则编译器不会与它有任何关系。

public int fooBarBax(int v1) throws SomeException,AnotherException{
   ...
}

This can get tricky if you're using objects you haven't written in your method that might throw an exception.

如果您使用的对象没有在方法中编写,可能会抛出异常,那么这可能会很棘手。


You main code entry point in Java will be a method to a class, as opposed to PHPs main global entry point

Java中的主代码入口点将是类的方法,而不是PHPs的主全局入口点


Variable names in Java do not start with a sigil ($), although I think they can if you want them to

Java中的变量名不以sigil($)开头,尽管我认为如果您希望它们这样做,它们可以这样做


Class names in Java are case sensitive.

Java中的类名是区分大小写的。


Strings are not mutable in Java, so concatenation can be an expensive operation.

在Java中字符串不是可变的,所以连接可能是一个昂贵的操作。


The Java Class library provides a mechanism to implement threads. PHP has no such mechanism.

Java类库提供了一种实现线程的机制。PHP没有这种机制。


PHP methods (and functions) allow you have optional parameters. In java, you need to define a separate method for each possible list of parameters

PHP方法(和函数)允许您具有可选参数。在java中,需要为每个可能的参数列表定义一个单独的方法

public function inPHP($var1, $var2='foo'){}

public void function inJava($var1){
    $var2 = "foo";
    inJava($var1,$var2);
}
public void function inJava($var1,$var2){

}

PHP requires an explicit $this be used when an object calls its own methods methods. Java (as seen in the above example) does not.

当对象调用自己的方法方法时,PHP需要一个显式的$。Java(如上面的示例所示)没有。


Java programs tend to be built from a "program runs, stays running, processes requests" kind of way, where as PHP applications are built from a "run, handle the request, stop running" kind of way.

Java程序往往是由“程序运行、保持运行、处理请求”之类的方式构建的,而PHP应用程序是由“运行、处理请求、停止运行”之类的方式构建的。

#2


14  

I think these two languages (as well as their runtime systems) are too different to list all differences. Some really big ones that come to my head:

我认为这两种语言(以及它们的运行时系统)差异太大,无法列出所有的差异。一些非常大的问题浮现在我的脑海中:

  • Java is compiled to bytecode, PHP is interpreted (as Alan Storm pointed out, since PHP 4, it’s not, but it still behaves as if it was);
  • Java被编译成字节码,PHP被解释(正如Alan Storm所指出的,因为PHP 4不是,但它仍然表现得好像它是);
  • Java is strong and statically typed, while PHP is rather weakly and dynamically typed;
  • Java是强类型和静态类型,而PHP是弱类型和动态类型;
  • PHP is mostly used to dynamically generate Web pages. Java can do that too, but can do anything else as well (like Applets, mobile phone software, Enterprise stuff, desktop applications with and without GUI, 3d games, Google Web Toolkit...); and
  • PHP主要用于动态生成Web页面。Java也可以做到这一点,但也可以做其他任何事情(比如applet、手机软件、企业产品、带GUI和不带GUI的桌面应用程序、3d游戏、谷歌Web Toolkit…);和
  • add your favourite difference here
  • 在这里加入你最喜欢的不同之处

You will notice most differences when it’s time to, but what’s most important:

当你需要的时候,你会注意到大多数的不同,但最重要的是:

  • PHP offers OOP (object-oriented programming) as an option that is ignored in most projects. Java requires you to program the OOP way, but when learning Java with a background in a not-so-OOP-language, it’s really easy to mess things up and use OOP the wrong way (or you might call it the sub-optimum way or the inefficient way...).
  • PHP提供了OOP(面向对象编程)作为大多数项目中都忽略的选项。Java要求您编写OOP方法,但是当您以不那么OOP语言的背景学习Java时,很容易把事情搞糟,用错误的方式使用OOP(或者您可能把它称为次优方法或低效方法…)。

#3


6  

  • Java is strongly-typed. PHP isn't;
  • Java是强类型的。PHP不;
  • PHP does a lot of implicit type conversion, which can actually be problematic and is why PHP5 has operators like === and !==. Java's implicit type conversion is primarily limited to auto-boxing of primitive types (PHP has no primitive types). This often comes up.
  • PHP进行了大量隐式类型转换,这实际上是有问题的,这也是为什么PHP5中有=== =和!=这样的操作符。Java的隐式类型转换主要局限于基本类型的自动装箱(PHP没有基本类型)。这经常出现。

Consider:

考虑:

$val = 'a';
if (strpos('abcdefghij', $val)) {
  // do stuff
}

which is incorrect and will have the block not executed because the return index of 0 is converted to false. The correct version is:

这是不正确的,并且不会执行该块,因为返回索引0被转换为false。正确的版本是:

$val = 'a';
if (strpos('abcdefghij', $val) !== false) {
  // do stuff
}

Java conditional statements require an explicit boolean;

Java条件语句需要显式的布尔值;

  • PHP variables and arrays are all prepended by $ and otherwise indistinguishable;
  • PHP变量和数组都以$开头,否则无法区分;
  • The equivalent of PHP associative arrays is PHP Maps (eg HashMap). Associative arrays are ordered on insertion order and can be used like ordinary arrays (on the values). Theres one Map implementation that maintains insertion order in Java but this is the exception rather than the norm;
  • PHP关联数组的等效值是PHP映射(如HashMap)。关联数组是按插入顺序排列的,可以像普通数组一样使用(在值上)。在Java中有一个维护插入顺序的映射实现,但这是一个例外,而不是规范;
  • $arr['foo'] = 'bar' insert or update an element in an associative array. Java must use Map.put() and Map.get();
  • $arr['foo'] = 'bar'插入或更新关联数组中的元素。Java必须使用Map.put()和Map.get();
  • PHP5 has the equivalent of function pointers and anonymous functions (using create_function()); 5.3 introduces closures at the language level. Java must use inner classes for both, which is somewhat more verbose. Moreover, inner classes are limited in how they can access variables from the outer scope (read Java Closures on JavaPapers), making them not as powerful as true closures.
  • PHP5具有等价的函数指针和匿名函数(使用create_function()));5.3在语言级别引入闭包。Java必须同时使用内部类,这有点冗长。此外,内部类在如何从外部范围(在javapap上读取Java闭包)访问变量方面受到限制,这使得它们不如真正的闭包强大。
  • Variable declaration is optional in PHP;
  • PHP中变量声明是可选的;
  • Use of global variables within functions requires explicit use of the global keyword in PHP;
  • 在函数中使用全局变量需要在PHP中显式地使用全局关键字;
  • POST/GET parameters are, unless configured otherwise (register_globals()) automatically result in global variables of the same name. They can alternatively be accessed via the $_POST global variable (and $_SESSION for session variables) whereas support for these things comes from a JEE add-on called the servlets API via objects like HttpServletRequest and HttpSession;
  • POST/GET参数是,除非配置其他参数(register_globals())),否则会自动生成同名的全局变量。它们也可以通过$_POST全局变量(以及会话变量$_SESSION)进行访问,而对这些内容的支持来自于一个名为servlets API的JEE扩展,其对象包括HttpServletRequest和HttpSession;
  • Function declaration in PHP uses the function keyword whereas in Java you declare return types and parameter types;
  • PHP中的函数声明使用Function关键字,而Java中声明返回类型和参数类型;
  • PHP function names can't normally * whereas Java allows method overloading as long as the different method signatures aren't ambiguous;
  • PHP函数名通常不会冲突,而Java允许方法重载,只要不同的方法签名没有歧义;
  • PHP has default values for function arguments. Java doesn't. In Java this is implemented using method overloading.
  • PHP具有函数参数的默认值。Java没有。在Java中,这是通过方法重载实现的。
  • PHP supports the missing-method pattern, which is confusingly called "overloading" in the PHP docs.
  • PHP支持缺失方法模式,这在PHP文档中被混淆地称为“重载”。

Compare:

比较:

function do_stuff($name = 'Foo') {
  // ...
}

to

void doStuff() {
  doStuff("Foo");
}

void doStuff(String what) {
  // ...
}
  • String constants in PHP are declared using single or double quotes, much like Perl. Double quotes will evaluate variables embedded in the text. All Java String constants use double quotes and have no such variable evaluation;
  • PHP中的字符串常量使用单引号或双引号进行声明,这很像Perl。双引号将计算嵌入文本中的变量。所有Java字符串常量使用双引号,没有这样的变量评估;
  • PHP object method calls use the -> operator. Java uses the . operator;
  • PHP对象方法调用使用->操作符。Java使用。运营商;
  • Constructors in Java are named after the class name. In PHP they are called __construct();
  • Java中的构造函数以类名命名。在PHP中,它们被称为__construct();
  • In Java objects, this is implicit and only used to be explicit about scope and in certain cases with inner classes. In PHP5, $this is explicit;
  • 在Java对象中,这是隐式的,只用于显示范围,在某些情况下使用内部类。在PHP5中,这是显性的;
  • Static methods in Java can be called with either the . operator on an instance (although this is discouraged it is syntactically valid) but generally the class name is used instead.
  • Java中的静态方法可以用实例上的操作符(虽然不建议这样做,但语法上是有效的),但是通常使用类名。

These two are equivalent:

这两个是等价的:

float f = 9.35f;
String s1 = String.valueOf(f);
String s2 = "My name is Earl".valueOf(f);

but the former is preferred. PHP uses the :: scope resolution operator for statics;

但前者更受青睐。PHP使用::范围解析算子用于静态;

  • Method overriding and overloading is quite natural in Java but a bit of a kludge in PHP;
  • 方法重写和重载在Java中是很自然的,但在PHP中就有点复杂了;
  • PHP code is embedded in what is otherwise largely an HTML document, much like how JSPs work;
  • PHP代码嵌入到其他大部分HTML文档中,就像jsp是如何工作的;
  • PHP uses the . operator to append strings. Java uses +;
  • PHP使用。操作符添加字符串。Java使用+;
  • Java 5+ methods must use the ellipsis (...) to declare variable length argument lists explicitly. All PHP functions are variable length;
  • Java 5+方法必须使用省略号(…)显式地声明变量长度参数列表。所有PHP函数都是可变长度;
  • Variable length argument lists are treated as arrays inside method bodies. In PHP you have to use func_get_args(), func_get_arg() and/or func_num_args();
  • 变量长度参数列表被视为方法体中的数组。在PHP中,必须使用func_get_args()、func_get_arg()和/或func_num_args();
  • and no doubt more but thats all that springs to mind for now.
  • 毫无疑问还有更多,但这就是我现在想到的。

#4


1  

  • you could use JavaDoc tool to autogenerate documentation on your software. But you need to write comments in specific way.

    您可以使用JavaDoc工具在您的软件上自动生成文档。但是你需要以特定的方式写评论。

  • you can't run PHP on mobile phones :) There are a lot of run time environments and platforms. That means you need to think in advance which libraries there could be missing or which limitations there could be (screen size, memory limits, file path delimiter "/" or "\" e.g).

    您不能在移动电话上运行PHP:)有许多运行时环境和平台。这意味着您需要预先考虑哪些库可能会丢失,或者有哪些限制(屏幕大小、内存限制、文件路径分隔符“/”或“\”例如)。