echo "This text is generated from a php script!!!
";
This text is generated from a php script!!!
//create variables
$favColor = "teal";
$favNumber = "17";
//write out variables
echo "My favorite color is $favColor
";
echo "My favorite number is $favNumber
";
My favorite color is teal
My favorite number is 17
//create variables
$favColor="teal";
$favNumber="17";
//perform calculations
$addNum = $favNumber + 10;
$subNum = $favNumber - 10;
$multNum = $favNumber * 10;
$divNum = $favNumber / 10;
$modNum = $favNumber % 10;
//write out results
echo "My favorite color is $favColor
";
echo "My favorite number is $favNumber
";
echo "Calculations using $favNumber and 10:
";
echo "
Add: " .$addNum;
echo "
Subtract: " .$subNum;
echo "
Multiply " .$multNum;
echo "
Divide: " .$divNum;
echo "
Modulus: " .$modNum;
My favorite color is teal
My favorite number is 17
Calculations using 17 and 10:
Add: 27
//create variables
$stringVarColor = "red";
echo '$stringVarColor' . ' = "red" <br>';
$intVarNumber = "1";
echo '$intVarNumber' . ' = "1" <br>';
$stringVarPet = "dog";
echo '$stringVarPet ' . ' = "dog"';
echo "<br>";
echo "<br>";
//write favorite color
echo "<p>Your favorite color is: $stringVarColor</p>";
//test to see if favorite color is a hot color
echo "<em>result of conditional statement</em>: ";
if ($stringVarColor == "red" or $stringVarColor == "orange" or $stringVarColor == "yellow") {
echo "You like it hot!</p>";}
echo "<br>";
//write out favorite number
echo "<p>Your favorite number is: $intVarNumber</p>";
//test to see if favorite number is a greater than 10
echo "<em>result of conditional statement</em>: ";
if ($intVarNumber > 10){
echo "<p>Whoa! That's a big number.</p>";}
else{echo "<p>A good simple number</p>";}
echo "<br>";
//write out the pet variable
echo "<p>Your pet is a $stringVarPet</p>";
//test to see what kind of pet
echo "<em>result of conditional statement</em>: ";
if($stringVarPet=="none"){
echo"I'm sorry you dont have a pet.</p>";}
elseif($stringVarPet == "dog"){
echo "A dog can be your best friend.</p>";}
elseif($stringVarPet == "cat"){
echo "Cats are cool</p>";}
else{echo "That's an interesting pet!</p>";}
echo "<br>";
Demonstrating conditional statements. Hard-coded variables (constants) are used here.
$stringVarColor = "red"Your favorite color is: red
result of conditional statement: You like it hot!Your favorite number is: 1
result of conditional statement:A good simple number
Your pet is a dog
result of conditional statement: A dog can be your best friend.
//create variable
$price = 100;
//1. FOR LOOP------------------------------
//print title
echo "1. For Loop: Discount price by Week | Displaying 8 Weeks with 10% discount each week:";
echo "
'price' starts as $price";
echo "
'discountedPrice' starts as uninitialized";
echo "
'x' is initialized as 0 in loop statement";
echo "
";
for ($x=0; $x<8 $x++;){
$discountedPrice = $price - ($price * $x *.10);
echo "
Week " . ($x+1) . ": \$" . round($discountedPrice, 2);
}
echo "
";
//2. WHILE LOOP------------------------------
//print title
echo "2. While Loop: Discount price by Week with $20 Minimum Price";
echo "
'price' starts as $price";
echo "
'discountedPrice' starts as $discountedPrice but is manually reset to 100";
echo "
'x' starts as $x but is manually reset to 0";
echo "
";
//initialize $x to 0 before loop.
$x = 0;
//set %discPrice to $price the first time, so the loop will run
$discountedPrice = $price;
while ($discountedPrice > 10){
$discountedPrice = $price - ($price * $x *.10);
echo "
Week " . ($x+1) . ": \$" .round($discountedPrice, 2);
$x=$x+1;
}
echo "
";
//3. DO WHILE LOOP------------------------------
//print title
echo "3. Do While Loop: Quantity Discount:";
echo "
'price' starts as $price";
echo "
'discountedPrice' starts as $discountedPrice but is reset to 100";
echo "
'x' starts as $x but is manually reset to 0";
echo "
";
//initialize $x to 0 before loop.
$x = 0;
do{
$discountedPrice = $price - ($price * $x * .01);
echo "
Minimum quantity: " . $x . ": \$" .round($discountedPrice, 2);
//increment counter
$x=$x+10;
}
while($x <= 70);
echo "
";
1. For Loop: Discount price by Week | Displaying 8 Weeks with 10% discount each week:
'price' starts as 100
'discountedPrice' starts as uninitialized
'x' is initialized as 0 in loop statement
2. While Loop: Discount price by Week with $20 Minimum Price
'price' starts as 100
'discountedPrice' starts as 30 but is manually reset to 100
'x' starts as 8 but is manually reset to 0
3. Do While Loop: Quantity Discount:
'price' starts as 100
'discountedPrice' starts as 10 but is reset to 100
'x' starts as 10 but is manually reset to 0
$download = "true";
$shipping = "2.99";
$title = "Cost by Quantity";
$downloadTitle = "- Downloads
";
$cdTitle = "- CDs | Shipping is $2.99 per order";
if($download=="true"){
echo $title . $downloadTitle;
$price="9.99";
$quantity=1;
while ($quantity < 7){
$price = $price * $quantity;
echo "$quantity for $price
";
$quantity = $quantity + 1;
$price="9.99";
}
}
$download = "false";
if($download=="false"){
echo $title . $cdTitle;
$price = "12.99";
for($quantity=1; $quantity < 7; $quantity++){
$price = $price * $quantity;
$total = $price + $shipping;
echo "$quantity for $total
";
$price="12.99";
}
}
Cost by Quantity- Downloads
1 for 9.99Cost by Quantity- CDs | Shipping is $2.99 per order
1 for 15.98
function function1tax($pricearg, $quantityarg){
define('TAXRATE',.0825);
$taxAmtFunctionSide=
round($pricearg * $quantityarg * TAXRATE,2);
return $taxAmtFunctionSide;
}
$itemPrice=99.99;
$qty=3;
echo "You have ". $qty ." items @ \$" . $itemPrice . " each...
";
$pretaxtotal = $itemPrice * $qty;
echo"The subtotal: \$". $pretaxtotal . "
";
$taxAmtScriptSide = function1tax($itemPrice,$qty);
//calling tax function and passing args price and qty
echo "The tax Amt: \$". $taxAmtScriptSide . "
";
$finaltotal=$itemPrice * $qty + $taxAmtScriptSide;
echo "Your final Total: \$". $finaltotal . "
";
echo "----------------------------
Thanks for shopping!
";
You have 3 items @ $99.99 each...
The subtotal: $299.97
The tax Amt: $24.75
Your final Total: $324.72
----------------------------
Thanks for shopping!
function calcDiscountStandalone($qtyarg, $pricearg){
//0 items no disc, 1 item no disc, 2 items 5pct disc, etc
$discountArray=array(0,0,.05,.10,.15,.20,.25,.30,.35,.40,.45);
//$discountArray[$qtyarg] indicates array index
$discountPrice=$pricearg-($pricearg*$discountArray[$qtyarg]);
$discountedTotalFunctionSide=$discountPrice*$qtyarg;
return $discountedTotalFunctionSide;
}
//associative array
$menuItemsArray=array(
"Hamburger"=>1.99,
"Cheeseburger"=>2.49,
"Fries"=>.99,
"Soda"=>1.29,
"Shake"=>1.79
);
echo"Price for 1:";
foreach($menuItemsArray as $itemKeyElem=>$priceValElem){
$discountedTotalScriptSide=round(calcDiscountStandalone(1,$priceValElem),2);
//element 1 in discount array bc qty is 1
//also plug in price to apply to discount pct
//round the result to 2 decmial places
echo"
$itemKeyElem: \$$discountedTotalScriptSide";
}
echo"
";
echo"Price for 5:";
foreach($menuItemsArray as $itemKeyElem=>$priceValElem){
$discountedTotalScriptSide=round(calcDiscountStandalone(5,$priceValElem),2);
//element 5 in discount array bc qty is 5
//also plug in price to apply to discount pct
//round the result to 2 decmial places
echo"
$itemKeyElem: \$$discountedTotalScriptSide";
}
echo"
";
//calc an order of 5 hamburgers, 1 cheeseburger,
//3 fries, 2 sodas, and 2 shakes
echo"
Your Order:";
//reset this
$discountedTotalScriptSide=0;
$itemTotal=round(CalcDiscountStandalone(5,$menuItemsArray['Hamburger']),2);
//using calcDiscount function:
//5 hamburgers, element 5's contents in discount array as param 1
//price of 'Hamburger' from menuItemsArray as param 2
echo"
5 Hamburgers: \$$itemTotal";
//accumulator
$discountedTotalScriptSide=$discountedTotalScriptSide+$itemTotal;
$itemTotal=round(CalcDiscountStandalone(3,$menuItemsArray['Cheeseburger']),2);
//using calcDiscount function:
//3 cheesburger, element 3's contents in discount array
//price of 'Cheeseburger' from menuItemsArray as param 2
echo"
3 Cheeseburgers: \$$itemTotal";
//accumulator
$discountedTotalScriptSide=$discountedTotalScriptSide+$itemTotal;
$itemTotal=round(CalcDiscountStandalone(3,$menuItemsArray['Fries']),2);
//using calcDiscount function:
//3 fries, element 3's contents in discount array
//price of 'Fries' from menuItemsArray as param 2
echo"
3 Fries: \$$itemTotal";
//accumulator
$discountedTotalScriptSide=$discountedTotalScriptSide+$itemTotal;
$itemTotal=round(CalcDiscountStandalone(2,$menuItemsArray['Soda']),2);
//using calcDiscount function:
//2 fries, element 2's contents in discount array
//price of 'Fries' from menuItemsArray as param 2
echo"
2 Sodas: \$$itemTotal";
//accumulator
$discountedTotalScriptSide=$discountedTotalScriptSide+$itemTotal;
$itemTotal=round(CalcDiscountStandalone(2,$menuItemsArray['Shake']),2);
//using calcDiscount function:
////2 shakes, element 2's contents in discount array
//price of 'Shake' from menuItemsArray as param 2
echo"
2 Shakes: \$$itemTotal";
//accumulator
$discountedTotalScriptSide=$discountedTotalScriptSide+$itemTotal;
echo"Total Order Price: \$$discountedTotalScriptSide";
echo"
----------------------------
Thank you!!!
";
Price for 1:
Hamburger: $1.99
Cheeseburger: $2.49
Fries: $0.99
Soda: $1.29
Shake: $1.79
Price for 5:
Hamburger: $7.96
Cheeseburger: $9.96
Fries: $3.96
Soda: $5.16
Shake: $7.16
Total Order Price: $23.2
----------------------------
Thank you!!!
//associative array
$kimbraArray=array("Kimbra1"=>"Lightyears", "Kimbra2"=>"Human",
"Kimbra3"=>"Past Love", "Kimbra4"=>"Right Direction",
"Kimbra5"=>"Everybody Knows", "Kimbra6"=>"The Good War",
"Kimbra7"=>"Top of the World", "Kimbra8"=>"Real Life",
"Kimbra9"=>"Recovery", "Kimbra10"=>"Like They Do on the TV",
"The Beatles"=>"The White Album");
echo'Artists and Albums
';
foreach($kimbraArray as $artist=>$album){
echo $artist . ": " . $album . "
";
}
echo "
";
$download = "true";
if($download=="true"){
echo "$title $downloadTitle";
$price="9.99";
$quantity=1;
while ($quantity < 7){
$price = $price * $quantity;
echo "$quantity for $price
";
$quantity = $quantity + 1;
$price="9.99";
}
}
$download = "false";
if($download=="false"){
echo $title . $cdTitle;
$price = "12.99";
for($quantity=1; $quantity < 7; $quantity++){
$price = $price * $quantity;
$total = $price + $shipping;
echo "$quantity for $total
";
$price="12.99";
}
}
Cost by Quantity - Downloads
1 for 9.99Cost by Quantity- CDs | Shipping is $2.99 per order
1 for 15.98
$productsArray=
array(
"t-shirt"=>array("single"=>9.99,"10 or more"=>8.99,"25 or more"=>7.99,"50 or more"=>6.99,"100 or more"=>5.99),
"ballcap"=>array("single"=>11.99,"10 or more"=>10.99,"25 or more"=>9.99,"50 or more"=>8.99,"100 or more"=>7.99),
"visor"=>array("single"=>8.99,"10 or more"=>8.49,"25 or more"=>7.99,"50 or more"=>7.49,"100 or more"=>6.99),
"magnet"=>array("single"=>4.50,"10 or more"=>4.25,"25 or more"=>4.00,"50 or more"=>3.50,"100 or more"=>3.00),
"coffee mug"=>array("single"=>6.50,"10 or more"=>6.00,"25 or more"=>5.50,"50 or more"=>5.00,"100 or more"=>4.50),
);
//show price for 25 or more visors
echo"Price for 25 or more visors: \$" . $productsArray['visor']['25 or more']."
";
//show single prices for each item
echo"Single Prices:";
echo " Loop through all rows of the products array, print 'single' price only";
echo "";
foreach($productsArray as $item=>$pricesArray){
echo"
" . $item .":\$" . $pricesArray['single'];
}
echo "
";
echo "Ballcap Prices:
";
echo " Loop through one row of the products array";
echo "";
foreach($productsArray['ballcap'] as $qty=>$price){
echo"
" . $qty . ": \$" . $price;
}
echo "";
$maxPrice = 7.50;
echo"Items available for less than \$$maxPrice:
";
echo " Loop through all rows of the products array and loop through each element of the nested arrays and test against a condition";
echo "";
foreach($productsArray as $item=>$prices){
foreach($prices as $qty=>$discPrice){
if($discPrice < $maxPrice){
echo"
$item - $qty: \$$discPrice";
}
}
}
Price for 25 or more visors: $7.99
Single Prices: Loop through all rows of the products array, print 'single' price only
t-shirt:$9.99
ballcap:$11.99
visor:$8.99
magnet:$4.5
coffee mug:$6.5
Ballcap Prices: Loop through one row of the products array
single: $11.99
10 or more: $10.99
25 or more: $9.99
50 or more: $8.99
100 or more: $7.99
Items available for less than $7.5: Loop through all rows of the products array and loop through each element of the nested arrays and test against a condition
t-shirt - 50 or more: $6.99
t-shirt - 100 or more: $5.99
visor - 50 or more: $7.49
visor - 100 or more: $6.99
magnet - single: $4.5
magnet - 10 or more: $4.25
magnet - 25 or more: $4
magnet - 50 or more: $3.5
magnet - 100 or more: $3
coffee mug - single: $6.5
coffee mug - 10 or more: $6
coffee mug - 25 or more: $5.5
coffee mug - 50 or more: $5
coffee mug - 100 or more: $4.5
//multidimensional array
$multiArray =
array(
"The Beatles"=>array("A Hard Day's Night"=>1964,"Help!"=>1965,
"Rubber Soul"=>1965,"Abbey Road"=>1969),
"Led Zeppelin"=>array("Led Zeppelin"=>1971),
"Rolling Stones"=>array("Let it Bleed"=>1969, "Sticky Fingers"=>1971),
"The Who"=>array("Tommy"=>1969, "Quadrophenia"=>1973,
"The Who by the Numbers"=>1975),
);
echo "Tommy by The Who was released in ".
$multiArray['The Who']['Tommy']. ".";
echo "
";
echo"All Album info:
";
foreach($multiArray as $artist=>$album){
echo "";
echo " $artist:
";
foreach($album as $title=>$year){
echo "$title, $year
";
}
}
echo"
The Who Album Release Dates :
";
foreach($multiArray as $artist=>$album){
if($artist=="The Who"){
foreach($album as $title=>$year){
echo "$year $title
";
}
}
}
echo"
Albums released after 1970 :
";
foreach($multiArray as $artist=>$album){
foreach($album as $title=>$year){
if($year > 1970){
echo "$artist- $title, $year
";
}
}
}
Tommy by The Who was released in 1969.
The Beatles:
A Hard Day's Night, 1964
Help!, 1965
Rubber Soul, 1965
Abbey Road, 1969
Led Zeppelin:
Led Zeppelin, 1971
Rolling Stones:
Let it Bleed, 1969
Sticky Fingers, 1971
The Who:
Tommy, 1969
Quadrophenia, 1973
The Who by the Numbers, 1975
Also check out: Devops overview →