Welcome! Log In Create A New Profile

Advanced

Functions - Just Can't Get It Right

Posted by 38701162 - Dumi 
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
Functions - Just Can't Get It Right
August 19, 2013 09:56PM
Hi!

I'm just confused, I want to create that function that accepts 5 arguments but if I put something between brackets it gives me an error of undefined variables but the same code runs correctly with nothing between brackets. Below is the layout of my code:
Language: PHP
display($movie, $time, $drink, $ticket, $category); function display($movie, $time, $drink, $ticket, $category)   display(); function display()
First one doesn't run but second one does, any suggestions?
avatar
Mac
Re: Functions - Just Can't Get It Right
August 20, 2013 07:52AM
undefined variable is a notice, not an error, which you can turn off (wrong way), or define it before you use it (correct route) by using isset

Language: PHP
// this will generate the error notice $movie=$_POST[';movie';]; echo "$movie"; //or use $movie     // this is the correct way if(isset($_POST[';movie';])){ $movie=$_POST[';movie';]; } echo "$movie"; //or use $movie
Re: Functions - Just Can't Get It Right
August 20, 2013 02:38PM
Thanks Mac, let me try it.
Re: Functions - Just Can't Get It Right
August 21, 2013 06:45AM
Morning!
I still have a challenge, I still get error message saying "call to undefined function" here is my code:
Language: PHP
if(isset($_REQUEST[';movie';])) { foreach ($_REQUEST[';movie';] as $yourmovie) { display($yourmovie); function display($yourmovie) { echo "Movie : ". $yourmovie; } } }
Please help, I have changed my code so much that I'm confused
Re: Functions - Just Can't Get It Right
August 21, 2013 06:48AM
I should add that if I use only the foreach function, it runs with no problem but if I put function in or out of the foreach, I get the error.
Re: Functions - Just Can't Get It Right
August 21, 2013 07:21AM
Hi Dumi

(FYI...this is a new question and should actually be a new thread)

I think the first time your loop runs you are calling a function display but it has not been created yet...
move the display function out of the loop and out of the if statement...
it wont get executed until you call it...
so try this.


Language: PHP
function display($yourmovie) { echo "Movie : ". $yourmovie; }     if(isset($_REQUEST[';movie';])) { foreach ($_REQUEST[';movie';] as $yourmovie) { display($yourmovie); } }

hope that works
avatar
Mac
Re: Functions - Just Can't Get It Right
August 21, 2013 08:40AM
Good answer. A function is a "standalone", defined elsewhere. Having the function inside a loop is like defining it over and over. That said, you can just as well use the echo line on its own.
Re: Functions - Just Can't Get It Right
August 21, 2013 10:11PM
Thanks a lot it worked.
Sorry, only registered users may post in this forum.

Click here to login