1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Where to add this code?

Discussion in 'PHP' started by Divvy, Jun 9, 2017.

  1. #1
    Hello friends,

    Can someone give me a little help?

    I have this code:
    if(get_field( "nofollow_tag" )) { $nofollow_tag = 'rel="nofollow noopener"'; } else { $nofollow_tag = ''; }
    PHP:
    And now I need to add this code to show the value:
    <?php echo $nofollow_tag; ?>
    PHP:
    The idea is to show in this line:
    Since that code is already between php tags, how can I replace the #CODEHERE# with my code?
    My php knowledge is very poor to do this... probably I need to create a variable right?

    Thanks!!
    SEMrush
     
    Solved! View solution.
    Divvy, Jun 9, 2017 IP
    SEMrush
  2. #2
    Hi,
    probably someting like below?

    <?php
    $output = sprintf(
    '%s <a class="link" %s href="%s" %s>%s %s %s %s %s %s %s</a> %s',
    $site_icon,
    $target,
    $href,
    $nofollow_tag,
    $title,
    $sticky_icon,
    $additional_icon,
    $mobile,
    $new_icon,
    $extra,
    $hover,
    $review
    );
    ?>

    Ref:http://php.net/manual/en/function.sprintf.php
     
    hdewantara, Jun 9, 2017 IP
    Divvy likes this.
  3. Divvy

    Divvy Well-Known Member

    Messages:
    727
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    WOWWW thank you very much bud!
    I don't understand why I didn't remembered that :D
     
    Divvy, Jun 9, 2017 IP
  4. Harshal shah

    Harshal shah Active Member

    Messages:
    126
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    65
    #4
    you can put your code after $href in your output variable.

    Eg. $href,$nofollow_tag,
     
    Harshal shah, Jun 13, 2017 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,401
    Likes Received:
    1,876
    Best Answers:
    245
    Trophy Points:
    515
    #5
    ... and people wonder why I call sprintf halfwit garbage that's a remnant of early C compilers being useless single-pass rubbish that has ZERO place in modern code. STOP making the parser work twice as hard as it has to!

    
    <?php
    $output = 
    	'<a class="link" ' .
    	$site_icon .
    	' href="' . $target . '"' .
    	$nofollow_tag  . '>' .
    	$title . ' ' .
    	$sticky_icon . ' ' .
    	$additional_icon . ' ' .
    	$mobile . ' ' .
    	$new_icon . ' ' .
    	$extra . ' ' .
    	$hover . '</a>' .
    	$review;
    ?>
    
    Code (markup):
    Of course if you are only using that nofollow_tag variable once, don't waste a 'variable for nothing' on it -- and even if you were, ternary operators are your friend. Makes me wonder just how many 'variables for nothing' you've got that vomiting up in there.

    
    <?php
    $output = 
    	'<a class="link" ' .
    	$site_icon .
    	' href="' . $target . '"' .
    	(get_field( "nofollow_tag" ) ? ' rel="nofollow noopener"' : '' ) .
    	'>' .
    	$title . ' ' .
    	$sticky_icon . ' ' .
    	$additional_icon . ' ' .
    	$mobile . ' ' .
    	$new_icon . ' ' .
    	$extra . ' ' .
    	$hover . '</a>' .
    	$review;
    ?>
    Code (markup):
    Loses the variable for nothing and does the comparison right there in the $output creation.

    Either way, 99.99% of the time you see sprintF, somebody is horribly overthinking something simple.
     
    deathshadow, Jun 13, 2017 IP