3raser Posted April 4, 2011 Share Posted April 4, 2011 Can someone give me an actual time when functions would become handy? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/ Share on other sites More sharing options...
Philip Posted April 4, 2011 Share Posted April 4, 2011 Now. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196757 Share on other sites More sharing options...
Maq Posted April 4, 2011 Share Posted April 4, 2011 Can someone give me an actual time when functions would become handy? Thanks! Please, you can easily Google this for more reasons. Calling functions helps you not repeat code. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196758 Share on other sites More sharing options...
3raser Posted April 4, 2011 Author Share Posted April 4, 2011 EDIT: I decided to post a question about functions instead. Here is my function code: <?php function OverallGrabInfo($username) { $set_query = mysql_query("SELECT d.username, u.date, u.username FROM downloads d, users u WHERE d.username = '$username' AND u.username = '$username'") or die(mysql_error()); if(mysql_num_rows($set_query) == 0) $content_return = 'Sorry, no information was found'; else $grab = mysql_fetch_assoc($set_query); $content_return = $grab['date']; return $content_return; } ?> Here is my page code: <?php if(!$_COOKIE['user']) { $welcome = 'Welcome, guest! <a href="signup.php">Sign up now for uploading abilites!</a>'; } else { include_once('functions.php'); $welcome = 'Welcome, '. $_COOKIE['user'] .'!'. $content_return; } ?> <html> <head> <link rel="stylesheet" type="text/css" href="style/style.css" /> <title><?php echo $title; ?></title> </head> <body> <div class="logo"><a href="index.php"><img src="style/images/logo.png"></a></div> <center> <div class="background"> <div class="container"> Welcome. </div> </div> </center> </body> </html> How do I set $username to the one that will be processed in the function? Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196759 Share on other sites More sharing options...
Maq Posted April 4, 2011 Share Posted April 4, 2011 So submitting a post into a database would require a function? Why, though? I mean, more code to do what you could do on the actual posting page... Yes. You use functions all the time. mysql_query(...) is a function. Trust me, you wouldn't want to write that code every time. Native functions are used to reduce code, so are custom ones. EDIT: Because you can ask questions on here. Thus the point of this whole section? Because, unless you have a very specific question, then there are literally thousands of results for "PHP, why use functions?", and therefore, wasting our time. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196762 Share on other sites More sharing options...
.josh Posted April 4, 2011 Share Posted April 4, 2011 you don't *need* functions, but they make life easier. The point of functions is being able to reuse code. If you just have a single page doing a single thing, then there is little point in making a function out of it. But if you have many pages doing the same thing, it makes more sense to make it into a function and call the same function - have a single instance of the code instead of many instances of it. Why? For one thing, having 100 instances of the same thing is a waste of space. Also, what about having to make updates to it? If you suddenly need to make a change to the code, you would be making a change to it in 100 places instead of one. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196764 Share on other sites More sharing options...
3raser Posted April 4, 2011 Author Share Posted April 4, 2011 EDIT: I needed to make changes to the original post, like the one below, but couldn't edit. So I had to re-post, sorry for this. Here is my function code: <?php function OverallGrabInfo($username) { $set_query = mysql_query("SELECT d.username, u.date, u.username FROM downloads d, users u WHERE d.username = '$username' AND u.username = '$username'") or die(mysql_error()); if(mysql_num_rows($set_query) == 0) $content_return = 'Sorry, no information was found'; else $grab = mysql_fetch_assoc($set_query); $content_return = $grab['date']; return $content_return; } ?> Here is my page code: <?php if(!$_COOKIE['user']) { $welcome = 'Welcome, guest! <a href="signup.php">Sign up now for uploading abilites!</a>'; } else { include_once('functions.php'); $welcome = 'Welcome, '. $_COOKIE['user'] .'!'. $content_return; } ?> <html> <head> <link rel="stylesheet" type="text/css" href="style/style.css" /> <title><?php echo $title; ?></title> </head> <body> <div class="logo"><a href="index.php"><img src="style/images/logo.png"></a></div> <center> <div class="background"> <div class="container"> Welcome. </div> </div> </center> </body> </html> How do I set $username to the one that will be processed in the function? Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196770 Share on other sites More sharing options...
Maq Posted April 4, 2011 Share Posted April 4, 2011 You would do something like this: else { include_once('functions.php'); $content_return = OverallGrabInfo($_COOKIE['user']); $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return; } FYI, your IF/ELSE blocks in your function are missing braces. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196776 Share on other sites More sharing options...
3raser Posted April 4, 2011 Author Share Posted April 4, 2011 You would do something like this: else { include_once('functions.php'); $content_return = OverallGrabInfo($_COOKIE['user']); $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return; } FYI, your IF/ELSE blocks in your function are missing braces. I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196779 Share on other sites More sharing options...
Maq Posted April 4, 2011 Share Posted April 4, 2011 You would do something like this: else { include_once('functions.php'); $content_return = OverallGrabInfo($_COOKIE['user']); $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return; } FYI, your IF/ELSE blocks in your function are missing braces. I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O That only works if you want the block to only include the first line directly below it. It will throw a fatal error if you run it the way your blocks are set up. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196781 Share on other sites More sharing options...
3raser Posted April 4, 2011 Author Share Posted April 4, 2011 You would do something like this: else { include_once('functions.php'); $content_return = OverallGrabInfo($_COOKIE['user']); $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return; } FYI, your IF/ELSE blocks in your function are missing braces. I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O That only works if you want the block to only include the first line directly below it. It will throw a fatal error if you run it the way your blocks are set up. Thanks This is now solved. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196784 Share on other sites More sharing options...
Maq Posted April 4, 2011 Share Posted April 4, 2011 You would do something like this: else { include_once('functions.php'); $content_return = OverallGrabInfo($_COOKIE['user']); $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return; } FYI, your IF/ELSE blocks in your function are missing braces. I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O That only works if you want the block to only include the first line directly below it. It will throw a fatal error if you run it the way your blocks are set up. Thanks This is now solved. You could have just hit the Auto Solve button ;P Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196786 Share on other sites More sharing options...
3raser Posted April 4, 2011 Author Share Posted April 4, 2011 Heh, I wish! But yeah, one last question regarding functions (hopefully!). Here is my code below: <?php function OverallGrabInfo($username) { $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u WHERE d.username = '$username' AND u.username = '$username' LIMIT 1") or die(mysql_error()); if(mysql_num_rows($set_query) == 0) { $content_return = 'Sorry, no information was found'; } else { $grab = mysql_fetch_assoc($set_query); //login information if($grab['COUNT(d.username)'] > 0) { $welcome_return = "You have uploaded ". $grab['COUNT(d.username)'] ." files. You've registered on ". $grab['u.date'] ."!"; } else { $welcome_return = "You have uploaded 0 files. You've registered on ".$grab['date'] . "!"; } //display downloads } return $content_return; } ?> Is it possible to grab information from $set_query without having the WHERE? I used the WHERE code to grab specific information regarding the user, but is it possible to do another section of the query to display all rows in the table uploads without having the WHERE? Or will I just need to create another query? Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196838 Share on other sites More sharing options...
Maq Posted April 4, 2011 Share Posted April 4, 2011 Now, when you call this function, unless you specify the $where parameter to be "FALSE", it will include the WHERE clause. function OverallGrabInfo($username, $where=TRUE) { if($where===TRUE) { $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u WHERE d.username = '$username' AND u.username = '$username' LIMIT 1") or die(mysql_error()); } else { $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u") or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196843 Share on other sites More sharing options...
3raser Posted April 4, 2011 Author Share Posted April 4, 2011 Now, when you call this function, unless you specify the $where parameter to be "FALSE", it will include the WHERE clause. function OverallGrabInfo($username, $where=TRUE) { if($where===TRUE) { $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u WHERE d.username = '$username' AND u.username = '$username' LIMIT 1") or die(mysql_error()); } else { $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u") or die(mysql_error()); } Ah, nice! Also, if I were to make a sign-up/registration page, would creating another function for specifically that cut down on the code processing time? It's a one time code thing, though. So I'm not sure if I should create a function for it in functions.php, or just put the code on the signup.php page. Quote Link to comment https://forums.phpfreaks.com/topic/232680-question-about-functions/#findComment-1196845 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.