Hi Guys. I'm still a php newbie. In our class, we have just discussed creating PHP functions. As expected, we were given some exercises. Unfortunately I was not able to make my self-made function. There were actually two files, namely; functions.php and max_of_3.php

the 'functions.php' is where the function is stored.
the max_of_3.php will be accessing the functions.php using 'include', however, like I said earlier, it won't work. Unfortunately I can't think of any other reasons why it doesn't work. Any suggestions?

Here is the code for the functions.php

<?php

//This function accepts three numbers and returns the biggest one.
function max_of_3($x,$y,$z)
{
if(($x > $y)and($x > $z))
	{return $x;}

else if(($y > $x)and($y > $z))
	{return $y;}
else 
	{return $z;}
}
?>

Now here's the code for the max_of_3.php

<html>

<head>

</head>

<title> Max of 3 </title>

<body>
<form name='Maxof3.php' method='post'>
<body bgcolor="white">
<font size="5" face="garamond" color="red">

<center>
<table border="20">


<tr>

<td bgcolor="black"><font color="white">Enter First Number</td></font>
<td bgcolor="black"><input type = "text" name = "First" size="20"></td>
</tr>

<tr>
<td bgcolor="black"><font color="white">Enter Second Number</td></font>
<td bgcolor="black"><input type = "text" name = "Second" size="20"></td>
</tr>

<tr>
<td bgcolor="black"><font color="white">Enter Third Number</td></font>
<td bgcolor="black"><input type = "text" name = "Third" size="20"></td>
</tr>
		<tr>
			<td></td>
			<td><input type='submit' value='Submit'>
			<input type='reset' value='Reset'>
		</tr>
</table>
</form>

</body>

</html>

<?php
include ("functions.php");

$x = $_POST['First'];
$y = $_POST['Second'];
$z = $_POST['Third'];

$big = max_of_3($x,$y,$z);

echo "Biggest of the 3 is ";
echo $big;
?>

any help would be much appreciated! :)

Member Avatar for diafol

you should use max($x,$y,$z) instead of all those ifs.

Your code is working fine here. What is the result you got?

Your code is working fine here. What is the result you got?

Hi Karthik,

I'm actually using XAMPP as a server, if that matters. Now, whenever I try to run, or shall I say, submit, no output is shown.

What do you think should I do?

you should use max($x,$y,$z) instead of all those ifs.

Sir,

AS what I have said earlier, we are required to create our own function. The name of my function is max_of_3, which receives 3 parameters that will be compared. Those ifs are the body of my function.

Send an echo from functions.php just to be sure is including fine. You should also get some notices from PHP because $_POST variables aren't there until you send the form.

A question: max_of_3.php and Maxof3.php are the same file? On the form tag you send data to the second.

Send an echo from functions.php just to be sure is including fine. You should also get some notices from PHP because $_POST variables aren't there until you send the form.

A question: max_of_3.php and Maxof3.php are the same file? On the form tag you send data to the second.

Hi Cereal,

Just a little mistake, its actually "max_of_3" not "max_of_3.php" and it refers to the name of my self-made function, the Maxof3.php is where I apply the max_of_3 function.

Hey Guys,

I think it's been resolved. I can see the output now. There were actually a lot of comments on my functions.php file. I transferred the code solely for the function and it worked and displayed. Thanks anyways! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.