A simple comparison between PHP and Java syntax

Source: Internet
Author: User
Tags alphanumeric characters

Recently a friend ready to go from Java to PHP, to help her write a simple introduction to PHP, simply tell the difference between Java and PHP.

PS. Ueditor is not going to work----just look at it.

 

PHP

Java

file format

<%%> or <%php%> The code is contained inside the

Save as . php file

write directly

Save as . Java file

language Type

Instant Compile execution

Static compilation Execution

Notes

PHP exclusive

# This is also a single-line comment

the same point as Java:

//&NBSP; This is a single-line comment

This is a multiline comment block

multiline

*/



variable naming

PHP Exclusive

variable to $ The beginning of the symbol, followed by the name of the variable

Same as Java:

variable names must begin with a letter or underscore

variable names cannot start with a number

variable names can contain only alphanumeric characters and underscores ( A-Z,0-9 and _)

variable names are case sensitive ($y and $Y are two different variables)



php is a weak type, used directly when needed, Do not declare

function test () {

  $x = ten;

 echo $x;

}

detailed how to use

/http www.w3school.com.cn/php/php_variables.asp

strongly typed, need to declare type and initialize

public void Test () {

 int x =10;

 system.out.println (x);

}

Simple Output

echo $x;

or

var_dump ($x)

System.out.println (x);

 

Variable Type

strings, integers, floating-point numbers, logic, arrays, objects, NULL.

Although PHP does not specify the type in the variable declaration, but there are these types on the mechanism of the language



Normal Array

$goods = Array (" item 1", " item 2");

Use

$goods [0], Get " Product 1 "

Get Array length

echo Count ($goods);

string[] goods = new string[]{
Item 1 "," item 2 "}

Use

Goods[0], Get " Product 1 "

Get Array length

System.out.println (goods.length);

Associative Arrays

$goodsInfo = array ("id" = "xxxx", "name" = " product 1", "price" = "20.00");

How to use:

$goodsInfo ["id"];

 

in the Java does not have an associative array in it, you want to implement similar functions only using Map

map<string,string> goodsinfo = new hashmap<string,string> ();

goodsinfo.put ("id", "xxxx");

goodsinfo.put ("name", " item 1");

goodsinfo.put ("Price", "20.00");

How to use:

goodsinfo.get ("id");

Associative array traversal

foreach ($goodsInfo as $key = = $val) {

echo " Product attributes: ". $key.", values: ". $val;

echo "<br/>";

}

Output Result:

Product attributes: ID, values: xxxx

... The following ellipsis

for (String Key:goodsInfo.keySet ()) {

 string str = String.Format (" Commodity properties: %s,

key,goodsinfo.get (key));

 system.out.println (str);

}

output result:

value: xxxx

operator

too much, too lazy to write, see for yourself

http://www.w3school.com.cn/php/php_operators.asp

 

Logical Conditions

If

If Else

If ElseIf Else

Switch

for

while

these are the same, there is nothing to say, said a PHP in, and Java not in the

Foreach

this and JS only applies to arrays, and is used to iterate through each key in the array. / value pairs.

for normal arrays

foreach ($array as $value) {

Direct use of $value

}

for associative arrays, the example is referenced in the above associative array

foreach ($array as $key = = $value) {

Direct use of $key and $value

}

function

and the JS in the same way, it's not written here and Java the difference.

built-in global variables

PHP mainly used in Web , so there are many global variables built in, and Java used in Request get the same, specific explanations look at the Web page

http://www.w3school.com.cn/php/php_superglobals.asp


Recommended books: "PHP in detail"

Recommended Sites: php100

A simple comparison between PHP and Java syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.