JamesKoash Posted August 28, 2013 Share Posted August 28, 2013 Hi there, At present, I'm using the variables $nameerror and $_POST['name'], in the middle of string concatenation, but this is throwing up errors on the occasions where these variables are undefined. Here is my code at present: <input type=\"text\" name=\"name\" MAXLENGTH=\"50\" value=\"". htmlentities($_POST['name']) ."\" />" . $nameerror . I now want to change this code to only display $nameerror and $_POST['name'] if they have been set and are not empty. I've searched around online and it seems that IF statements can't be used in the middle of string concatenation, so conditional statements using ? and : are recommended instead. However, every time I try conditional statements, PHP seems to throw up some sort of error. I've tried all sorts of combinations, including: $nameerror ? $nameerror : FALSE $nameerror ? $nameerror : "" isset($nameerror) ? $nameerror : FALSE isset($nameerror) ? $nameerror : "" empty($nameerror) ? FALSE : $nameerror empty($nameerror) ? "" : $nameerror None of these work. Some throw up error messages. Other stop the page from fully loading, meaning elements are missing. Can someone advise me on what I'm doing wrong here as I've been wracking my head for hours and still can't work it out. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281625-conditional-statements-in-middle-of-string-concatenation/ Share on other sites More sharing options...
Solution mac_gyver Posted August 28, 2013 Solution Share Posted August 28, 2013 use the following, with the () around it, so that it is treated as an expression and it won't try to use parts of the concatenated strings - (isset($nameerror) ? $nameerror : "") Quote Link to comment https://forums.phpfreaks.com/topic/281625-conditional-statements-in-middle-of-string-concatenation/#findComment-1447097 Share on other sites More sharing options...
JamesKoash Posted August 28, 2013 Author Share Posted August 28, 2013 Thanks, that's fixed it Quote Link to comment https://forums.phpfreaks.com/topic/281625-conditional-statements-in-middle-of-string-concatenation/#findComment-1447135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.