mertcek
mertcek

Reputation: 11

PHP function not working

<?php
$name = $_POST['name'];
$namecheck  = namecheck($name);

    function namecheck($name){

            if(strlen($name)>0)
            {
                return TRUE;
            }
            else if(strlen($name)==0)
            {
                return FALSE;
            }
        };

    function control($namecheck1) 
    {
    if ($namecheck == TRUE) 
    {
        echo "It is TRUE"; 
    } 
    else if ($namecheck == FALSE) 
    {
        echo "It is FALSE";
    }
    };
?>

I wrote that there is no problem in HTML part, there is a problem in my php functions because I am new in PHP. Can you make it proper.

I think you will understand what I want to do in the functions its simple Im trying to do if it is true I want to see "it is TRUE" in the screen. Else .....

Upvotes: 1

Views: 10823

Answers (7)

vitthal
vitthal

Reputation: 11

This works fine

<?php
$name = $_REQUEST['name'];

    function namecheck($name1)
    {

            if(!empty($name1))
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }


    if (namecheck($name) == TRUE) 
    {
        echo "It is TRUE"; 
    } 
    else if (namecheck($name) == FALSE) 
    {
        echo "It is FALSE";
    }

?>

Upvotes: 0

Vytautas
Vytautas

Reputation: 3539

Some tips:

  • instead namecheck() you can use empty()
  • before using $_POST['name'] you should check if it exists isset() should help

Upvotes: 1

Vinnyq12
Vinnyq12

Reputation: 1559

your are referencing $namecheck in the function "control" but the parameter passed is named $namecheck1. $namecheck in the scope of function "control" is undefined.

Upvotes: 1

Haru
Haru

Reputation: 1381

It looks as if you are not calling control function.

Upvotes: 1

MDEV
MDEV

Reputation: 10838

In your control function, the parameter is called $namecheck1 at first, but you only call it $namecheck when you try to use it inside the function.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

Take a look at the variables. They don't match:

function control($namecheck1) 
    {
    if ($namecheck == TRUE) 

You also never actually invoke that function.

Upvotes: 6

Nanne
Nanne

Reputation: 64399

You're not calling your 'control' function. try starting with

$name = $_POST['name'];
$namecheck  = namecheck($name);
control($namecheck);

Also, your definition of you function is wrong (or the variable you use is). You can change the function to this

 function control($namecheck) 

Of the if's to

if ($namecheck1 == TRUE) 

in the end the name after control( is the one you should check for in the if's

Upvotes: 2

Related Questions