", "!==" operators, Java does not have them."/> ", "!==" operators, Java does not have them.">

Home  >  Article  >  Backend Development  >  Is there any difference in syntax between php and java?

Is there any difference in syntax between php and java?

青灯夜游
青灯夜游Original
2020-08-18 14:14:432002browse

There are differences in syntax between php and java. Differences: 1. PHP has EOF, but Java does not; 2. The connectors between variables are different, Java uses " ", and PHP uses "."; 3. PHP has magic constants, but Java does not; 4. PHP has "==" =", "<>", and "!==" operators are not available in Java.

Is there any difference in syntax between php and java?

Recommended: "PHP Video Tutorial"

There is a difference in syntax between php and java. Let me introduce to you some syntax differences between php and java.

The difference between the basic syntax of PHP and Java. The difference here only distinguishes syntax and does not involve function calls

1. The way to declare variables

Java:

 int a = 10;

PHP:

$a = 10

2.EOF

This is not available in Java, so I don’t know what this is for yet
PHP:

echo <<<EOF
"hello"
EOF;

3.Constant

Java:

public final NUM = 10;

PHP:

define("NUM", 10);

4. Connectors between variables

Java:

int age = 18;
String str = "我今年"+18+"岁";

PHP:

$age = 18;
$str = "我今年" . $age . "岁";

5.if statement

About else-if
PHP can be written as elseif
java can only be written as else if
(The difference is the space between else and if)

6. Array declaration

Java:

// 方式1
int[] arr = new int[3];
arr[0] = 12;
arr[1] = 23;
arr[2] = 46;
// 方式2
int[] arr = {12, 23, 46}

PHP:
The array function is required to declare an array in PHP

// PHP中数组允许插入不同类型的数据
$arr = array("e1", "e2", 23, 45);

Get the array length:
java:

int[] arr = new int[3];
int count = arr.length();

php:

$arr = array("e1", "e2", 23, 45);
$arrLength = count($arr);

There is also something called an associative array in php, which is similar to map## in Java #

$array1 = array("key1" => "value1", "key2" => "value2", "key3" => "value3");
$array1["key4"] = "value4";
$array1["key5"] = "value5";
$array1["key6"] = "value6";
7. Function declaration method

Java:

public 返回值 函数名(参数){
    // sth;
}
php:

function 函数名(参数){
    //return 决定是否有返回值
}
8. Magic constant

There is no such thing in Java

PHP: A structure similar to
__XXX__, such as __LINE__ (current line)

9. Namespace

By the way Well, the namespace in PHP is similar to the Java package

10. Construction method

Java construction method declaration:

class A{
    public A(){}
}
php:

class A{
    function __construct($name){}
}
11 .Method call

Java:

实例.方法();
php:

实例->方法();
12. Class constant

java:

final int TAG = 1001;
php:

const TAG = 1001;
13. Execute the method of the parent class:

Java:

super.方法();
php:

parent::方法();
13. Method static variable

Java:

class A{
    public static int a = 10;
}
// 访问方式:
A.a
php:

class A{
    public static $a = 10;
}
// 访问方式:
A::$a;
14. Operator

Only list the ones that PHP has and Java does not have

Comparison operators:
PHP:

绝对等于:x === y
不等于:x <> y
绝对不等于:x !== y
Logical operators:

与:x and y
或:x or y
异或:x xor y
and so on....

For more programming-related knowledge, please visit:

Introduction to Programming! !

The above is the detailed content of Is there any difference in syntax between php and java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn