Programming beginners learn PHP from a Java perspective-variables-


This article is written under the policy of learning about the language PHP by comparing it with Java. Therefore, although I have knowledge of Java, it is intended for people who are "what is PHP ??" (mainly the author). By the way, the author is a beginner of The ☆, which is Java learning period → about 3 months, PHP learning period → about 2 weeks, so there may be mistakes in the content, but I am not responsible at all! … But I would be grateful if you could point it out in the comments. I hope that it will serve as a reference for your learning.

The decisive difference between Java and PHP

First of all, about the big difference between the two languages ... Well, if you think about it normally, there are no words (knowledge) to return anything when you say "everything is different" because they are different languages in the first place. So let's leave that for now ... The big difference between Java and PHP is ** "compiler language" or "scripting language" **. … Compiler? Script? Somehow difficult horizontal characters appear and it is difficult to understand, but the brief explanation is as follows.

・ Java → Compiler language When the program is executed, it is converted (compiled) so that it can be read by a computer. The operation is fast because the conversion is completed first.

・ PHP → Script language When the program is executed, it is executed while being converted into the computer language one by one. The operation is slow because it is converted each time, but the source code is reflected quickly.

In other words, when you run a program, the difference is whether or not all of it is once converted into words used by the computer. For example, from the standpoint of a user who uses a certain program, it is better to convert only once at the beginning than to convert one by one, which saves the extra effort of conversion. On the other hand, from the standpoint of the developer of a certain program, if you want to execute only the changed part, you only need to convert that part, so the script language that converts it one by one rather than the compiler language that converts everything. The result will be reflected faster and more convenient. As you can see, Java and PHP have very different characteristics in terms of conversion to computer language. When I was just studying PHP, I was impressed with PHP, which reflects the results just by clicking the refresh button in the browser by modifying the source. In Java, I had to restart the server one by one, so I wasn't happy with it, so PHP is easy for me to learn while checking the operation.

About variables

From here, the main subject. Regarding variables, the major differences between Java and PHP are as follows.

-Java → Variable "type" is ** required ** (ex. String hoge = "Yamada")

-PHP → Variable "type" is ** unnecessary ** Just add \ $ to the beginning of the variable (ex. $ Hoge = "Yamada")

When declaring a variable, Java must declare a fixed "type". On the other hand, in PHP, you can create a variable just by adding "$" to the beginning of the variable name. In Java, there are fixed "types" such as String and int, and if you try to put a numerical value in a variable of String (string) type, you will be scolded as an error ... However, PHP will set the variable type. There is no rule to decide first, so you can put anything, whether it's a character or a number, in one variable. Regarding variables, Java has tight rules, and PHP has loose rules. However, PHP has no variable type, so you can put anything in it, but beginners like me should be careful about such an idea. The point is that PHP doesn't have variable types, but PHP takes care of converting variable types. For example, if you have the following example sentence:


