Announcements | Last Post | |
---|---|---|
SoC Curricula | 09/30/2017 01:08PM | |
Demarcation or scoping of examinations and assessment | 02/13/2017 07:59AM | |
School of Computing Short Learning Programmes | 11/24/2014 08:37AM | |
Unisa contact information | 07/28/2011 01:28PM |
PHP8 - Create your own Functions February 15, 2012 04:06PM |
Registered: 12 years ago Posts: 31 Rating: 0 |
Re: PHP8 - Create your own Functions February 16, 2012 10:22PM |
Registered: 12 years ago Posts: 116 Rating: 0 |
Re: PHP8 - Create your own Functions February 20, 2012 02:53PM |
Registered: 12 years ago Posts: 77 Rating: 0 |
Re: PHP8 - Create your own Functions February 22, 2012 08:04PM |
Registered: 12 years ago Posts: 15 Rating: 0 |
Re: PHP8 - Create your own Functions February 23, 2012 04:05PM |
Registered: 12 years ago Posts: 96 Rating: 0 |
Language: PHPfunction paint($color) { echo "The color of the room is " . $color; } paint("blue");
Language: PHPfunction paint($color="red") { echo "The color of the room is " . $color; } paint();
Re: PHP8 - Create your own Functions February 24, 2012 10:54AM |
Registered: 12 years ago Posts: 77 Rating: 0 |
Language: PHP/* * Remove all the whitespaces */ $color = "blue"; function remove_whitespace($string) { $l = 0; $output = ';';; for ($i = 0, $n = strlen($string); $i < $n; $i++) { $num = substr($string, $i, 1); if ($num == '; ';) { $l++; } else { $output .= $num; } return $output; } } function paint($color="red") { echo "The color of the room is " . $color; } $color = remove_whitespace($color); paint(color);
Re: PHP8 - Create your own Functions February 25, 2012 04:07PM |
Registered: 12 years ago Posts: 115 Rating: 0 |
Re: PHP8 - Create your own Functions February 25, 2012 09:29PM |
Registered: 12 years ago Posts: 31 Rating: 0 |
It seems like the author 'wasted' a variable or named his/her variables strangely. Why not just write:Language: PHP$discount_total = $total_spent - ($total_spent * $discount); $total_charged = $discount_total;
or perhaps it would make more sense for clarity to have written:Language: PHP$total_charged = $total_spent - ($total_spent * $discount);
Language: PHP$discount_total = $total_spent * $discount; $total_charged = $total_spent - $discount_total;
Re: PHP8 - Create your own Functions February 27, 2012 12:07AM |
Registered: 12 years ago Posts: 31 Rating: 0 |
Re: PHP8 - Create your own Functions March 01, 2012 02:13PM |
Registered: 13 years ago Posts: 159 Rating: 0 |
Re: PHP8 - Create your own Functions March 03, 2012 12:10PM |
Registered: 12 years ago Posts: 45 Rating: 0 |
Re: PHP8 - Create your own Functions March 06, 2012 09:20AM |
Registered: 12 years ago Posts: 96 Rating: 0 |
Language: PHPfunction testNum() { $day = $_POST[';day';]; $month = $_POST[';month';]; $year = $_POST[';year';]; if (is_int($day) && is_int($month) && is_int($year)) { testVal(); } else { echo "Sorry please type in numbers"; } }
Re: PHP8 - Create your own Functions March 06, 2012 09:46AM |
Admin Registered: 18 years ago Posts: 10,001 Rating: 353 |
Language: PHPfunction testNum() { if (is_int($day) && is_int($month) && is_int($year)) { testVal(); } else { echo "Sorry please type in numbers"; } $day = $_POST[';day';]; $month = $_POST[';month';]; $year = $_POST[';year';]; testNum($day.... etc)
Re: PHP8 - Create your own Functions March 06, 2012 10:22AM |
Registered: 12 years ago Posts: 96 Rating: 0 |
Language: PHP<?php $day = ""; $month = ""; $year = ""; $currentDate = getdate(); function getAnswer() { $day = $_POST[';day';]; $month = $_POST[';month';]; $year = $_POST[';year';]; $currentDate = getdate(); if ($year == $currentDate[';year';]) { if ($month == $currentDate[';mon';]) { $dayAnswer = $currentDate[';mday';] - $day; $dayDate = abs($dayAnswer); echo "Posted " . $dayDate . " Days Ago"; } else { $monthAnswer = $month - $currentDate[';mon';]; $monthDate = abs($monthAnswer); echo "Posted " . $monthDate . " Months Ago"; } } else { echo "Posted more than a year ago"; } } function testVal() { $day = $_POST[';day';]; $month = $_POST[';month';]; $year = $_POST[';year';]; $currentDate = getdate(); if ($year > $currentDate[';year';]) { echo "invalid year, please try again"; } elseif ($month > $currentDate[';mon';]) { echo "invalid month, please try again"; } elseif ($day > $currentDate[';mday';]) { echo "invalid day, please try again"; } else { getAnswer(); } } function testNum() { $day = $_POST[';day';]; $month = $_POST[';month';]; $year = $_POST[';year';]; if (is_int($day) && is_int($month) && is_int($year)) { testVal(); } else { echo "Sorry please type in numbers"; } } if (isset($_POST[';Submit1';])) { testNum(); } ?> <br /> Date of last post:<br /> <FORM NAME ="form1" METHOD ="POST" ACTION ="Dates.php"> Day: <INPUT TYPE = "TEXT" VALUE ="<?php echo $day; ?>" name="day"> Month: <INPUT TYPE = "TEXT" VALUE ="<?php echo $month; ?>" name="month"> Year: <INPUT TYPE = "TEXT" VALUE ="<?php echo $year; ?>" name="year"> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"> </FORM>
Re: PHP8 - Create your own Functions March 07, 2012 07:20AM |
Registered: 12 years ago Posts: 31 Rating: 0 |
http://php.net/manual/en/function.is-int.phpQuote
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
Re: PHP8 - Create your own Functions March 07, 2012 07:57AM |
Admin Registered: 18 years ago Posts: 10,001 Rating: 353 |
Re: PHP8 - Create your own Functions March 07, 2012 08:14AM |
Registered: 12 years ago Posts: 96 Rating: 0 |
>Quote
To test if a variable is a number or a
> numeric string (such as form input, which is
> always a string), you must use
> is_numeric().
Re: PHP8 - Create your own Functions March 07, 2012 09:04AM |
Registered: 12 years ago Posts: 31 Rating: 0 |
Language: PHP<?php // function relies on arguments passed to it, not on potentially arbitrary external vars function testDate($year_val, $month_val, $day_val){ if( is_numeric($year_val) && is_numeric($month_val) && is_numeric($day_val) ){ /* insert code */ } } ?>
Language: PHP<?php // values from the form submission $year_value = $_POST[';year';]; $month_value = $_POST[';month';]; $day_value = $_POST[';day';]; //pass those values to the function testDate($year_value, $month_value, $day_value); ?>
Language: HTML<select name="year"> <option>--Choose a year---</option> <option value="2012">2012</option> <option value="2011">2011</option> <option value="2010">2010</option> ... </select>
Re: PHP8 - Create your own Functions March 07, 2012 09:25AM |
Registered: 12 years ago Posts: 96 Rating: 0 |
Re: PHP8 - Create your own Functions March 08, 2012 07:15AM |
Admin Registered: 18 years ago Posts: 10,001 Rating: 353 |
Re: PHP8 - Create your own Functions March 08, 2012 11:45PM |
Registered: 12 years ago Posts: 73 Rating: 0 |
Re: PHP8 - Create your own Functions March 09, 2012 12:06AM |
Registered: 12 years ago Posts: 73 Rating: 0 |
Language: PHP<?php $page = $_GET[';page';]; include(';includes/header.php';); if($page==""){ include(';includes/home.php';); } elseif ($page=="contact"){ include(';includes/contact.php';); } include(';includes/footer.php';); ?>
Re: PHP8 - Create your own Functions March 09, 2012 12:09AM |
Registered: 12 years ago Posts: 73 Rating: 0 |
Language: PHP<?php $page = $_GET[';page';]; $person = $_GET[';person';]; include(';includes/header.php';); if($page==""){ include(';includes/home.php';); } elseif ($page=="contact"){ include(';includes/contactheader.php';); if ($person=="john"){ include(';includes/contacts/john.php';); } include(';includes/contactfooter.php';); } include(';includes/footer.php';); ?>
Re: PHP8 - Create your own Functions March 09, 2012 09:50AM |
Registered: 12 years ago Posts: 11 Rating: 0 |
Re: PHP8 - Create your own Functions March 11, 2012 04:45PM |
Registered: 12 years ago Posts: 12 Rating: 0 |
Re: PHP8 - Create your own Functions March 12, 2012 01:37PM |
Registered: 12 years ago Posts: 15 Rating: 0 |
Re: PHP8 - Create your own Functions March 12, 2012 02:15PM |
Registered: 12 years ago Posts: 31 Rating: 0 |
Re: PHP8 - Create your own Functions March 12, 2012 06:44PM |
Registered: 12 years ago Posts: 11 Rating: 0 |
Re: PHP8 - Create your own Functions March 14, 2012 11:32AM |
Registered: 12 years ago Posts: 77 Rating: 0 |
Language: PHP$newfunc = create_function(';$a,$b';, ';return "($a + $b)"';); echo $newfunc(10, 20);
Re: PHP8 - Create your own Functions March 15, 2012 03:10PM |
Registered: 12 years ago Posts: 45 Rating: 0 |