How do PHP custom functions reference external variables?

Source: Internet
Author: User
A problem was encountered when writing a loop nesting of comments:
Warning:invalid argument supplied for foreach () in/home/yiliaoba/domains/chaochaoblog.com/public_html/wp-content/ Themes/chaochao/comments.php on line 49
Well, there's a invalid argument, which is an unavailable parameter.

How does this problem come about, go back and look for the code as follows:
foreach ($comments as $comment):

That's what it looks like, $comments is a variable outside of the function, and we're using it in the function. According to the general program of thinking, the variables outside the function should be considered as global variables, if it is a global variable, then call in the function is completely no problem.

It seems that PHP and our traditional thinking a bit of a problem, then the global variables in PHP, I deliberately write code to try.

1. Define the direct output externally with global:

Global $mytext; $mytext = "Nihao"; function Chao_echo () {echo $mytext;} Chao_echo ();

Result: no output;

2. Output with Globals array:

Global $mytext; $mytext = "Nihao"; function Chao_echo () {echo $GLOBALS [' mytext '];} Chao_echo ();

Result: Output Normal

3. Declare variables outside the function globally within the function:

$mytext = "Nihao", function Chao_echo () {global $mytext; echo $mytext; Echo $GLOBALS [' mytext '];} Chao_echo ();

Result: direct output or with Globals global array output is OK.
4. Pass the function external variables in parameters:

$mytext = "Nihao"; function Chao_echo ($mytext) {echo $mytext;} Chao_echo ($mytext);

Result: can be output.

To summarize, in PHP, there are three ways to refer to variables outside the function in a function:

1. Outside the function global declaration, the function uses the $_globals array reference.

2. function within the global declaration, function within the $_globals array or direct reference.

3. A parameter is passed when the function is called.
So, our modified version of the loop nesting function is as follows

<?php function chao_comment_circle ($chao _id, $comments) {? ><?php foreach ($comments as $comment) ><?php  if ($comment->comment_parent== $chao _id) > <!--comments B--and <div class= "Comment_one_sub" > <?php if (function_exists (' Get_avatar ')) {?> <div class= "gravatar_sub" ><?php Echo Get_avatar ($comment Comment_author_email, ' + ');?></div> <?php}?> <div class= "Comment_frame_sub" > <div class= "Comment_author_sub" ><a href= "<?php echo $comment->comment_author_url;?>" ><?php Echo $comment- >comment_author;?></a></div> <div class= "comment_reply_sub" onclick= "chao_reply (' <?php echo $comment->comment_id;?> ', ' <?php echo $comment->comment_author;?> ') "><a href=" #respond " > "Reply" </a></div> <div class= "comment_date_sub" ><?php echo $comment->comment_date;?> & lt;? php echo $comment->comment_time;? &GT;&LT;/DIV&GT;</div><div class= "Comment_text_sub" ><?php Echo $comment->comment_content;?></div> <?php chao_comment_circle ($comment->comment_id, $comments)?></div><!--Sub-comment e--><?php endif ? ><?php Endforeach?><?php}?>

The

$chao _id is the ID of our parent comment, that is, we call the reply output function at the end of the comment because it is the first to output not a reply.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.