Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1279057
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm trying to create a few custom post types, and instead of hard-coding their names I just want to define them as a globally scoped variable.
I thought I understood the scope but apparently not. The code below results in an error of Undefined variable
:
$filmLabel = "Films";
$showLabel = "Shows";
function CreatePostTypes() {
register_post_type( $filmLabel, GenerateFilmType($filmLabel));
register_post_type( $showLabel, GenerateFilmType($showLabel));
}
add_action( 'init', 'CreatePostTypes' );
I've tried using $GLOBAL
as well, with the same result.
Can anyone spot what's wrong?
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm trying to create a few custom post types, and instead of hard-coding their names I just want to define them as a globally scoped variable.
I thought I understood the scope but apparently not. The code below results in an error of Undefined variable
:
$filmLabel = "Films";
$showLabel = "Shows";
function CreatePostTypes() {
register_post_type( $filmLabel, GenerateFilmType($filmLabel));
register_post_type( $showLabel, GenerateFilmType($showLabel));
}
add_action( 'init', 'CreatePostTypes' );
I've tried using $GLOBAL
as well, with the same result.
Can anyone spot what's wrong?
1 Answer
Reset to default 1Globals are generally discourgaged. You can use define instead.
<?php
define("FILM_LABEL", "Films");
define("SHOW_LABEL", "Shows");
function CreatePostTypes() {
register_post_type( FILM_LABEL, GenerateFilmType( FILM_LABEL ) );
register_post_type( SHOW_LABEL, GenerateFilmType( SHOW_LABEL ) );
}
add_action( 'init', 'CreatePostTypes' );
本文标签: phpVariable global scope
版权声明:本文标题:php - Variable global scope 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274281a2369636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
define()
which seems a bit dated in modern PHP, I would simply go with a public class constant. – kero Commented Oct 21, 2021 at 8:14