Carter Roeser Carter Roeser - 13 days ago 5
PHP Question

PHP - If variable is not set, get post variable

I am trying to pass through a variable to a new name if it is set, or get a POST variable if the first variable is not set.

This is my code but it is not working:

if (isset($randombg)) {
$background = $randombg;
} else {
$background = $_POST['bg'])
};


What is wrong with it, or how can I fix this?

Answer

You have typos in your code. There is a ) in $background = $_POST['bg']) and a semicolon

you this should work:

$defaultbg = '#FFFFFF';

if (isset($randombg)) {
    $background = $randombg;    
} elseif( isset( $_POST['bg'] ) ) {  
   $background = $_POST['bg'];
   }else{
       $background = $defaultbg;
       }
Comments