0

I have the following code that suppose to change the background color after I enter the RGB color codes and click submit.

I don't know for what reason I get the "Undefined variable" and have a black background color before I click the submit button

< ?php

        error_reporting(E_ALL); 
        ini_set('display_errors',true);



$form = "< form method='post' action=$_SERVER[PHP_SELF] >\n

        R: < input type='text' name='r' >
        G: < input type='text' name='g' >
        B: < input type='text' name='b' > 
        < input type='submit' name='buton' value='go' >\n";
        < /form >

$hexa = array();
$culoareHexa = array();

function &decimal2hexa($valoare) {

    $valoriHexa = array('0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9', '10'=>'A', '11'=>'B', '12'=>'C', '13'=>'D', '14'=>'E', '15'=>'F' );

    if ($valoare <= 15) {
        $numarHexa[] = $valoare;
        $numarHexa[] = 0;
    } else {
        while ($valoare >= 15) {
            $catul = $valoare / 16;
            settype($catul, 'int');
            $restul = $valoare % 16; 
            $valoare = $catul; 
            $numarHexa[] = $restul;
        }
        $numarHexa[] = $catul;
    }

    krsort($numarHexa);

    foreach ($numarHexa as $key => $value) {
        if ($value > 9) {
            $numarHexa[$key] = $valoriHexa[$value];
        }
    }

    $numarHexa = array_values($numarHexa);  //reindexez si pastrez valorile pe pozitia initiala
    return $numarHexa;

}


if ($_SERVER['REQUEST_METHOD'] === 'GET') {

    echo $form;
} else {

    if (!isset($_POST['r']) || !is_numeric($_POST['r']) || ($_POST['r'] > 255) || ($_POST['r'] < 0) || 
        !isset($_POST['g']) || !is_numeric($_POST['g']) || ($_POST['g'] > 255) || ($_POST['g'] < 0) ||
        !isset($_POST['b']) || !is_numeric($_POST['b']) || ($_POST['b'] > 255) || ($_POST['b'] < 0)) {
            echo "date invalide!";
            echo $form;
    } else { 
        $culoareHexaR =& decimal2hexa($_POST['r']);
        $culoareHexaG =& decimal2hexa($_POST['g']);
        $culoareHexaB =& decimal2hexa($_POST['b']);
        var_dump($_POST);
        var_dump($culoareHexaR);
        var_dump($culoareHexaG);
        var_dump($culoareHexaB);
        $culoareHexa = array_merge($culoareHexaR, $culoareHexaG, $culoareHexaB);
        var_dump($culoareHexa);
        $culoareHexaString = "";
        for ($i = 0; $i < count($culoareHexa); $i++) {
            $culoareHexaString .= $culoareHexa[$i]; 
        }
        echo $culoareHexaString;
    }
}

? >

< html >

    < body bgcolor="< ?php echo $culoareHexaString ? >"> 

    < /body >
< /html >

If I declare the $culoareHexaString outside the if statement, it works just fine but I do not understand why.

in the following example it is not necessary to declade the $c variable outside the if statement.

$a = 5; 
    $b = 6; 

    if ($a > $b) {
        echo "this will not be print";
    } else {
        $c = $a+$b;
    }

$c variable will have a value of: < ?php echo $c ? >

what I am missing?

thanks!

1
  • 1
    Because $culoareHexaString isn't initialized and it enters the if statement so $culoareHexaString in the else statement never get's declared! In your example it enters the else statement and that's why it's defined (BTW: I hope you don't have a space here < ?php) Commented Jan 28, 2015 at 19:27

2 Answers 2

1

Here:

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
   ...
} else {
   // code not executed on GET/initial page view
}

You initialize $culoareHexaString in a block that is never executed, because the first view/non-submit is a GET request, and thuse the else condition is ignored.

Try initializing a default value outside that block, like:

$coloareHexaString = '#000000'; // default value?
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
   ...
} else {
   // code not executed on GET/initial page view
}

As for your example, echoing $c would also be undefined if $a < $b, as it was never initialized.

<?php

$a = 7;
$b = 6;

if ($a > $b) {
    echo "this will not be print";
} else {
    $c = $a+$b;
}

echo $c; // this will be undefined.

?>
Sign up to request clarification or add additional context in comments.

Comments

0

Because $culoareHexaString is not setted. When you use if-else statement, actually there will be code blocks and at the run time according to the statement related block content is handling. for detecting value setting, use isset() method.

Also you can set default color at the beginning like;

    < ?php
                error_reporting(E_ALL); 
                ini_set('display_errors',true);
       $culoareHexaString = "#000000";
       $form = "< form method='post' action=$_SERVER[PHP_SELF] >\n

                R: < input type='text' name='r' >
                G: < input type='text' name='g' >
                B: < input type='text' name='b' > 
                < input type='submit' name='buton' value='go' >\n";
                < /form >

        $hexa = array();
        $culoareHexa = array();

.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.