Is there any difference in syntax between php and java?

青灯夜游
Release: 2023-03-04 17:46:02
Original
2058 people have browsed it

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;
Copy after login

PHP:

$a = 10
Copy after login

2.EOF

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

echo <<<EOF
"hello"
EOF;
Copy after login

3.Constant

Java:

public final NUM = 10;
Copy after login

PHP:

define("NUM", 10);
Copy after login

4. Connectors between variables

Java:

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

PHP:

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

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}
Copy after login

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

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

Get the array length:
java:

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

php:

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

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";
Copy after login
7. Function declaration method

Java:

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

function 函数名(参数){
    //return 决定是否有返回值
}
Copy after login
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(){}
}
Copy after login
php:

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

Java:

实例.方法();
Copy after login
php:

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

java:

final int TAG = 1001;
Copy after login
php:

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

Java:

super.方法();
Copy after login
php:

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

Java:

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

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

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

Comparison operators:
PHP:

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

与:x and y
或:x or y
异或:x xor y
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template