Page 1 of 1

Why use functions? Rate Topic: ***** 3 Votes

#1 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • View blog
  • Posts: 10,792
  • Joined: 08-August 08

Posted 23 July 2011 - 06:30 AM

*
POPULAR

The manual does a good job of explaining functions and their arguments:
http://php.net/manua...e.functions.php
http://www.php.net/m...s.arguments.php
but it doesn't attempt to explain why you should use them.

Imagine if you could break your project into several smaller pieces that you could simply describe to the computer in something close to plain English. Wouldn't that make things easier? It turns out that you can, or at least very close to it. Look at this code:
<?php
$username = '';
$loggedin = retrieve_logged_in_session_status($username);
if($loggedin) {
	show_welcome_page($username);
} elseif(validate_user_input() && check_user_password()) {
	show_welcome_page($username);
} else {
	show_login_page();
}
?>

It's pretty straight forward, isn't it? First, I'm telling the computer that I want to see if the user has already logged in. If they have already logged in then I want to show them the welcome page. If not, I check to see if they've entered valid input for logging in. If they have I check to see if they've entered a valid user name and password, and if they have I show them the welcome page. Finally, if they are not logged in and they haven't given a valid user name and password I show them the login page.

At this point you might be thinking that this is too easy. Well, there is a bit more! The code above will not run as it is. It needs that plain english defined for the computer so that it knows what to do. Below I've defined a couple of the functions. You can use the links provided above to see how the functions work, then you might try writing the other two functions and getting this code to work!
function retrieve_logged_in_session_status(&$user) {
	session_start();
	if(isset($_SESSION['Username'])) {
		$user = $_SESSION['Username'];
		return true;
	}
	return false;	
}

function show_welcome_page($user) {
	echo str_replace("USERNAME", $user, file_get_contents("welcome.html"));
}



Is This A Good Question/Topic? 8
  • +

Replies To: Why use functions?

#2 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 23 July 2011 - 07:07 AM

Nice job man. You did a great job at explaining why you should use functions. I find myself using functions for a lot of things I do in my project. I create a whole file dedicated to functions.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • View blog
  • Posts: 10,792
  • Joined: 08-August 08

Posted 23 July 2011 - 08:37 AM

View PostEnvXOwner, on 23 July 2011 - 10:07 AM, said:

I create a whole file dedicated to functions.

Sounds like it might be time to start thinking about organizing those functions in to groups that apply to the same things. You might associate other attributes with those groups of functions and then call the groups classes, and instances of them would be objects. ;)
Was This Post Helpful? 3
  • +
  • -

#4 Lightme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 06-March 10

Posted 23 July 2011 - 08:44 AM

Thanks a lot for this explination, I am also going to read the 2 links you placed.

It is exactley where I am in the book PHP:In a nuttshell...
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Posted 25 July 2011 - 05:21 AM

Indeed functions are very important but they can also be overused. The more function calls the longer it's going to take to follow a script's logic especially if they are nested.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1