$hoge = "a";
$fuga = 0;
if ($hoge == $fuga){
    //The above conditional expression is judged as true

What! In the conditional expression of this if statement, PHP recognizes the variable hoge and the variable fuga as equivalent. A = 0 is not the same as you normally think. Why does this happen? Actually, in PHP, when you try to compare a character with a number, you are careful to convert the character to a number. Moreover, if the first word of a letter does not have a valid number, it seems to recognize that letter as 0. So, in the above source, when I tried to convert the letter "a" to a number, there was no valid number in the letter, so I arbitrarily set it to "0". The above ** PHP is too careful and idle problem ** seems to occur in many places, not limited to if statements. For this reason, especially those with little programming experience, you should always be aware of variable types. (Self-discipline)


-Java → Variable requires a type. The rules are strict! It's annoying, but there are fewer mistakes.

・ PHP → If you add \ $ at the beginning, the type of the variable will be judged without permission. It's nice, but inflexible! Be careful for beginners.

This time is over.

Next time preview! !!

Undecided! !! Perhaps! Array! !! Such!

Thank you for visiting.

Supplement (About Java's automatic type conversion function)

I forgot that Java also had the ability to automatically convert types ... so here's a little more detail In Java, you can write as follows.


//Type conversion when calculating between numbers
double hoge = 2.5 * 5;
//String type and int type can be combined
String fuga = "windows" + 10;
//You can assign an int type number to a double type variable
double boee = 10;

Let's look at three examples in order

The top example is that if a calculation is done in a double variable hoge, the number will be double. Types that handle numbers include not only ints, but also doubles and floats that handle decimal numbers, longs that can store numbers larger than ints, and shorts and bytes that can store values smaller than ints. When doing calculations between numbers like this, Java takes care to convert the type to either type before doing the calculation. For example, it will calculate after 5 → 5.0 before calculating.

In the example in the middle, when concatenating a String type and a number, the number is converted to a String type and then combined. Character concatenation is just adding characters and characters (numerical values before conversion), so it can be said to be an example of automatic type conversion at the time of calculation, just like the example in a sense.

The bottom example is that you can assign a variable of one type to a variable of a different type. This is an automatic conversion for types that handle the numbers mentioned earlier. In this example, int, which is an integer numeric type, is assigned to double, which is a numeric type including decimals. If you sysout this, it will be "10.0", but I think some people can guess it. It is possible to express 10 as 10.0. So Java will automatically convert the type, saying "It's an int type, but should I convert it to double ...". Conversely, double-type numbers cannot be assigned to int-type variables. Certainly 10.0 can be expressed as 10. However, if the value to be assigned is 2.5, 2.5 cannot be represented by the int type, which is an integer type. In other words, integers can be represented by numbers that include decimals, but numbers that include decimals cannot be represented by integers alone, so this is the rule. In a certain reference book, it is expressed like this.

When assigning a value of "small type" to a box of "large type" semantically, the assigned value is automatically converted to the type of the variable to be assigned before the assignment is performed. (Nakayama / Kunimoto 2014 p.78)

For details, see "Automatic type conversion when Java is assigned".

As mentioned above, Java may also perform type conversion automatically. There is a lot to understand ...

References Kiyotaka Nakayama and Daigo Kunimoto (2014) "Introduction to Java for a refreshing understanding" Impress Corporation


From a fantasy movie


double harry = 9 + 0.75;
String potter = harry + "Track";

-Impressions- Somehow I came up with it so I tried it

Recommended Posts

Programming beginners learn PHP from a Java perspective-variables-
If a person from Java learns PHP
Java, abstract classes starting from beginners
Run a batch file from Java
Access Teradata from a Java application
[For programming beginners] What is a method?
Java starting from beginners, logical operators / conditional operators
Try running a Kubernetes Job from Java
From installing Eclipse to executing Java (PHP)
Access protected fields from grandchildren (Java / PHP)
GetInstance () from a @Singleton class in Groovy from Java
Connect to Aurora (MySQL) from a Java application
To become a programmer from a Java shop
I tried hitting a Java method from ABCL
Create Scala Seq from Java, make Scala Seq a Java List
A collection of simple questions for Java beginners
[Java] Get a random value from an array
[Swift 5.3] Learn from A Swift Tour Swift [Xcode 12.1] <10/28 under construction>
Minecraft BE server development from PHP to Java
Ssh connect using SSHJ from a Java 6 app
java programming basics
Technical books that programming beginners who start professional programmers from Java should read (2017 version)
java Generic Programming
How to jump from Eclipse Java to a SQL file
Call a method with a Kotlin callback block from Java
Java development for beginners to start from 1-Vol.1-eclipse setup
Using the database (SQL Server 2014) from a Java program 2018/01/04
[Note] Create a java environment from scratch with docker
[Java] How to erase a specific character from a character string
I made a Wrapper that calls KNP from Java
Call a program written in Swift from Processing (Java)
Generate models from JSON to Swift, PHP, C #, JAVA
~ I tried to learn functional programming in Java now ~
Quick learning Java "Introduction?" Part 3 Talking away from programming