18

From a Q&A Thread I've came to meet with some old things, but I'm afraid, I found myself in a condition that, I still don't know the meaning of them and don't know why to use them.

  • %1$s - (found to load widget id)
  • %2$s - (found to load widget class/classes)
  • %s - (found here)

And how the following things work? I mean what's the explanation of the code-format?

  • !Website::getThemeOption("format_post/{$post_format}/content/hide")): - (found here)
  • if( !wp_verify_nonce( $_POST['my_noncename'] ) plugin_basename( __FILE__ ) ) return; - (found here)

This question can go on and on..., I know. But things like these are not clearly explained anywhere. I, not just want their meaning and purposes, but want some inner/core explanation so that I can understand their motives and how they works.

And also, if such things are already clearly defined in Codex, I'd love to read them. And would love to have a list of such curious things if such a list is already sorted out.

7
  • 3
    This is all plain PHP. See sprintf().
    – fuxia
    Commented Sep 27, 2013 at 14:23
  • The Codex exists to provide WordPress-specific documentation. It is not intended to teach PHP programming, and all of your questions are about PHP programming. PHP documentation can be found at php.net, and lots of PHP beginner tutorials can by found using Google.
    – Ben Miller
    Commented Sep 27, 2013 at 14:50
  • 3
    @BenMiller: Be patient please. Before toscho's comment, I's even unaware about the question's position. It's almost clear now with Dave's answer. Commented Sep 27, 2013 at 14:56
  • @MayeenulIslam, I was simply attempting to explain why you don't find such explanations in the Codex. I'm sorry if I came across as impatient.
    – Ben Miller
    Commented Sep 27, 2013 at 15:08
  • @BenMiller-RememberMonica, I also received this as impatience, as my first impression is that what you suggest is shutting down stack exchange as all of the questions asked can almost be found in manuals and any specific questions can be derived with creative thinking. :-) Anyway, yes, Codex is a great tool, but Google drives the flow in this question here ;) Commented Mar 10, 2022 at 11:38

1 Answer 1

14

This is really more of a general PHP programming question and might get closed for that reason. But before that happens, let's see if I can't clear some of this up for you.

Those strings that start with % signs are in "printf format". For example, %2$s translates to "replace this token with the second parameter, and treat it like a string".

When you see a variable in curly braces inside a string, like "Today is {$day}", that's the same as putting the variable there without curly braces, like "Today is $day". But formatting the variable like this makes it stand out, so it's more clear there's a variable there. It also prevents other characters next to the variable name from getting interpreted as part of the name. Without curly braces, would PHP know what to do with "Your robot name would be {$firstName}Number5"? It would see "$firstNameNumber5" and look for a variable named $firstNameNumber5 instead of simply $firstName.

I'm not sure what you're asking about in the last example. Is it the __FILE__ you're confused by? That's a PHP Magic Constant that gets replaced with the full path to the file that bit of code is in.

4
  • Before getting closed just the last simple one: !Website::. Why the double colon there? Commented Sep 27, 2013 at 14:44
  • 2
    The :: has to do with object-oriented programming -- in the case of Website::getThemeOption, you're calling the function getThemeOption belonging to the class Website.
    – Pat J
    Commented Sep 27, 2013 at 14:55
  • 2
    Also: {$var} helps PHP distinguish between something like {$complete_variable_name} and {$prefix}_rest_of_name, for example.
    – Pat J
    Commented Sep 27, 2013 at 14:57
  • FIY double colon is used to access only static or overridden methods and properties of a class, with regular methods you use -> e.g. $myObject->someMethod();
    – ed-ta
    Commented Jun 11, 2015 at 16:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.