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. Link to comment https://forums.phpfreaks.com/topic/30794-function-help/ 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. Link to comment https://forums.phpfreaks.com/topic/30794-function-help/#findComment-141951 Share on other sites More sharing options...
marcus Posted December 15, 2006 Author Share Posted December 15, 2006 So $num * ($percent/100) ? Link to comment https://forums.phpfreaks.com/topic/30794-function-help/#findComment-141952 Share on other sites More sharing options...
marcus Posted December 15, 2006 Author Share Posted December 15, 2006 Thanks, it works :D Link to comment https://forums.phpfreaks.com/topic/30794-function-help/#findComment-141953 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; Link to comment https://forums.phpfreaks.com/topic/30794-function-help/#findComment-141954 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 Link to comment https://forums.phpfreaks.com/topic/30794-function-help/#findComment-141955 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); Link to comment https://forums.phpfreaks.com/topic/30794-function-help/#findComment-141975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.