 # PHP excercise calculateTip

exercise:
Write a function calculateTip() which takes a number representing the total cost of a meal as its first argument. It should also take a second, optional argument—an integer representing the percent tip desired (eg. 25 will indicate a 25% tip should be calculated). If no second argument is passed in, the function should default to a 20% tip. The function should return the new total—the previous total plus the calculated tip.

``````function calculateTip (\$tip = "100*(1+(25/100))")
{
echo "\$tip";
}
echo calculateTip (100,25);
echo calculateTip (100);
echo calculateTip (65,15);
``````

I am beginner and doing exercise to calculate tip as above, is someone could help to correct? as I could not get the hint suggested by exercise as well. thank you

I’m also struggling with this particular exercise and the hints.

They say you should be passing in two arguments, a cost(\$cost) and then an optional value for tip(\$tip). If no value for tip is passed into the function there is a default value. I think they show you how to set an optional argument in a previous exercise.

You should also do your calculations inside the function. Using \$cost and \$tip to calculate a final \$final_cost. Then return that final cost.

I hope this helps

1 Like

I am also a noobie and at first got stuck doing this exercise.
First of all, make it 2 parameters in the function calculateTip (\$cost,\$tip=20)
Than make a new \$total in the body of the function and finally return it.
Finish with twice echo of the function with different integer arguments.
What I got is:

<?php
function calculateTip(\$total_cost,\$tip = 20)
{
\$new_total = (\$total_cost * \$tip) / 100 + \$total_cost;
return \$new_total;
}
echo calculateTip(100);
echo calculateTip(100,50);

I hope that helps.

1 Like