Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:39:09+00:00 2026-05-27T18:39:09+00:00

I’m relatively new to PHP, and I want to ensure I code as efficiently

  • 0

I’m relatively new to PHP, and I want to ensure I code as efficiently and as correctly I can – specifically, I’d like to ask about how functions should be implemented with each other and how large and how many tasks they each should do under optimal conditions.

Example

For the sake of an example (and possibly something I will need to implement), say there is link to a page which reads “forgotten your username/password?”, upon clicking this link, they are brought to a page forgot.php, which has two forms on it:

1) If they’ve forgotten their password, they enter their username, and a link to a reset form is sent to their email address.

2) And another is if they’ve forgotten their username, they enter their email address, and this is sent to them via their email address .

Upon clicking submit, the relevant form is $_POST‘ed to a new page called forgotsubmit.php, which checks if it is being accessed legitimately through an if statement:

//loggedIn() is a previously defined function

if (!loggedIn() && isset($_POST['forgotUsername']) || isset($_POST['forgotPassword'])) {
    header('Location: index.php');
    exit();
}

Otherwise, the page will run normally, and that is: start a function(s), and display a message telling the user to check their email. How I would implement/create the function(s) necessary for this to occur.

Possibility One – 1 function

Should there be a single, large, function that is called that can handle both forgotten passwords and usernames, through an if statement inside the function, then create a query fetching the relevant fields of data from a MySQL databse, organize and then strip_tags() all the data, send an email containing the necessary information, and then echo a success/failure back depending on what form was chosen and whether the code completed without errors?

Possibility Two – 2 functions

Have an if statement on forgotsubmit.php which determines which function to run – one to fetch a username, and another to reset the password, each doing what was described in possibilty one.

Possibility Three – a handful of functions

The same as possibility two, except a separate sendMail() function could send out the email, another function could handle the MySQL queries, etc.

Does calling too many functions slow performance? Is the (if any) performance loss made up by the time cost of not having to code sending mail twice, for example?

Possibility Four – dozens of functions, no functions, something else entirely?

Could I be using something better for a task like this? Would multiple functions be appropriate? I have heard of classes, but haven’t looked into them, would they be appropriate?

Summary

I hope I have made myself concise enough. As I understand (or have read), the job of a function is to ‘make repetitive processes simple’, perhaps one of these possibilities would fulfill that task. How would you code it? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:39:10+00:00Added an answer on May 27, 2026 at 6:39 pm

    It completely depends on the task on your hand. Its always better to have some functions like do_this(), do_that() instead do_all_stuff().

    For me I follow a general rule. Each time I end up need a code block I wrote earlier, I convert it to a function. If you follow this rule, you’ll end up creating lots of re-usable functions. Also the code will automatically be refactoring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.