Home >Backend Development >PHP Tutorial >How do php custom functions reference external variables?
I encountered a problem when writing the 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
Okay, an invalid argument has appeared, that is, an unavailable parameter.
How did this problem arise? Go back and find the code as follows:
foreach ($comments as $comment):
You can see now that $comments is ##Variable outside the function, but we use it in the function. According to the general programming ideas, variables outside the function should be regarded as global variables. If they are global variables, then there is no problem in calling them in the function.
It seems that PHP has some problems with our traditional thinking. So what are the global variables in PHP? I specially wrote the code to try it out. 1. Use global definition to output directly externally:global $mytext; $mytext=”nihao”; function chao_echo(){ echo $mytext; } chao_echo();Result: no output;
global $mytext; $mytext=”nihao”; function chao_echo(){ echo $GLOBALS['mytext']; } chao_echo();Result: Output is normal
$mytext=”nihao”; function chao_echo(){ global $mytext; echo $mytext; echo $GLOBALS['mytext']; } chao_echo();Result: Direct output or use GLOBALS globally Array output is fine.
4. Pass the external variables of the function into the 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
reference variables outside the function in PHP:
1. Global declaration outside the function, and use the $_GLOBALS array reference inside the function. 2. Global declaration within the function, $_GLOBALS array within the function or direct reference. 3. Pass a parameter when calling the function.Then, our modified version of the loop nested function is as follows
<?php function chao_comment_circle($chao_id,$comments){ ?> <?php foreach ($comments as $comment) > <?php if($comment->comment_parent==$chao_id) > <!-- 子评论 b --> <div class="comment_one_sub"> <?php if (function_exists('get_avatar')) { ?> <div class="gravatar_sub"><?php echo get_avatar($comment->comment_author_email,'32'); ?></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">【回复】</a></div> <div class="comment_date_sub"><?php echo $comment->comment_date; ?> <?php echo $comment->comment_time; ?></div> </div> <div class="comment_text_sub"><?php echo $comment->comment_content; ?></div> <?php chao_comment_circle($comment->comment_ID,$comments); ?> </div> <!-- 子评论 e --> <?php endif ?> <?php endforeach ?> <?php } ?>$chao_id is the ID of our parent comment, that is, we should output the comment that is not a reply first, and call the reply output function at the end of it .
The above is the detailed content of How do php custom functions reference external variables?. For more information, please follow other related articles on the PHP Chinese website!