Jump to content

Function


nemiux

Recommended Posts

Hi, i need to learn function with $_post, but here what happens:

page one:

<HTML>

<HEAD></HEAD>

<title> Funkcija </title>

<BODY bgcolor="black" text="white">

<form type="post" action="/funkcija.php">

Lets look, does this number can be divided from 2. <br>

Your number: <input type="text" name="a"> <br>

<input type="submit" value="Lets check!">

</BODY>

</HTML>

Everything was ok here, result is simple, you should know, but now the problem

<HTML>

<HEAD></HEAD>

<title> Rezultatas </title>

<BODY bgcolor="black" text="white">

<h3 align="center"> <B> Your result</B> </h3>

<BR>

<BR>

<?php 

$a = $_post [a];

  function lygu ($a)

{

if ($a % 2 == 0)

  { echo "Number can be divided";

   

  }

else

{

echo "Number cannot be divided";

}

echo lygu ($a);

}

?>

 

 

<a href="/index.php"> try again??

<br>

</BODY>

</HTML>

 

 

When i click submit in first page, this is my result:

"Your result

 

 

try again??"

 

 

 

P.S. I must use funtion at this, if something's wrong, please rewrite command and make bold/italic text where it has changed. Thank you very much.

 

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/144643-function/
Share on other sites

You are calling your function within the scope of the function itself and doubling up on your echo. Check your braces carefully. Should be:

 

<?php 

function lygu ($a) { 
   if ($a % 2 == 0) { 
      echo "Number can be divided"; 
   } 
   else { 
      echo "Number cannot be divided"; 
   } 
} 

$a = $_POST['a'];
lygu ($a);
?> 

Link to comment
https://forums.phpfreaks.com/topic/144643-function/#findComment-759003
Share on other sites

Here's the problem this time:

Everything works, but it doesn't matter do i enter 2, 3, 5, it still writes number CAN be divided

What is the problem?

 

Everything can be divided as long as it is not trying to be divided by 0. You mean to say, is the passed in number divisible by 2?

 

<?php 

function lygu ($a) { 
   if (($a % 2) == 0) {  // parans here around the first portion
      echo "Number is divisible by two."; 
   } 
   else { 
      echo "Number is not divisible by two."; 
   } 
} 

$a = $_POST['a'];
lygu ($a);
?> 

Link to comment
https://forums.phpfreaks.com/topic/144643-function/#findComment-759016
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.