marcus Posted December 15, 2006 Share Posted December 15, 2006 i just started trying to learn basic function work[code]<?phpfunction percent($num,$percent){ if(isset($num) && isset($percent)){ $math = $num * 0.$percent; echo "$percent% of $num is $math"; }else { echo "A number and percent must be valid!"; }}percent(10,30);?>[/code]I get:[code]Parse error: parse error, unexpected T_VARIABLE in C:\Documents and Settings\Owner\Desktop\xampp\htdocs\testing\1a\i2.php on line 7[/code]I have tried double quotes but it just echoed it off. Quote Link to comment Share on other sites More sharing options...
Orio Posted December 15, 2006 Share Posted December 15, 2006 You cant do 0.$percent.You can do $percent/100 instead :)Orio. Quote Link to comment Share on other sites More sharing options...
marcus Posted December 15, 2006 Author Share Posted December 15, 2006 So $num * ($percent/100) ? Quote Link to comment Share on other sites More sharing options...
marcus Posted December 15, 2006 Author Share Posted December 15, 2006 Thanks, it works :D Quote Link to comment Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 you cannot use 0.$percent, the script is thinking it's a string.use $percent = $percent / 100; Quote Link to comment Share on other sites More sharing options...
craygo Posted December 15, 2006 Share Posted December 15, 2006 I would imagine that line 7 is[code]$math = $num * 0.$percent;[/code]You can't have a period in the middle. PHP thinks you are appending something to your variablesdo this[code]<?phpfunction percent($num,$percent){ if(is_int($num) && is_int($percent)){ $div = $percent/100; $math = $num * $div; echo "$percent% of $num is $math"; }else { echo "A number and percent must be valid!"; }}percent(10,30);?>[/code]Also i changed your check. Af unction will return an error if the variables are not set so no need to double check. What you want to check for is if the variables are integers.Ray Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 15, 2006 Share Posted December 15, 2006 Although I think it is very bad format, you can get it to work similar to how you first had it.$math = $num * ("0.".$pct); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.