最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Using same variable names in files added with get_template_part()

programmeradmin2浏览0评论

Say I have a file named content-header.php and content-body.php.

In content-header.php I have this variable:

$some_var = 'apples';

and in content-body.php I have a variable with the same name but different value:

$some_var = 'bananas';

In another file I use:

get_template_part('content', 'header');
get_template_part('content', 'body');

Would $some_var be limited to its own file or overwrite the other? Is it ok to use same variable names in files added through get_template_part()?

Would I have to use unique variable names?

Say I have a file named content-header.php and content-body.php.

In content-header.php I have this variable:

$some_var = 'apples';

and in content-body.php I have a variable with the same name but different value:

$some_var = 'bananas';

In another file I use:

get_template_part('content', 'header');
get_template_part('content', 'body');

Would $some_var be limited to its own file or overwrite the other? Is it ok to use same variable names in files added through get_template_part()?

Would I have to use unique variable names?

Share Improve this question asked Jun 5, 2019 at 20:16 at least three charactersat least three characters 33712 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You can absolutely use the same variable names. Arguments like that do not pass through from one template to another without a little help from a function.

So if in content-header.php you have $fruit = banana; and in content-body.php you have $fruit = apple; you will not have a conflict. Go nuts. Or bananas. ;)

发布评论

评论列表(0)

  1. 暂无评论