WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#25668 closed defect (bug) (invalid)

selected, checked functions

Reported by: ciapci Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords: reporter-feedback close
Focuses: Cc:
PR Number:

Description

The selected / checked functions (from general-template.php.) are not working 100% properly. They do return the desired value when called but if you want to use them as an echo, they also get called. I tested with 2 exact strings and the non-echo version still get's returned. Basically, you get the echo and the non-echo at once.

Change History (18)

#1 @SergeyBiryukov
6 years ago

  • Component changed from Validation to General
  • Keywords reporter-feedback added
  • Severity changed from major to normal

Could you provide a piece of code to reproduce the issue?

#2 @jdgrimes
6 years ago

These functions both employ the __checked_selected_helper() function. This function returns the result regardless of whether it echo's it. I don't see why that would cause a problem though, unless you did this:

echo checked( $checked, $current, true );

That would echo the result twice, but it would be caused by using an extra unnecessary echo.

#3 @toscho
6 years ago

  • Cc info@… added
  • Keywords close added

This is not a bug, just normal PHP behavior.

#4 follow-ups: @markoheijnen
6 years ago

It's not really PHP behavior. I guess it would be nice to not return the value on echo but it isn't a big issue.

#5 in reply to: ↑ 4 @ciapci
6 years ago

Replying to markoheijnen:

It's not really PHP behavior. I guess it would be nice to not return the value on echo but it isn't a big issue.

How can I fix it and submit a patch?

#8 in reply to: ↑ 4 ; follow-up: @toscho
6 years ago

Replying to markoheijnen:

I guess it would be nice to not return the value on echo but it isn't a big issue.

Why? I don’t see a bug or even a problem here. If you need the return value as a string, you can get it. And if you want to print it out, that works too.

We don’t even know if a change would break existing code. Let’s say someone is using the current behavior to reuse the return value:

$check = checked( $checked, $current, true );
// create the next form field then:
echo $check;

Not very likely, but possible. The current state works, we shouldn’t change it without a real benefit.

#9 in reply to: ↑ 8 ; follow-up: @ciapci
6 years ago

Replying to toscho:

Replying to markoheijnen:

I guess it would be nice to not return the value on echo but it isn't a big issue.

Why? I don’t see a bug or even a problem here. If you need the return value as a string, you can get it. And if you want to print it out, that works too.

We don’t even know if a change would break existing code. Let’s say someone is using the current behavior to reuse the return value:

$check = checked( $checked, $current, true );
// create the next form field then:
echo $check;

Not very likely, but possible. The current state works, we shouldn’t change it without a real benefit.

Ithe checked function is using the __checked_selected_helper function and in it, the $echo variable is under a if. Basically, if it's set to true, it echoes the string as it should, but if it is not set, it still echoes. $echo does not have a default value.
If using that code you mentionedm you should get printed checked='checked' twice. That is not normal.

Last edited 6 years ago by SergeyBiryukov (previous) (diff)

#10 in reply to: ↑ 9 ; follow-up: @toscho
6 years ago

Replying to ciapci:

Ithe checked function is using the __checked_selected_helper function and in it, the $echo variable is under a if. Basically, if it's set to true, it echoes the string as it should, but if it is not set, it still echoes.

No, it doesn’t. $echo is a required parameter here, if you don’t pass a value for it, you get a PHP error. And if it is false, nothing is printed.

If using that code you mentionedm you should get printed checked='checked' twice. That is not normal.

You control both places where the return value is printed. Try it. This is exactly what you can expect from the existing code.

#11 @helen
6 years ago

#25673 was marked as a duplicate.

#12 in reply to: ↑ 10 ; follow-up: @ciapci
6 years ago

Replying to toscho:

Replying to ciapci:

Ithe checked function is using the __checked_selected_helper function and in it, the $echo variable is under a if. Basically, if it's set to true, it echoes the string as it should, but if it is not set, it still echoes.

No, it doesn’t. $echo is a required parameter here, if you don’t pass a value for it, you get a PHP error. And if it is false, nothing is printed.

If using that code you mentionedm you should get printed checked='checked' twice. That is not normal.

You control both places where the return value is printed. Try it. This is exactly what you can expect from the existing code.

I used this code: http://d.pr/i/MMnc and I'm getting this: http://d.pr/i/HaYl.

It only shows once if I remove the true and the echo. But I need to set the result as a variable.

#13 follow-up: @markoheijnen
6 years ago

Then your code is wrong. You can also see it as a feature ;)

#14 in reply to: ↑ 13 @ciapci
6 years ago

Replying to markoheijnen:

Then your code is wrong. You can also see it as a feature ;)

my code is wrong because I need it as a variable? I'm working inside a string. I need it to echo because it's supposed to be something like $output .='string'.checked('aaa','aaa',true).'string'; I get an extra checked='checked'.
Why is my code wrong?

#15 in reply to: ↑ 12 @toscho
6 years ago

Replying to ciapci:

I used this code: http://d.pr/i/MMnc and I'm getting this: http://d.pr/i/HaYl.

It only shows once if I remove the true and the echo. But I need to set the result as a variable.

Then set the last parameter to false. Your code is wrong, not checked().

#16 @ciapci
6 years ago

hmm... I'm a little embarrassed right now.
I'm just going to say Sorry! and THANKS!
And we'll leave it at that, shall we?

#17 @toscho
6 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Don’t worry, we all had these moments. They are necessary steps. :)

#18 @helen
6 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.