Home > other >  what is this code mean : return '/' === substr( get_option( 'permalink_structure'
what is this code mean : return '/' === substr( get_option( 'permalink_structure'

Time:11-27

what is this code mean : return '/' === substr( get_option( 'permalink_structure' ), -1, 1 );

I know " get_option( permalink_structure ) " mean.

I read a wordpres plugin and see this code please answer me what is this code mean :::

return '/' === substr( get_option( 'permalink_structure' ), -1, 1 );

When I print this code , the number 1 is printed , my permalink_structure is "/%postname%/"

CodePudding user response:

return '/' === substr( get_option( 'permalink_structure' ), -1, 1 )

let's break this code.

  1. get_option( 'permalink_structure' )
    here get_option is a function that takes a string as input and as a result, it gives us a value from the wp_options table by matching the option name which we passed as an argument.

  2. substr( get_option( 'permalink_structure' ), -1, 1 )
    substr is an extraction function for a string you can read about in detail on the documentation page. string, offset, length are 3 params for this function. In the example option value is a string, -1 is offset and 1 is the length. so here we're telling the function, here is the string now goto to offset -1, which is the first char from the end, and then from that point extract the 1 character.

  3. '/' === substr( get_option( 'permalink_structure' ), -1, 1 )
    here === is called strict comparison, which means data type and value both should be same. so from point no. 2 code, we're expecting / as substr output and then we're comparing it with ===

  4. return '/' === substr( get_option( 'permalink_structure' ), -1, 1 )
    here using return we're returning the result of the comparison which will be either true or false.

I print this code, the number 1 is printed, and my permalink_structure is "/%postname%/"

1 is being printed because you're getting true as the result and if you'll echo/print the boolean true it will get print as 1

  • Related