For doing what it sounds like you're doing (adding executable code into existing HTML pages), I think PHP is your best and easiest choice supported by all webhosts for free, so forge ahead with it. It's universal and useful, worth knowing.
[If you're looking for a language to replace AppleSoft BASIC to get tasks done on your local PC -- I sold my AppleSoft BASIC user manual on eBay just a few years ago -- I'd suggest Perl as the easiest language to get tasks done quickly and easily, the best language to do the things that people once used the various BASICs for.]
But PHP is quirky and in a number of ways forgiving, so it is very easy to write code that runs just fine and yet does not do what you expected it to. Pay close attention to the PHP manual and its user comments for every function you use, to be alerted to its quirks, and so you can compensate for PHP's sloppiness by writing your own code to be meticulous and avoid ambiguity.
(Forget about Java for this purpose, but, full disclosure, I have forgotten about Java for all purposes and am biased.)
Some specifics:
#1 It's only a ### missing semicolon! You carry on as if I'd commented-out the whole page.
Not having a line end until the semicolon is very handy when it comes to formatting lines for readability.
#2 Can't you reserve $ for string variables like a normal language?
There are no string variables, just variables. A variable can be a string one second and a number the next (or a string interpreted as if it were a number, or a number interpreted as a string).
#3 OK, so I used parentheses ( ) instead of braces { }. Didn't the IF give you just a teeny bit of a clue what I was getting at?
PHP: "What am I, psychic?"
#4 Just because I said = instead of == is no excuse to play dead. I mean, honestly.
Yes, assignment vs. equality test, important distinction inherited from the C language! But for equality test, look at === (three equals!); often it is the best choice, most likely to make the comparison you intend. Also !== .
#6 What do you mean, you didn't have any way of telling me that I inadvertently opened my local file directly instead of through MAMP?
PHP: "If you don't open the file through MAMP, I don't launch, and if I don't launch, I can't do anything, not even complain!"