Jump to content

what is the best way to write statesment in the function ?


Recommended Posts

The best way is to use them when needed, you can over function a program. But really there is no "standard" for writing functions. It is more or less to get rid of redundant code, so if say you need to constantly grab data for different ID's you can make a grabData(ID) function which grabs the data for a specific ID, etc.

 

But if you have specific ideas, why not share what you are thinking and attempt to write one for what you are thinking and ask for that to be critiqued.

Yeah basically small snippets of reusable code are what functions are. If you see something you think can be used again somewhere else in your script, make it a function. I'd say have a minimum of 3 executable lines of code as a minimum. I've seen people use functions to shorten a function name, and while it's handy, it's hardly needed really

examples

function pr($expression, $return = false) {
    return print_r($expression, $return);
}

function mres($data) {
    return mysql_real_escape_string($data);
}

They're kinda handy for the users that don't have a good PHP ide but for those that do it's pointless with intelli code popups, snippet shortcuts and auto replaces

function mres($data) {
    return mysql_real_escape_string($data);
}

 

Your mres() is really just an alias the way you put it, albeit it does look better to type a few hundred times than the former. A  good example if they need to run multiple checks such as simple validation.

Thank you so much friends . I have seen lot's of builtin php function has optional arguments. How do we write optional arguments ?

 

It's generally a good idea to read up on the manual, as they provide samples and useful user contributed comments which are relevant to many problems.

 

Here is a very simple example:

function myFunction ($display, $bold=false) {
      if ($bold == true) {
         echo "<b>" . $display . "</b>";
      } else { 
         echo $display;
      }
}

myFunction ('This is a string! ');
myFunction ('This is a bold string' , true);

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.