sumolotokai Posted January 26, 2010 Share Posted January 26, 2010 Hi, Any help with this would be much appreciated, I have racked my brains but to no avail. I am new to all this (3 weeks experience). I am working through a book at the mo and this code popped up function tagwrap($tag, $txt, $func = " ") { if ((!empty($txt)) && (function_exists($func))) { $txt = $func($txt); return "<$tag>$txt</$tag>\n"; } } I am happy with what it does but not how it does it. what does this do? $txt = $func($txt); Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/ Share on other sites More sharing options...
sumolotokai Posted January 27, 2010 Author Share Posted January 27, 2010 hello again, I dont really understand why people are viewing this and not responding. I know it is a silly question. 8: function tagWrap( $tag, $txt, $func="" ) { 9: if ( ! empty( $txt ) && function_exists( $func ) ) { 10: $txt = $func( $txt ); 11: return "<$tag>$txt</$tag>\n"; 12: } 13: } 14: 15: function underline( $txt ) { 16: return "<u>$txt</u>"; 17: } 18: 19: print tagWrap('b', 'make me bold'); 20: // <b>make me bold</b> 21: 22: print tagWrap('i', 'underline me too', "underline"); 23: // <i><u>underline me too</u></i> 24: 25: print tagWrap('i', 'make me italic and quote me', 26: create_function('$txt', 'return ""$txt"";')); 27: // <i>"make me italic and quote me"</i> 28: I have found the code online and here it is above! my query again, what does line 10 do? I cant bring myself to skip this and carry on. I have been stuck on this one line for 72 hours! Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002395 Share on other sites More sharing options...
PFMaBiSmAd Posted January 27, 2010 Share Posted January 27, 2010 I dont really understand why people are viewing this and not responding. Because the forum section where you posted this (PHP Applications, Frameworks and Libraries » Third Party PHP Scripts) has nothing to do with the problem. Moving thread to the PHP Help forum section... Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002405 Share on other sites More sharing options...
sumolotokai Posted January 27, 2010 Author Share Posted January 27, 2010 Ahhh, I see. Hopefully I will have more luck here, thanks for moving it. Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002420 Share on other sites More sharing options...
JasonO Posted January 27, 2010 Share Posted January 27, 2010 It simply puts $txt into a function (although I'm not sure that function is) and the returned value of that is then placed back in to the $txt variable. The function tagWrap has 3 parameters. Tag, Text and Func. Whatever function is passed into the tagWrap will depend on what it returns (will be nothing if $func refers to a non existant function). If you look at line 15 you will see of the functions 'underline'. It's more or less an expanded function of that, but allowing it to be more dynamic (ie can use any function that exists within it, rather than just underline). Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002427 Share on other sites More sharing options...
teamatomic Posted January 27, 2010 Share Posted January 27, 2010 Nothing. function tagWrap( $tag, $txt, $func="" ) { if ( ! empty( $txt ) && function_exists( $func ) ) { $txt = $func( $txt ); return "<$tag>$txt</$tag>\n"; } } look at the first line of code. it uses the vars inside of (vars) as passed with the call to it. $a=1 $b=2 $answer = add($a,$b) function add($a,$b){ $sum = $a+$b; return "$sum"; } $answer would have a value of 3 Now look at the second line of code. $func is empty as passed to tagWrap, so how can function_exist find a non-named function that obviouslt does not exist? The statement is FALSE and does nothing. It is for you to put a function there for the function to use. Confused? http://www.php.net/manual/en/functions.user-defined.php HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002429 Share on other sites More sharing options...
jl5501 Posted January 27, 2010 Share Posted January 27, 2010 The idea of the $func in the call is for you tp specify a function that can be applied to the text before it is wrapped eg you may want to call strtoupper() on the text to make it upper case In which case you simply call tagWrap('b', 'make me bold','strtoupper'); Then of course the if(function_exists()) checks the function you have supplied exists, and if so, calls it Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002437 Share on other sites More sharing options...
sumolotokai Posted January 27, 2010 Author Share Posted January 27, 2010 Thanks for all the help!!! It is starting to make a bit more sense, time to continue Quote Link to comment https://forums.phpfreaks.com/topic/189892-noob-help-plz/#findComment-1002580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.