Jump to content

PHP Parenthesis? - totally new at this


hollyosburn
Go to solution Solved by ginerjm,

Recommended Posts

My website is down with a fatal error.  I know just the tiniest bit about coding, and found the error log.  I know the line that is causing a fatal error, but don't know where to put parenthesis to fix.  If someone could help me with this, I would be very grateful.

This is the error:

PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`

Here is the line the log says has the problem:

 $coronavirus_notice_banner_color = (@$_POST['action']=='save_coronavirus_notice_plugin') ? $coronavirus_notice_banner_color : (get_option('coronavirus_notice_banner_color')) ? esc_html(get_option('coronavirus_notice_banner_color')) : '#CC0000';

There are a few lines just like the one above that probably have the same issue, but if I can get some help on the one above, I think I can follow the pattern and fix the others.

Thank you in advance!

 

Link to comment
Share on other sites

I believe it is telling you that the complex statement that someone wrote there is no longer supported (if it ever was).  Putting 2 of these comparisons together is silly coding in my book just to supposedly save some coding space or whatever.  But it is difficult to re-interpret when you come back to it 3 months later.  It also seems to be very incorrect.  (Are you sure you copied it correctly?)

To explain.  The first that has the POST in it is a test against a string value.  It is true then you are returning the value of the 'banner color' to the result var, which IS that same value.  If it is not true then you are doing another (similar) test of the result of a call to getoption which if it returns a true value will return the value of a call to the esc_html function but if it is not true will simply return a color value (#cc0000).

To put it in a different way:

if ($_POST['action'] == 'save_corona_virus_notice_plugin')
	$coronavirus_notice_banner_color = $coronavirus_notice_banner_color;
elseif(get_option('coronavirus_notice_banner_color'))
		$coronavirus_notice_banner_color = esc_html(get_option('coronavirus_notice_banner_color'));
	else
		$coronavirus_notice_banner_color = '#CC0000';

I don't see a parens problem here.  But I see some highly unnecessarily complex variable names.  Way too much typing.

Link to comment
Share on other sites

4 minutes ago, ginerjm said:

I believe it is telling you that the complex statement that someone wrote there is no longer supported (if it ever was).  Putting 2 of these comparisons together is silly coding in my book just to supposedly save some coding space or whatever.  But it is difficult to re-interpret when you come back to it 3 months later.  It also seems to be very incorrect.  (Are you sure you copied it correctly?)

To explain.  The first that has the POST in it is a test against a string value.  It is true then you are returning the value of the 'banner color' to the result var, which IS that same value.  If it is not true then you are doing another (similar) test of the result of a call to getoption which if it returns a true value will return the value of a call to the esc_html function but if it is not true will simply return a color value (#cc0000).

To put it in a different way:

if ($_POST['action'] == 'save_corona_virus_notice_plugin')
	$coronavirus_notice_banner_color = $coronavirus_notice_banner_color;
elseif(get_option('coronavirus_notice_banner_color'))
		$coronavirus_notice_banner_color = esc_html(get_option('coronavirus_notice_banner_color'));
	else
		$coronavirus_notice_banner_color = '#CC0000';

I don't see a parens problem here.  But I see some highly unnecessarily complex variable names.  Way too much typing.

Thank you so much for responding.  Here is more of the code.  I think I copied it correctly?  What can I do to get around the fatal error?  Is there a way to fix the parens so that it matches the  Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` requirement that the error message says is the problem?

image.png.b9b708c8d04a3b9fee94a2b9dc72f253.png

image.png

Link to comment
Share on other sites

  • Solution

As I said first thing -- the style of this code is not allowed at all.  It has to be re-written.  These new lines are not the same as the one you have the error on.  They do not follow the same format as the bad one.

I have no idea what the app is supposed to do for you but it is written rather tediously and I would have definitely found another way to do this that the mind-numbing way that the original code chose.  I'm guessing that he/she too was a noob when writing it.

Try this:

 $coronavirus_notice_banner_color =
	@$_POST['action']=='save_coronavirus_notice_plugin' ? 
		$coronavirus_notice_banner_color : 
	(get_option('coronavirus_notice_banner_color') ? esc_html(get_option('coronavirus_notice_banner_color')) : 
		'#CC0000');

And I really don't like the use of the @ in this code.  It is not a good practice.

Edited by ginerjm
  • Like 1
Link to comment
Share on other sites

The line that I have the error on is line 155.  I appreciate any help you can give me.  This is my wordpress website. 

 

Thank you for the updated code.  It seems to work!  The website is still a mess, but at least it's back up.

Edited by hollyosburn
changed to accept above code as solution!
Link to comment
Share on other sites

Now study the difference in the original 'bad' line and the new line I gave you.  Compare that to the syntax that the error message showed you and note how the parens have to be placed.   Did you just move a parens on that line, causing this issue?

Link to comment
Share on other sites

Not to mention WP's get_option() function allows a default in it's signature so the entire convoluted second ternary isn't necessary.

$coronavirus_notice_banner_color = (!empty($_POST['action']) && $_POST['action'] == 'save_coronavirus_notice_plugin') ? $coronavirus_notice_banner_color : esc_html(get_option('coronavirus_banner_color', '#cc0000'));

Either way, did you recently update your version of PHP or is this a recent plugin addition to your site? The way it's set up isn't supported anymore - PHP used to do it's best to figure out import in nested ternary operations, but it doesn't now. It needs to be explicit by way of parenthesis.

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.