09-10-2006, 07:30 PM
|
#1 (permalink)
|
|
Moderator Of The Year
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
|
Number Factoring PHP Script HELP!!!
Im trying to create a script that will Factor a Number,
here is what i have so far but i keep getting this error unexpected T_INC, expecting ')' on Line 5
PHP Code:
<?php
$num = '14' ;
$counter = '1';
function factor($num , $counter++ ){
"$num / $counter++";
}
Factor();
?>
Can any one point me in the right direction with finishing this script???
I think im moving in the right direction.
~Snapple
|
|
|
09-11-2006, 02:58 AM
|
#2 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 29
|
Re: Number Factoring PHP Script HELP!!!
PHP Code:
<?php
$num = '14' ;
$counter = '1';
function factor($num, $counter = '1')
{
++$counter;
if ( ($num % $counter == 0) && ($num > $counter) )
{
echo $num.' can be divided by '.$counter.'<br>';
factor($num, $counter);
}
elseif( ($num % $counter != 0) && ($num > $counter) )
{
factor($num, $counter);
}
else
{
echo $num.' can be divided by '.$num.'<br><br>';
}
}
factor($num, $counter);
factor(17);
factor(38,5);
factor(198,1);
?>
Last edited by moriakaice : 09-11-2006 at 03:14 AM.
|
|
|
09-11-2006, 05:15 AM
|
#3 (permalink)
|
|
Junior Member
Join Date: Sep 2006
Location: Sector ZZ9 Plural Z Alpha
Posts: 34
|
Re: Number Factoring PHP Script HELP!!!
Aside from the functional errors moriakaice, pointed out, a simple explanation of the error you're getting:
PHP Code:
function factor($num , $counter++ )
is not allowed.
In the same way that
PHP Code:
function factor($num , $counter=$someothervar)
wouldn't be allowed.
default values for function parameters need to be constant, they will NOT be evaluated at runtime.
Have a look at the PHP manual entry on function arguments for more details.
A little addition to moriakaice's script: I'd add a check after each increment operation to make sure $counter isn't zero. Calling the function with factor($num, -1); will terminate the script in a quick and dirty way...
__________________
"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams
|
|
|
09-11-2006, 09:31 AM
|
#4 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 29
|
Re: Number Factoring PHP Script HELP!!!
Yup, we can add:
PHP Code:
if ($counter < 2)
{
$counter = 2;
}
$counter = floor($counter);
after ++counter;
Of course, you can put the floor into some checking, so it won't be executed each time; it's up to you. Same with $counter checking - if you'll be factorising numbers by yourself, you won't provide it with incorrect arguments, however, when making it public, Itsacon is right, be careful about varibles.
|
|
|
09-11-2006, 09:11 PM
|
#5 (permalink)
|
|
Junior Member
Join Date: Sep 2006
Posts: 38
|
Re: Number Factoring PHP Script HELP!!!
i seem to be missing something. where is the factored number going to go? the script seems to do the job, then dead end? don't you want it to echo the output somewhere?
|
|
|
10-01-2006, 06:53 PM
|
#6 (permalink)
|
|
Moderator Of The Year
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
|
Re: Number Factoring PHP Script HELP!!!
Sorry for being so late to the game, yeah i need it to echo the results, Also i dont want the numbers to duplicate so im going to play with this. I tend to learn better by disecting the code to see what does what. now here is another question.
PHP Code:
<?php
Function factor (This is the Function)
{
But what does the second set of brackets imply???}
?>
|
|
|
10-02-2006, 01:24 AM
|
#7 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 29
|
Re: Number Factoring PHP Script HELP!!!
PHP Code:
<?php
function ThisIsFunctionName ($thisIsFunctionStartingParameter1, $thisIsFunctionStartingParameter2)
{ //here the function instructions start
//here the function does what it does
} // here the function instructions end
In my function, the numbers were not duplicated, so I don't see reason to edit anything.
|
|
|
10-02-2006, 01:16 PM
|
#8 (permalink)
|
|
Moderator Of The Year
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
|
Re: Number Factoring PHP Script HELP!!!
Mori,
Thanks now can the Paramaters be blank???
like...
PHP Code:
<?php
Function factor()
{ //yadda yadda yadda
//Yadda yadda yadda
}
?
|
|
|
10-03-2006, 01:16 AM
|
#9 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 29
|
Re: Number Factoring PHP Script HELP!!!
Yup, no problems.
It can be
PHP Code:
<?php
function OurFunction1 ()
{
echo 'Working';
}
function OurFunction2($text = 'Default value of TEXT if not provided')
{
echo $text;
}
function OurFunction3 ($text1, $text2 = 'Default again')
{
echo $text1.'<br>'.$text2;
}
function Break2Lines ()
{
echo '<br><br>';
}
OurFunction1();
Break2Lines();
OurFunction2();
Break2Lines();
OurFunction2('Break, I need break...');
Break2Lines();
OurFunction3('Smexy');
Break2Lines();
OurFunction3('Smexy', '... Not really');
?>
Try these 
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00
|
|
|
|
|