In Java, if we run:
public class HelloWorld{
public static void main(String []args){
if (true) {
int test = 1;
} else {
int test = 2;
}
System.out.println(test);
}
}
It will throw:
HelloWorld.java:9: error: cannot find symbol
System.out.println(test);
^
symbol: variable test
location: class HelloWorld
1 error
However, in php, if we run:
<?php
//Enter your code here, enjoy!
if (true) {
$test = 1;
} else {
$test = 2;
}
echo $test;
It will print 1.
I suspect if that is because Java is strong typed language and php is weak typed language. Can someone give a more deep and lower level explanation?