Syntax

General

PHPJavascript
Location Embedded between <?php and ?> in a .php file <head><script type="text /javascript"></script></head>
<head><script type="text/javascript" src="xxx.js"></script></head>
Comments // line comment
/* multiline comment */
// line comment
/* multiline comment */
Case Sensitive yes
Line Endings Lines optionally, but typically, end with a semicolon
Output echo "Hello"; or print "Hello"; document.write("Hello");

Javascript locations

The <script> </script> Javascript can appear in either the <head> or <body> of an HTML document. If it's in the body section it generates page content.

Operators

PHPJavascript
Arithmetic +, -, *, /, % +, -, *, /, %, ++, --
Assignment =, +=, -=, *=, /=, %=
Logical and, or, xor, !, &&, || &&, ||, !
Conditional (cond) ? (true) : (false)
Comparison ==, === (exactly equal), !=, >, <, >=, <=

Variables

Rules

PHPJavascript
Names must start with a dollar sign Names must begin with a letter or an underscore
The first character after the dollar sign must be a letter or underscore
Variables are declared with the var keyword
Names are case sensitive Names are case sensitive
Can be printed as print $some_var (no quotes), or print "Hello, $name" (double quotes), but not print 'Hello, $name' (single quotes).
String concatenation is done with a period: $city . ', ' . $state = Lexington, KY String concatenation is done with a plus sign. If one of the variables is a number the result is a string

Strings

PHPJavascript
$str = "This is a string"
echo $str = This is a string
strlen($str) = 16
strtolower($str) = this is a string
strtoupper($str) = THIS IS A STRING
$str .= " and more" = This is a string and more
Values in single quotes are treated literally, values in double quotes are interpreted. If $var="test"
Literal: echo 'var is equal to $var' = var is equal to $var
Interpreted: echo "var is equal to $var" = var is equal to test

Numbers

PHPJavascript
$n = 123456.789;

round($n) = 123457
round($n, 2) = 123456.79
number_format($n) = 123,457
number_format($n, 2) = 123,456.79

Statements & Loops

Conditions

PHPJavascript
if if (condition)
{
}
else if ()
{
}
else
{
}

Switch

PHPJavascript
switch switch ()
{
case 1:
  break;
case 2:
  break;
default:
}

Loops

PHPJavascript
for for (x=start; x<end; x++)
{
}
for for (x in array)
{
}
loop exit break;
continue continue;
while while ()
{
}

xxx

yyy

PHPJavascript
Free Web Hosting