Martha Raup 10/24/09 CIS 52a
Problem 1:
<?php$variables = "variables";
$myText = <myHereDoc
<pre>
This is some PHP text.
It is completely free.
I can use "double quotes"
and 'single quotes',
plus $variables too, which will
be properly converted to their values.
<b>Even <span style="color: red">HTML tags</span> work!</b>
And I can write myHereDoc, as long as it
is not alone on a line, like this:</pre>
myHereDoc;
echo $myText;
?>
*************************************************************
Problem 2:
<?php
function printBR($txt) {
echo $txt."<br />";
}
$myString = "Hi, welcome to my page!";
$myBoolean = true;
$myDouble = 4.234;
$myInteger = 3;
$myNull;
printBR("My string is: ".$myString.", and it's type is: ".gettype($myString));
printBR("My Boolean is: ".$myBoolean.", and it's type is: ".gettype($myBoolean));
printBR("My double is: ".$myDouble.", and it's type is: ".gettype($myDouble));
printBR("My integer is: ".$myInteger.", and it's type is: ".gettype($myInteger));
printBR("My null is: ".$nyNull.", and it's type is: ".gettype($myNull));
?>
**************************************************************
Problem 3:
<?php
$fixedRateMortgage = 5.03;
$homeEquity = 8.49;
$autoLoan = 7.33;
$cdInterest = 1.28;
echo "30 year fixed rate mortgage loan: ".$fixedRateMortgage."%. This figure is of the '".gettype($fixedRateMortgage)."' data type.<br />";
echo "30 year home equity loan: ".$homeEquity."%. This figure is of the '".gettype($homeEquity)."' data type.<br />";
echo "48 month new auto loan: ".$autoLoan."%. This figure is of the '".gettype($autoLoan)."' data type.<br />";
echo "6 month CD interest rate: ".$cdInterest."%. This figure is of the '".gettype($cdInterest)."' data type.<br />";
echo (strtoupper("source:"))." <a href=\"http://www.bankrate.com/\">BankRate.com</a>, as of 10/15/09";
?>
*************************************************************
Problem 4:
<?php
$testValue = $_POST['inputValue'];
if (!isset($_POST['submit'])) {
?>
<p>Enter a value here:<br />
<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post" />
<input type="text" size="12" maxlength="20" name="inputValue" />
<br />
<input type="submit" value="Submit" name="submit" />
</form<br />
Press the "Submit" button to evaluate the input using the PHP conditional (ternary) operator.
</p>
<?php
} elseif (($testValue !==0) & ($testValue !=="")) {
echo "Hello. Welcome to Martha's test of the ternary operator."."<br />";
echo "The value you submitted was: ".$testValue.".<br />";
echo (is_numeric($testValue) ? "The value entered is a number." :
(is_double($testValue)) & ((round($testValue)%2)==0)) ? "The number '".round($testValue)."' (rounded) is even." : "The value you entered is NOT a number!";
}
?>
**************************************************************
Problem 5:
<?php
$fTemp = 0;
while ($fTemp <= 100) {
$cTemp = ($fTemp - 32) * .55;
echo $fTemp." Fahrenheit is equal to ".$cTemp." Celsius<br />";
$fTemp++;
}
?>
*************************************************************
Problem 6:
<?php
for ($fTemp = 0; $fTemp <= 100; $fTemp++) {
$cTemp = ($fTemp - 32)*.55;
echo $fTemp." Fahrenheit is equal to ".$cTemp." Celsius.<br />";
}
?>
**************************************************************
Problem 7:
<?php
$test1 = $_POST['grade1'];
$test2 = $_POST['grade2'];
$test3 = $_POST['grade3'];
$total = $test1 + $test2 + $test3;
$average = ($total / 3);
if (!isset($_POST['submit'])) {
?>
<form action="<?php echo $PHP_SELF;?>" method="post">
<p>Grade test #1
<input type="text" size="4px" maxlength="3px" name="grade1" />
Grade test #2
<input type="text" size="4px" maxlength="3px" name="grade2" />
Grade test #3
<input type="text" size="4px" maxlength="3px" name="grade3" />
<input type="submit" value="Submit" name="submit" />
</p>
<p>Please enter your most recent scores on tests, using numbers between 0 and 100.
Then, press the Submit button to calculate your grade.</p>
</form>
<?php
} else {
echo (!is_numeric($test1)) ? "Please enter a legitimate grade score." : "You entered ".$test1.", "
.$test2.", and ".$test3.".<br >The total grade point was: ".$total.", and the average grade point was: "
.round($average).".<br />";
if (($average >90) & ($average < 100)) {
echo "<h1>Congratulations! You've got an 'A'!</h1>";
} elseif (($average>80) & ($average < 89)) {
echo "<h2>You got a B - Keep up the good work!</h2>";
} elseif (($average > 70) & ($average < 79)) {
echo "<h3>You got a C, which is just 'average'. Surely you can do better!</h3>";
} elseif (($average > 60) & ($average < 69)) {
echo "<h4>Whoops - you got a D. Get with the program!</h4>";
} elseif (($average > 0) & ($average < 59)) {
echo "<h5 style='color:red'>Too bad - you got an F, and ".strtoupper("You are flunking!!")
."</h5>";
} else {
echo "You did not follow directions!";
}
}
?>
*************************************************************
Problem 8:
<?php
function createTable() {
// Set up variables
$cols=0;
$rows=0;
$personCount=1;
$randEven=0;
$randOdd=0;
$randTotal=0;
$randEvenTotal=0;
$randOddTotal=0;
$grandTotal=0;
$randEvenAve=0;
$randOddAve=0;
$grandTotalAve=0;
// create outer table
echo "<table style=\"border: 1px solid black; background-color:#99F\" cellpadding=\"2\" cellspacing=\"4\">";
echo "<tr style=\"text-align:center\"<td colspan=\"4\">Random Heads and Tails</td</tr>";
echo "<tr style=\"background-color:white;\"<td>Persons<td>Evens</td<td>Odds</td<td>RAND TOTAL</td</tr>";
for ($rows=1; $rows<=10;$rows++) {
echo "<tr style='color: white; text-align: center;'> \n";
echo "<td>Person ".$personCount."</td<td>".$randEven."</td<td>".$randOdd."</td<td>".$randTotal."</td>";
echo "</tr>";
$personCount++;
}
echo "<tr style='color: red; text-align: center;' <td>TOTALS</td<td>".$randEvenTotal."</td<td>".$randOddTotal."</td<td>".$grandTotal."</td</tr>";
echo "<tr style='color: black; font-weight: bold; text-align: center;'>";
echo "<td>AVERAGES</td<td>".$randEvenAve."</td<td>".$randOddAve."</td<td>".$grandTotalAve."</td</tr>";
?>
</table>
<?php
}
createTable();
// an attempt at "mathematical" constructs
function doRandomCoinToss() {
$random = rand(100, 150); // Thanks to Henri
if ($random%2==0) {
$randEvenCount++;
} else {
$randOddCount++;
$randOddTotal++;
}
/* ROTTEN CODE! Illegal to have a boolean??
function getRandomCount() {
$randEvenCount = 0;
$randOddCount = 0;
doRandomCoinToss();
if ($randEvenCount < 2) & ($randOddCount < 2) {
continue;
} else break;
while ($randEvenCount < 2) {
$randEvenCount++;
}
while $randOddCount < 2) {
$randOddCount++;
}
}
*/
function getRandTotal() {
for ($personCount=1; $personCount <= 10; $personCount++) {
$randTotal = $randEventCount + $randOddCount;
return $randTotal;
}
}
function totalRandCounts() {
getRandTotal();
for ($personCount=1; $personCount <= 10; $personCount++) {
$grandTotal += $randTotal;
return $grandTotal;
}
}
function getAverages() {
$averageEven = (randEvenCount / 10);
$averageOdd = (randEvenCount / 10);
$averageRandTotal = ($grandTotal / 10);
$randEvenAve = $averageEven;
$randOddAve = $averageOdd;
$randTotalAve = $averageRandTotal;
}
}
?>
**************************************************************
Problem 9:
<form name="favoriteCity" action="lab1_9-switch.php" method="post">
<fieldset>
<legend>Choose Your Favorite City</legend>
<input name='selectedCity' type='radio' value='sf' /> San Francisco<br />
<input name='selectedCity' type='radio' value='ny' />New York<br />
<input name='selectedCity' type='radio'value='london' />London<br />
<input name='selectedCity' type='radio' value='paris' />Paris<br />
<input name='selectedCity' type='radio' value='honolulu' />Honolulu<br />
<input name='selectedCity' type='radio' value='tokyo' />Tokyo<br />
</fieldset>
<input name="submit" type="submit" value="Submit" />
</form>
<?php
if (isset($_POST['submit'])) {
$selected_radio = $_POST['selectedCity'];
switch ($selected_radio) {
case "sf":
echo "Don't forget your umbrella!";
break;
case "ny":
echo "Enjoy the Big Apple!";
break;
case "london":
echo "Cheerio, Chap!";
break;
case "paris":
echo "Bonjour!";
break;
case "honolulu":
echo "Hang Five!";
break;
case "tokyo":
echo "Enjoy the sushi!";
break;
}
}
?>
*************************************************************
Problem 10:
<?php
function getMoney($euros) {
$euros = $_POST['euros'];
return $euros;
}
function converter($euros) {
getMoney($euros);
settype ($euros, float);
$dollars = ((149.08 * $euros)/100);
$dollars = round($dollars, 2);
return $dollars;
}
if (!isset($_POST['submit'])) {
?>
<p>Hello, Traveler! How many Euros did <?php echo strtoupper("you") ?> spend in Germany?<br />
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" />
<input type="text" size="12" maxlength="20" name="euros" /
<input type="submit" value="Convert Euros" name="submit" /</form</p>
<?php
} elseif (( $euros !== 0) || ($euros !== '')) {
echo "The exchange rate on 10/17/09: $100 is worth 67.08 Euros, and €100 is worth 149.08 dollars.<br />";
echo "The formula for converting Euros to dollars, therefore, is €1 = \$1.4908.<br /<br />";
echo "You spent €".getMoney($euros)." Euros in Germany, which is the equivalent of $".converter($_POST["euros"])." in American money. Hope you enjoyed your trip!!";
} else {
echo "Sorry, you did not complete the form properly.";
}
?>
**************************************************************
Problem 11:
<?php
$string = "This is the string to convert. Oh My!";
function converter($conversionMethod, $string) {
strtoupper($string)== "upper" ; // error message when i use "unexpected" = assignment operator. WHY????
strtolower($string) == "lower";
ucFirst($string) =="title";
ucWords($string) =="words";
}
echo "This is the string to convert: ".$string."<br />";
echo "This is the string converted to all upper capitalization: ".converter($string, "upper")."<br />";
echo "This is the string converted to all lower capitalization: ".converter( $string,"lower")."<br />";
echo "This is the string converted to a title format: ".converter($string, "title")."<br />";
echo "This is the string converted to each word capitalized: ".converter($string, "words")."<br />";
/* FIRST ATTEMPT
function titleConverter($title, $string) {
ucwords($string);
echo $string;
}
function txt2upper($upper, $string) {
echo strtoupper($string);
}
function txt2lower($lower, $string) {
echo strtolower($string);
}
function firstWordConvert($first, $string) {
echo ucfirst($string);
}
*/
?>
*************************************************************