PHP Functions
Results 1 to 9 of 9

Thread: PHP Functions

  1. #1
    Junior Member
    Join Date
    Feb 2017
    Posts
    3

    Lightbulb PHP Functions

    Hello,

    I am new, just learning PHP. I would like appreciate if someone could assist me with the following two questions:

    (1) I will like to write a function named whatever, that contains one required and one default parameter. The function multiplies the two values.

    what(50); // This function call must return the value 450
    what(12,12)


    Below are my codes but not working. P lease kindly assist me how to achieve the above requirement. Where I mine getting it wrong:

    Code:
    function what($w, $u){
    $value =($w*$u);
    return $value;
    }
    $returnValue = what(50, 9);
    //echo $returnValue;
    
    function what($what, $num=12*12){
    	echo $what;
    }
    (2)I want to a function named changing that make use of a variable-length parameter list. I want to use the function to produce the following output:

    Function received 5 arguments
    Argument 0 = H
    Argument 1 = e
    Argument 2 = l
    Argument 3 = l
    Argument 4 = o

    Function received 8 arguments
    Argument 0 = F
    Argument 1 = e
    Argument 2 = e
    Argument 3 = -
    Argument 4 = F
    Argument 5 = i
    Argument 6 = -
    Argument 7 = F
    Argument 8 = 0

    Thanks for your assistance.

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,882
    I don't want to do your homework for you, but I will guide you a bit: a function parameter is made optional by assigning it a default value in the function declaration:
    PHP Code:
    function foo($required$optional=null) { /* ... */ }

    function 
    foo($required$optional=1) { /* ... */ 
    Note that all required parameters must be defined before any optional parameters.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,522
    Quote Originally Posted by lezyd View Post
    (1) I will like to write a function named whatever, that contains one required and one default parameter. The function multiplies the two values.

    what(50); // This function call must return the value 450
    what(12,12)


    Below are my codes but not working. P lease kindly assist me how to achieve the above requirement. Where I mine getting it wrong:

    PHP Code:
    function what($w$u){
    $value =($w*$u);
    return 
    $value;
    }
    $returnValue what(509);
    //echo $returnValue;

    function what($what$num=12*12){
        echo 
    $what;

    Welcome to PHPBuilder

    Let's see if I understand your requirements for #1.

    Your function will have one required factorial argument, and one optional factorial argument, and if the optional factorial argument is not given, the second factor of the multiplication will be "9"? If so ...

    PHP Code:
    //Redacted, as NogDog believes you're asking for homework help.  Perhaps you should introduce yourself more completely. 
    Last edited by dalecosp; 03-02-2017 at 11:14 AM.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,882
    And here's a tip for part of the second problem (converting a number to a character): http://php.net/manual/en/language.ty....string.substr
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,522
    Quote Originally Posted by lezyd View Post
    Function received 8 arguments
    Argument 0 = F
    Argument 1 = e
    Argument 2 = e
    Argument 3 = -
    Argument 4 = F
    Argument 5 = i
    Argument 6 = -
    Argument 7 = F
    Argument 8 = 0

    Thanks for your assistance.
    Incidentally, that's 9 arguments.

    And as far as that goes, another RTF(riendly!)M link: http://php.net/manual/en/functions.a...iable-arg-list
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Junior Member
    Join Date
    Feb 2017
    Posts
    3
    HTML Code:
    Thank you for your reply.
    
    I have sorted it out now.
    
    My problem before was that, when I called the function, I got FATAL ERROR: Cannot redeclare what(previously declared)
    
    Below are my codes and I got the values I was looking for: Please correct me if wrong:
    
    
    	function what($number1, $number2=NULL){
    
    	$R=$number1*$number2;
    
    	return R;
    
    	}
    
    	$c=what(50,9)// I want this function call to return 450
    
    	$d=what(12,12)
    
    	echo '$c<br/>';
    
    	echo '$d<br/>';
    
    
    Secondly, please,would kindly give me clue or guildeline to produce following output, using variable-length parameter;
    Quote Originally Posted by lezyd View Post
    Hello,

    I am new, just learning PHP. I would like appreciate if someone could assist me with the following two questions:

    (1) I will like to write a function named whatever, that contains one required and one default parameter. The function multiplies the two values.

    what(50); // This function call must return the value 450
    what(12,12)


    Below are my codes but not working. P lease kindly assist me how to achieve the above requirement. Where I mine getting it wrong:

    Code:
    function what($w, $u){
    $value =($w*$u);
    return $value;
    }
    $returnValue = what(50, 9);
    //echo $returnValue;
    
    function what($what, $num=12*12){
    	echo $what;
    }
    (2)I want to a function named changing that make use of a variable-length parameter list. I want to use the function to produce the following output:

    Function received 5 arguments
    Argument 0 = H
    Argument 1 = e
    Argument 2 = l
    Argument 3 = l
    Argument 4 = o

    Function received 8 arguments
    Argument 0 = F
    Argument 1 = e
    Argument 2 = e
    Argument 3 = -
    Argument 4 = F
    Argument 5 = i
    Argument 6 = -
    Argument 7 = F
    Argument 8 = 0

    Thanks for your assistance.

  7. #7
    Junior Member
    Join Date
    Feb 2017
    Posts
    3
    Thank you for your reply.

    I have sorted it out now.

    My problem before was that, when I called the function, I got FATAL ERROR: Cannot redeclare whatever(previously declared)

    Below are my codes and I got the values I was looking for: Please correct me if wrong:

    1
    function what($number1, $number2=NULL){
    2
    $R=$number1*$number2;
    3
    return R;
    4
    }
    5
    $c=what(50,9)// I want this function call to return 450
    6
    $d=what(12,12)
    7
    echo '$c<br/>';
    8
    echo '$d<br/>';


    Secondly, please,would kindly give me clue or guildeline to produce following output, using variable-length parameter;

  8. #8
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,882
    At its simplest:
    PHP Code:
    function what($number1$number2=1)
    {
        return 
    $number1 $number2;

    To use it:
    PHP Code:
    <p>The product of 50 and 9 is <?php echo what(509); ?>.</p>
    The thing I'll emphasize here is keeping functions narrowly focused to a specific purpose. Therefore, this function is only concerned with multiplying two numbers; it does not do any kind of output. The output in this case is left to the code that calls the function (and that code could be in another function).
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  9. #9
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,522
    In hope of enlightenment, I'll put up what I redacted earlier:

    PHP Code:
    function multiply($a$b=null) { //$b is an optional argument

        
    if (isset($b)) {  //if $b is present we return the product of $a and $b
            
    return $a $b;
        }
        return 
    $a 9;  //otherwise we return the product of $a and "nine".
    }

    //Usage would be as mentioned by NogDog:

    echo multiply(50); //450

    echo "<br>\n";  

    echo 
    multiply(1212); 
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •