PHP Notes 3



PHP Notes 3 Lakshman Gupta 9:16 AM 10/8/2019

keys values
24 "Ram"
"Lakshman"
"Pi" 3.14159
PHP Tutorial
Conditionals and Looping Constructs
PHP includes if and elseif conditionals, as well as while and for loops, all with syntax similar to C. The example
below introduces these four constructs:

// Conditionals
if ($a)
{
print "a is true<br/>\n";
}
else if ($b)
{
print "b is true<br/>\n";
}
else
{
print "neither a or b is true<br/>\n";
}
// Loops
for ($j = 0; $j < 10; $j++)
{
print "j = $j<br />\n";
}
while ($d)
{
print "ok<br />\n";
$d = test_something();
}
do
{
$c = test_something();
} while ($c);
Array Traverse Constructs:
foreach ($array as $value) {...} // To traverse elements (values) in an array.
{ .... }
foreach ($array as $key => $value) {...} // To traverse elements (key + value) in an associative array.
{ .... }
Operators
Assignment operators: = += -= *= /= .= (string concatenation)
Arithmetic Operators
Example Name Result
$a + $b Addition Sum of $a and $b.
$a - $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.
Incrementing/Decrementing Operators
Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.