Welcome! Log In Create A New Profile

Advanced

PHP8 - Create your own Functions

Posted by Gaia77490614 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
PHP8 - Create your own Functions
February 15, 2012 04:06PM
I have completed this section.
Loving this course.
avatar Re: PHP8 - Create your own Functions
February 16, 2012 10:22PM
creating function helps lessen the codes to write. Section completed.smileys with beer
Re: PHP8 - Create your own Functions
February 20, 2012 02:53PM
Awesome gaia

function me($name) {
echo $name;
}

$name = "PHP GURU";
array_map("me", $name);
Re: PHP8 - Create your own Functions
February 22, 2012 08:04PM
Completed this section!
Re: PHP8 - Create your own Functions
February 23, 2012 04:05PM
I've completed this section.

Here is note for everyone, and is very good pratice and a good habit to include in your functions.
You can assign a default value when passing values in and out of a function...i.e.

Language: PHP
function paint($color) { echo "The color of the room is " . $color; }   paint("blue");

This will give a result of:
The color of the room is blue

Let's say you for some reason a value is not passed in, you can assign a default value, or a fall back value like this:

Language: PHP
function paint($color="red") { echo "The color of the room is " . $color; }   paint();

This will give a result of:
The color of the room is red

But if you did pass in a value like this paint("blue"winking smiley like in the first example it will say:
The color of the room is blue

It's nothing major, but it makes your function more error-free and is good practice if your codes get complex.
Re: PHP8 - Create your own Functions
February 24, 2012 10:54AM
Nice one 77471466_Alwyn

Im currently using that functionality but ive added one more thing which removes the white spaces inbetween a word.

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);

Cheers
Re: PHP8 - Create your own Functions
February 25, 2012 04:07PM
Done and understood
avatar Re: PHP8 - Create your own Functions
February 25, 2012 09:29PM
Completed.

I found it useful to discover that a variable declared outside a function is not available to that function.

Also, regarding this section of code (in 'Returning a Value from a PHP Function'winking smiley:
Language: PHP
$discount_total = $total_spent - ($total_spent * $discount); $total_charged = $discount_total;
It seems like the author 'wasted' a variable or named his/her variables strangely. Why not just write:
Language: PHP
$total_charged = $total_spent - ($total_spent * $discount);
or perhaps it would make more sense for clarity to have written:
Language: PHP
$discount_total = $total_spent * $discount; $total_charged = $total_spent - $discount_total;

It seems like a small change, but I think this kind of thing is important, because a year later when it comes to fixing something, it should make as much sense as possible.

Any thoughts?
Re: PHP8 - Create your own Functions
February 27, 2012 12:07AM
I finished this section. What you mentioned mm77509382 about the line wasted is something I didn't think about before. But now that you brought it up it does seem strange to make the new variable for $discount_total. I am new to php, so I might be wrong, but could it just to keep the $discount_total separate from the $total_charged? So that when trouble shooting later the coder can see that it is actually a discount that is being worked out. That's sort of what I thought could be the reason.
Re: PHP8 - Create your own Functions
March 01, 2012 02:13PM
I've completed this chapter.
Re: PHP8 - Create your own Functions
March 03, 2012 12:10PM
owned this section
Re: PHP8 - Create your own Functions
March 06, 2012 09:20AM
I'm trying to create a function to check whether the three input fields are all integer.
My code is as follows:

Language: PHP
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"; } }

O my knowledge this should work...but it doesn't.
I don't get any errors, but doesn't matter what my input is (numbers or text) I get the "Sorry please type in numbers" message.
avatar
Mac
Re: PHP8 - Create your own Functions
March 06, 2012 09:46AM
How are you using the function within your larger code? You are not passing any variables to the testNum function.

And what does the testVal do?



Language: PHP
function 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
My aim is to write code where the user types in a date, and my php returns how long ago that date is compared to the current date.
Everything is working correctly, until now that I wanted to add coded to make sure the user puts in numeric values.
Here is all my code:

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>
avatar Re: PHP8 - Create your own Functions
March 07, 2012 07:20AM
@Alwyn

You could start by trying is_numeric() rather than is_int(). Form input always comes in as a string rather than an int, even if it is a typed number.

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().
http://php.net/manual/en/function.is-int.php
avatar
Mac
Re: PHP8 - Create your own Functions
March 07, 2012 07:57AM
Once again, it is how you use the functions

Language: PHP
$day = $_POST[';day';]; $month = $_POST[';month';]; $year = $_POST[';year';]; //then pass it to the function testNum($day, $month, $year)
Re: PHP8 - Create your own Functions
March 07, 2012 08:14AM
Thanks I'll try that and see if it works

mm77509382 Wrote:
-------------------------------------------------------
> @Alwyn
>
> You could start by trying is_numeric() rather than
> is_int(). Form input always comes in as a string
> rather than an int, even if it is a typed number.
>
>
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().
>
>
http://php.net/manual/en/function.is-int.php

thank you, this solved the problem instantly
avatar Re: PHP8 - Create your own Functions
March 07, 2012 09:04AM
@Alwyn

I'm glad it helped. But do hear what mac is saying please. When you write a function to test something, that something should be passed as an argument.

Something along these lines:
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 */ } } ?>

Then you call the function like this:

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); ?>

Otherwise you have functions that rely on external variables being there (e.g. $day). This would make the code harder to read and harder to reuse at the end of the day. Ideally you should be able to reuse the function again in another (perhaps entirely different) project without having to declare variables elsewhere on the page.

In this case, I would encourage you to check each input separately and instead of using text inputs, to use either a custom date picker or the <select> element in HTML. That way there's a far higher chance that the input will come through as you expect.

Something like this, I think:
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
mm77509382 Wrote:
-------------------------------------------------------
> @Alwyn
>
>
Thanks a lot man, would try it definitely!
I avoided it a bit, as I'm not 100% comfortable with passing on variables yet...but will practice grinning smiley
avatar
Mac
Re: PHP8 - Create your own Functions
March 08, 2012 07:15AM
A function requires you to send it information. If you do not send it the information (paramaters in the () )then it will not work.
Re: PHP8 - Create your own Functions
March 08, 2012 11:45PM
I have completed this part on functions, good section!
Re: PHP8 - Create your own Functions
March 09, 2012 12:06AM
Simple and easy use of PHP Include function to have only one header and one footer file for your website. If you need to change an image, a link, or even a typo, if you are using regular HTML files you have to edit each one individually, this way is better:

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
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';); ?>

In the code above we split up our “contact.php” file into two section, this way we can place the contact information for John right in the middle of our contact.php file.
Re: PHP8 - Create your own Functions
March 09, 2012 09:50AM
Done itgrinning smiley
Re: PHP8 - Create your own Functions
March 11, 2012 04:45PM
This section was really interesting.
Reminds me of my days in Turbo Pascal :O
Re: PHP8 - Create your own Functions
March 12, 2012 01:37PM
Done
Re: PHP8 - Create your own Functions
March 12, 2012 02:15PM
All done with this sectionsmile
Re: PHP8 - Create your own Functions
March 12, 2012 06:44PM
Completed the section. Useful stuff.
Re: PHP8 - Create your own Functions
March 14, 2012 11:32AM
Create function during runtime.

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
Just completed this section.
Sorry, you do not have permission to post/reply in this forum.