Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Explanation - Trying to understand some code.
Old 11-04-2005, 01:47 AM Explanation - Trying to understand some code.
Webmaster Talker

Posts: 626
Trades: 0
I am trying to teach myself PHP. I have been using SAMS Teach yourself PHP, MySQL & Apache in 24 hours.

I don't understand one of the examples used and was hoping for some clarification.

Code:
1: <html>
 2: <head>
 3: <title>Listing 6.14</title>
 4: </head>
 5: <body>
 6: <?php
 7:
 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: echo tagWrap('b', 'make me bold');
20: // will print <b>make me bold</b>
21:
22: echo tagWrap('i', 'underline me too', "underline");
23: // will print <i><u>underline me too</u></i>
24:
25: echo tagWrap('i', 'make me italic and quote me',
26:     create_function('$txt', 'return "&quot;$txt&quot;";'));
27: // will print <i>&quot;make me italic and quote me&quot;</i>
28: ?>
29: </body>
30: </html>
I don't understand this:

Quote:
Finally, on line 25, we call tagWrap(), which wraps text in quotation entities. Of course, it would be quicker to simply add the entities to the text to be transformed ourselves, but this illustrates the point that function_exists() works as well on anonymous functions as it does on strings representing function names.
I don't understand where function_create is coming from and how it works in this snipet of code.

Can anyone please clarify?

Zinc.
jim.thornton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-04-2005, 04:47 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
create_function is a builtin PHP function - there is documentation for it here:
http://www.php.net/manual/en/functio...e-function.php

echo tagWrap('i', 'underline me too', "underline"); means 'call the function called underline on the text, then put <i> tags round it'

echo tagWrap('i', 'make me italic and quote me',
create_function('$txt', 'return "&quot;$txt&quot;";'));
means 'call this function I've just given you on the text and then put <i> tags round it'
It is an anonymous function since it is never given a name, it gets created inside the arguments to tagWrap() and fed straight in.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 11-04-2005, 10:53 AM
Webmaster Talker

Posts: 626
Trades: 0
thanks... I now understand it. However, I still don't see why someone would want to do this rather than just putting the code inside the function. Could you enlighten me?

Zinc.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 11-04-2005, 03:31 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Say you want a function that returns another function, but what this new function does depends on results from some other part of your code. You can't know what these results will be before you start, so create_function allows you to create new bits of PHP using PHP itself.

Anonymous functions are useful when you need to pass a function as an argument, but only want to use it once. This way you don't have to store them or come up with new names for them all. Of course if you want to refer tothem again you have to assign them to a variable.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 11-06-2005, 04:24 PM
Webmaster Talker

Posts: 626
Trades: 0
Thanks.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Explanation - Trying to understand some code.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42981 seconds with 12 queries