Allows you to perform an arthmetic or string operation by combining an assignment operator with an arithmetic or string operator.
Examples:
1234567
//Operator Example Meaning=$x=5;//Assign 5 to variable $x.+=$x+=3;//Add 3 to $x and assign result to $x.-=$x-=2;//Subtract 2 from $x and assign result to $x.*=$x*=4;//Multiply $x by 4 and assign result to $x./=$x/=2;//Divide $x by 2 and assign result to $x.%=$x%=2;//Divide $x by 2 and assign remainder to $x.
Autoincrement and Autodecrement Operators
Examples:
12345
//Operator Function What It Does Examples++$xPreincrementAdds1to$x$x=3;$x++;$xisnow4$x++PostincrementAdds1to$x$x=3;++$x;$xisnow4--$xPredecrementSubtracts1from$x$x=3;$x--;$xisnow2$x--PostdecrementSubtracts1from$x$x=3;--$x;$xisnow2
//Operator/Operands Function$x==$y//$x is equal to $y$x!=$y//$x is not equal to $y$x>$y//$x is greater than $y$x>=$y//$x is greater than or equal to $y$x<$y//$x is less than $y$x<=$y//$x is less than or equal to $y$x===$y//$x is identical to $y in value and type$x!==$y//$x is not identical to $y
The result of the comparison is either true or false
Numbers are compared as you would expect
Strings are compared letter by letter using ASCII values
if($age>18){echo'You can vote';}else{echo'You can not vote';}
12345
<?phpif($age>18):?> <h1>You can vote</h1><?phpelse:?> <h1>You can not vote</h1><?phpendif;?>
if/elseif
1 2 3 4 5 6 7 8 9101112
if($age>18){echo'You can vote';}elseif($age>16){echo'You can drive';}else{echo'Get back to school!'}
1234567
<?phpif($age>18):?> <h1>You can vote</h1><?phpelseif($age>16):?> <h1>You can drive</h1><?phpelse:?> <h1>Get back to school!</h1><?phpendif;?>
NOTE: elseif and else if can be used interchangably only when used with curly brackets ({}), not colons (:). In other words, the followoing will not work:
1234567
<?phpif($age>18):?> <h1>You can vote</h1><?phpelseif($age>16):?> // ILLEGAL! <h1>You can drive</h1><?phpelse:?> <h1>Get back to school!</h1><?phpendif;?>
switch
1 2 3 4 5 6 7 8 910111213141516171819
$hungry_for='chocolate'switch($hungry_for){case'burgers':echo'Head to the Dane';break;case'ice cream':echo'Go to Culvers, and it is custard!';break;case'chocolate':echo'Grab a Snickers';break;case'pad thai':echo'Bandung has the best';break;default:echo'I do not have any suggestions for you';break;}
Verifying Variables
You can use == to check for empty strings, however it is better to use the built-in PHP functions
isset() will check that a variable exists and is set
empty() will check that a variable has any contents
$word1='flibertigibbits';// $word1 is set, but not empty$word2='';// $word2 is set, but it is also emptyif(isset($word1)){echo'$word1 is set<br />';}else{echo'$word1 is NOT set<br />';}if(empty($word1)){echo'$word1 is empty<br />';}else{echo'$word1 is NOT empty<br />';}if(isset($word2)){echo'$word2 is set<br />';}else{echo'$word2 is NOT set<br />';}if(empty($word2)){echo'$word2 is empty<br />';}else{echo'$word2 is NOT empty<br />';}if(isset($word3)){echo'$word3 is set<br />';}else{echo'$word3 is NOT set<br />';// $word3 is undefined so it is not set}if(empty($word3)){echo'$word3 is empty<br />';// $word3 is considered empty even though it's undefined}else{echo'$word3 is NOT empty<br />';}
Compound Conditional Logic
You can test multiple conditions by using the logical operators && and || (also works with and and or)
123456789
if(!empty($first_name)&&!empty($last_name)){echo'The full name is valid<br />';}if(!empty($first_name)||!empty($last_name)){echo'Either $first_name is valid, OR $last_name is valid, OR both are valid<br />';}
Boolean Flags
The use of boolean flags is a way to avoid duplicate code. Check out this example (fullName.php):
<?php$first_name=$_POST['firstname'];$last_name=$_POST['lastname'];if(empty($first_name)&&empty($last_name)){echo'You forgot to enter first name and last name';?> <form action="fullName.php" method="post"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /><br /> <input type="submit" value="Submit Fullname" name="submit" /> </form><?php}elseif(empty($first_name)&&!empty($last_name)){echo'You forgot to enter first name';?> <form action="fullName.php" method="post"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /><br /> <input type="submit" value="Submit Fullname" name="submit" /> </form><?php}elseif(!empty($first_name)&&empty($last_name)){echo'You forgot to enter last name';?> <form action="fullName.php" method="post"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /><br /> <input type="submit" value="Submit Fullname" name="submit" /> </form><?php}else// Both first name AND last name are filled in, form entry is validated{echo'Thanks for submitting your full name!';}?>
Here is how we can use boolean flags to reduce duplicate code (fullNameBoolFlags.php)
<?php$output_form=true;if(isset($_POST['submit'])){$first_name=$_POST['firstname'];$last_name=$_POST['lastname'];if(empty($first_name)&&empty($last_name)){echo'You forgot to enter first name and last name';}elseif(empty($first_name)&&!empty($last_name)){echo'You forgot to enter first name';}elseif(!empty($first_name)&&empty($last_name)){echo'You forgot to enter last name';}else// Both first name AND last name are filled in, form entry is validated{$output_form=false;echo'Thanks for submitting your full name!';}}if($output_form){?> <form action="fullNameBoolFlags.php" method="post"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /><br /> <input type="submit" value="Submit Fullname" name="submit" /> </form><?php}?>
<?phpif(isset($_POST['submit']))// The form has been submitted{$first_name=$_POST['firstname'];$last_name=$_POST['lastname'];$output_form=false;if(empty($first_name)&&empty($last_name)){echo'You forgot to enter first name and last name';$output_form=true;}elseif(empty($first_name)&&!empty($last_name)){echo'You forgot to enter first name';$output_form=true;}elseif(!empty($first_name)&&empty($last_name)){echo'You forgot to enter last name';$output_form=true;}else// Both first name AND last name are filled in, form entry is validated{echo'Thanks for submitting your full name!';}}else// Form has never been submitted{// Initialize to empty strings$first_name="";$last_name="";$output_form=true;}if($output_form){?> <form action="<?phpecho$_SERVER['PHP_SELF'];?>" method="post"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" value="<?phpecho$first_name;?>" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" value="<?phpecho$last_name;?>" /><br /> <input type="submit" value="Submit Fullname" name="submit" /> </form><?php}?>
Unique Table Rows
We should be able to identify table rows uniquely
The best way to take a table that doesn’t provide the ability to identify table rows uniquely is to add a new column that contains a unique id using ALTER TABLE