Home > Backend Development > PHP Tutorial > How do php custom functions reference external variables?

How do php custom functions reference external variables?

伊谢尔伦
Release: 2023-03-11 13:40:02
Original
3279 people have browsed it

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();
Copy after login

Result: no output;


2. Use GLOBALS array output:


global $mytext;
$mytext=”nihao”;
function chao_echo(){
echo $GLOBALS['mytext'];
}
chao_echo();
Copy after login

Result: Output is normal


3. Globally declare variables outside the function within the function:


$mytext=”nihao”;
function chao_echo(){
global $mytext;
echo $mytext;
echo $GLOBALS['mytext'];
}
chao_echo();
Copy after login

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);
Copy after login

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(&#39;get_avatar&#39;)) { ?> <div class="gravatar_sub"><?php echo get_avatar($comment->comment_author_email,&#39;32&#39;); ?></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(&#39;<?php echo $comment->comment_ID; ?>&#39;,&#39;<?php echo $comment->comment_author; ?>&#39;)"><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
}
?>
Copy after login

$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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template