Jump to content

Function help


marcus

Recommended Posts

i just started trying to learn basic function work

[code]
<?php

function 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

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 variables

do this
[code]<?php
function 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.