TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-20-2008, 02:58 AM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default whats a good way to write this function

what im trying to do is, if i were to type the function as br(); without any arguments i want the output to display my own message instead of php's error being

Warning: Missing argument 1 for br()


<?php

function br($num)
{
$num = 0;
if ($num == null) { die('br function must contain a value');
return;
}

else { echo "<br>"; }
return;
}


br();
?>
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 01-20-2008, 03:45 AM   #2 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

i think im getting somewhere :p but the die function keeps showing and the br wont work.

Code:
<?php

function br() { 
$num = func_num_args;
if ($num > 0 ) 
{

 echo "<br>";
 
  }

 
else { die('too many arguments specified or no arguments entered'); } 

}

br(5); 
?>
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 01-20-2008, 04:34 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

PHP Code:
function br($num null)
{
    if (
$num == null) {
        echo 
'br function must contain a value';
    }
    else {
        echo 
"<br>";
    }

Do you have any idea at all what you are doing? If yes, what are you TRYING to do?
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-20-2008, 10:39 AM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Sarmenhb,

There are a couple of ways to achieve what you want:

The first is to provide your function with a default value in the event that one isn't provided - see Xenon's example. In this case, he has set the default value to NULL, then checks against that in the function.

The second way is to use func_num_args() as you tried:

PHP Code:
function br()
{
    
$numargs func_num_args();
    
    if (
$numargs == 0)
    {
        die(
'You must provide a number!');
    }
    else
    {
        
$num func_get_args(0);    // Gets the first argument
        
echo "You provided $num !";
    }

This method also allows you to accept an unknown number of arguments if your function needs it.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-20-2008, 03:49 PM   #5 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Change <br> to <br />.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:05 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design