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

<?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); 
} 
?> 

 

try this...

You access a element inside an array by putting a key into a string...

Link to comment
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
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
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.