Jump to content

Conditional statements in middle of string concatenation


JamesKoash
Go to solution Solved by mac_gyver,

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.