Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 81,169
Total Posts: 566,839
There are 50 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

How could I merge these three functions?

Thread title: How could I merge these three functions?
Reply    
    Thread tools Search this thread Display Modes  
06-04-2012, 10:09 PM
#1
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old  How could I merge these three functions?

Self explanatory.

PHP Code:
function username($user) {
    if(
$user == null)
        return 
"<del>deleted</del>";
    else
        return 
$user;
}

function 
userURL($user) {
    if(!
$user == null)
        return 
"/user/".strtolower($user);
}

function 
userlink($user) {
    if(
$user == null)
        return 
"<del>deleted</del>";
    else
        return 
"<a href=/user/".strtolower($user).">$user</a>";

I don't see the need for three different functions when I could probably merge these into one. Though I'm not particularly sure how. If someone could point me in the right direction that would be swell.

06-04-2012, 10:23 PM
#2
.Nick is offline .Nick
.Nick's Avatar
Status: Yawn
Join date: Feb 2005
Location:
Expertise: Drupal
Software: Chromium, Komodo Edit, Eclipse
 
Posts: 1,159
iTrader: 3 / 100%
 

.Nick is on a distinguished road

  Old

Originally Posted by Dan View Post
Self explanatory.
It really isn't. Where are they used / what prompted you to to create 3 of them in the first place?

Reply With Quote
06-04-2012, 10:42 PM
#3
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

It's just a bunch of if statements.

Run them through your head with: ($user = "username") || ($user = null)

I cannot create variables that can be used outside of the function from the function? Like:

function username($user) {
if($user = null)
$deleted = "deleted";
}

Then when the function is called username($username); I can echo $deleted? Is something like this at all possible? My tests show the above wouldn't work.

Reply With Quote
06-04-2012, 11:04 PM
#4
.Nick is offline .Nick
.Nick's Avatar
Status: Yawn
Join date: Feb 2005
Location:
Expertise: Drupal
Software: Chromium, Komodo Edit, Eclipse
 
Posts: 1,159
iTrader: 3 / 100%
 

.Nick is on a distinguished road

  Old

Originally Posted by Dan View Post
It's just a bunch of if statements.

Run them through your head with: ($user = "username") || ($user = null)

I cannot create variables that can be used outside of the function from the function? Like:

function username($user) {
if($user = null)
$deleted = "deleted";
}

Then when the function is called username($username); I can echo $deleted? Is something like this at all possible? My tests show the above wouldn't work.
http://php.net/manual/en/language.variables.scope.php

You could declare $deleted outside the function and then get access to it inside a function with the global keyword.

Not that I'm saying that's a good idea or anything.

06-04-2012, 11:39 PM
#5
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

I second that doing so is a terrible idea. Passing a parameter by reference would be a much better idea but still may not be good. We don't really have any context into what you are doing. In general function shouldn't be returning formatted strings.

Reply With Quote
07-30-2012, 05:05 AM
#6
unikorndesigns is offline unikorndesigns
unikorndesigns's Avatar
Status: Junior Member
Join date: May 2011
Location:
Expertise: Web Designing and Development
Software: Photoshop, Dreamweaver and etc
 
Posts: 41
iTrader: 0 / 0%
 

unikorndesigns is on a distinguished road

  Old

ok...since the purpose of this question is not exposed, the way i see it....u wanted 1function to serve the purpose all the 3 functions you mentioned top???

and if so...its simple...!

PHP Code:
function userdetails($user,$item)
{
    if(
$user == null)
        return 
"<del>deleted</del>";
    else
    {
        switch(
$item)
        {
            case 
'1':
                return 
"<a href=/user/".strtolower($user).">$user</a>";
            break;

            case 
'2':
                return 
$username;
            break;

            default:
                return 
$user;
            break;
        };
    }


$item holds the number for the cases.....so using one function with 2 parameters you can output any detail you want...and easy to add more details for accessing, just introduce another case to the switch loop...tc

HOPE I HAVE HELPED YOU!!

08-11-2012, 09:09 PM
#7
fde is offline fde
Status: I'm new around here
Join date: Aug 2012
Location: netherlands
Expertise: php
Software: xampp, cream for vim
 
Posts: 3
iTrader: 0 / 0%
 

fde is on a distinguished road

  Old

Note that it is also possible to return arrays:

PHP Code:
function a(){
$array[]="a";
$array[]="b";
return 
$array;
}


$a=a();
print_r($a); 

Reply    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed