PHP
Variables:
$x=5;
$word="hello";
Printing:
echo "This is a sentence";
print "The value is $x";
Comparison Operators:
== (equals)
!= (not equals)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
Logical Operators
Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.
Conditional statements:
if($x > 5) {
print "$x is greater than 5";
}
if($word == "hello") {
print "The word is hello";
}
else {
print "The word is not hello";
}
For loops:
for($i=starting number; $i<=ending number; $i++) {
//Everything between these braces is repeated
}
//start at 0; end at 30; add 1 to $i each time
for($i=0; $i<=30; $i++) {
echo "i is currently $i";
}
//start at 1; end at 19; add 5 to $i each time
for($i=1; $i<20; $i=$i+5) {
echo "i is currently $i";
}
Declaring Functions:
function subtract($x, $y) {
$value = $x-$y;
return $value;
}
function print_hello() {
echo "Hello World";
}
Using Functions:
$result=$subtract(10,5);
echo "$result
";
print_hello();