Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Ternary Conditionals Question

Options
  • 02-07-2009 3:00pm
    #1
    Closed Accounts Posts: 3,762 ✭✭✭


    Im just writing a script where one GET variable, 'id', is mandatory. If this is not passed the script issues an error message through xml and dies.

    In the interest of "good coding practise" I want to use a ternary operator. So this is what I have:
    function xmlPrintError($errorMessage, $errorCode=false)
    {
    	header("content-type: application/xml");
    	echo('<?xml version="1.0"?><xmlresult><result>error</result><errorMessage>'.$errorMessage.'</errorMessage>');
    	if($errorCode!=false)
    	{
    		echo('<errorCode>'.$errorCode.'</errorCode>');
    	}
    	echo('</xmlresult>');
    	die();
    }
    
    $bookId =  isset($_GET['id']) ? $_GET['id'] : xmlPrintError('No book specified',5.11);
    

    In the given ternary conditional, is it incorrect to have the false condition call echo and die constructs?

    Or to phrase it another way, should all conditions of the ternary conditional return only data to then apply to the variable?


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I would say yes, it makes your code harder to read if you kill it in a ternary operator.

    Generally what I do is set the default value of the variable. If the variable is mandatory, you set a default value which shouldn't exist. So in your case above, "bookId" is the Id of a row in a table. So this ID must be a postive integer. So set your default value to -1 and test whether bookId has a positive numeric value and kill your script if not.

    You could also use zero, but I tend to avoid that because of instances where zero may be a valid positive integer...


  • Closed Accounts Posts: 3,762 ✭✭✭turgon


    I just did another one that is in a for loop, where the negative condition was "continue" and I got a "Parse error: syntax error, unexpected T_CONTINUE."

    So I think your way is fundamentally better, albeit a little longer.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    I agree with seamus, I think when assigining a value to a variable you are better off just doing that, it's much easier to read. Once assigned I'd then do some sanity checks on the variable.

    Slightly off topic, don't let the simplicicty of these two functions put you off using them - writing those ternary functions is a pain! I always have these two in a global include file
    function _get($key, $default=null){
    return isset($_GET[$key]) ? $_GET[$key] : $default;
    }
    
    function _post($key, $default=null){
    return isset($_POST[$key]) ? $_POST[$key] : $default;
    }
    


Advertisement