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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.