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!!
     
    Solved! View solution.
    Divvy, Jun 9, 2017 IP
  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:
    785
    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,732
    Likes Received:
    1,999
    Best Answers:
    253
    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