Home  >  Article  >  Backend Development  >  What is the syntax difference between php and java

What is the syntax difference between php and java

(*-*)浩
(*-*)浩Original
2019-05-08 16:21:457236browse

The difference in syntax between php and java is: in php, user-defined functions, classes, keywords, etc. are not case-sensitive, while variables are case-sensitive; in java, all Function names, keywords, classes, variables, etc. are all case-sensitive.

What is the syntax difference between php and java

The basic syntax of Java and PHP is basically the same. In fact, the basic syntax of most languages ​​​​is also the same, but they still have some subtle differences:

1.PHP is a scripting language, the code is executed on the server, and the results are returned to the browser in plain text.

2.PHP can run on a variety of different platforms: such as windows, linux, MaxOS, etc.

3.PHP scripts can be stored anywhere in the document, starting with .

4.PHP comments: PHP supports three comment methods:

A. Double slashes, for example: //This is to declare a variable

B.# symbol, for example : #This is to declare a variable

C./**/ For example: /*This is to declare a variable*/

java also supports three comment methods:

A. Double slashes, for example: //This is to declare a variable

B./**/ For example: /*This is to declare a variable*/

C./***/ For example: /**

*This is to declare a variable

*/

5. Case sensitivity issue in PHP: In PHP, all user-defined functions, classes and keywords (such as if, else, echo, etc.) are not case-sensitive, while variables are case-sensitive. of.

In java, all function names, keywords, classes, variables, etc. are case-sensitive.

6. PHP variable declaration: PHP is similar to Javascript. Both are reference types, and there is no need to specify the type when declaring. Java is a strong reference type, and its type must be specified when declaring.

The way to declare variables in PHP is: $X = 7; Note: The naming rules for java and PHP variables are the same.

7. The scope of variables in PHP:

A. global (global): can only be accessed outside the function

B. Local (local): Can only be accessed within the method body

C.static (static)

Global variables in java are allowed to be accessed within the method.

8.global keyword: You can use the global keyword in the function body to access global variables.

For example:

<span style="white-space:pre">	</span><pre name="code" class="html">
<?php
    $x=5;
    $y=10;
    
    function myTest() {  
    global $x,$y;
      $y=$x+$y;
    }
    
    myTest();
    echo $y; // 输出 15
?>

9. The difference between echo and print in PHP:

echo can output more than one string and is slightly faster than print

print can only output a string and return 1 (unlike java, which can use print to output a line of statements).

Note: In PHP, print can output variables in double quotes, but in java, variables cannot be output in double quotes.

For example:

pre name="code" class="html">$txt2="W3School.com.cn";
echo "Study PHP at $txt2";

同样的也能输出数组中的任意元素:
echo "My car is a {$cars[0]}";

echo and print can be used with or without parentheses. For example: echo and echo();

10. Classes in PHP: The implementation process is the same as that in java, both use the keyword class, but the data declarations are different.

<?php
class Car
{
  var $color;
  function Car($color="green") {
    $this->color = $color;
  }
  function what_color() {
    return $this->color;
  }
}
?>

11.Strings in PHP can be expressed using double quotes or single quotes. But in java only double quotes can be used.

For example: "string" 'string'

This is only part of the foundation. It can be said that these two languages ​​​​have a long history, so they have similarities but no differences.

The above is the detailed content of What is the syntax difference 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