Archive for November 14, 2010

Learn PHP-20 : The ?(ternary) Operator

Posted: November 14, 2010 in PHP
Tags:

There is an operator in PHP that is very similar in functionality to the if statement called the ternary (?) conditional operator. It evaluates to check if the first subexpression is true, if it is it will return the value of the second subexpression, if the first subexpression is false it will return the value of the third subexpression.

Here is a code example using flavor variables and checking to see if the shop has our favorite flavor using ?:

<?php
// Let's say our choice is chocolate
$flavor_choice = "chocolate";
// And we do not know it yet but all they have is vanilla
$output = ($flavor_choice == "vanilla") ? "Yes, we have vanilla." : "Sorry we do not have $flavor_choice.";
// output the appropriate subexpression
echo "$output";
?>

The output is shown below :

Sorry we do not have chocolate.

Learn PHP-19 : Concatenation Assignment Operators

Posted: November 14, 2010 in PHP
Tags:

The concatenation operator ( . ) returns the combined value of its right and left values. The variable’s data type has an affect on the output.

Here are code examples using the concatenation operator ( . ) to unite or combine variable values

<?php
echo "deve"."lop";          //displays "develop"
echo "<br /><br />";            // html line breaks

echo "deve" . "lop";        //displays "develop"
echo "<br /><br />";            // html line breaks

echo 4 . 3;                     //displays "43"
echo "<br /><br />";            // html line breaks

echo 4.3;                       //displays "4.3"
echo "<br /><br />";            // html line breaks

echo "4" . "3";                //displays "43"
echo "<br /><br />";            // html line breaks

echo '4' . '3';                  //displays "43"
?>

(more…)

Learn PHP-18 : Logical Operators

Posted: November 14, 2010 in PHP
Tags:

Logical operators are used when we want to combine operations and expressions into a set of logical comparisons. They are usually used in conjuction with “if and “else” statements to lay out the dynamic logic we need in many situations as we advance in our PHP development. We discuss if and else statements in later lessons.

The following table shows their usage and result logic:

Name Usage Result
And $var1 &&
$var2
TRUE if both $var1 and $var2
are TRUE
And $var1 and $var2 TRUE if both $var1 and $var2
are TRUE
Or $var1 or $var2 TRUE if either $var1 or
$var2 is TRUE
Or $var1 || $var2 TRUE if either $var1 or
$var2 is TRUE
Xor $var1 xor $var2 TRUE if either $var1 or
$var2 is TRUE, but not both
Is Not !$var1 TRUE if $var1 is not TRUE

(more…)