我正在尝试将 audiomack 集成到我的网站中,并且我已经编写了一个功能来使其正常工作。
//Get the Audiomack URL
if (!function_exists('samk_audio_audiomack')) {
function samk_audio_audiomack($url, $width = '100%', $height = 250) {
if($url!='') {
$buff = '';
$buff .= "\n<!-- audiomack | {$plugin_data['name']} | {$plugin_data['url']} -->\n";
$embed_src = $url;
$embed_ver_prefix = 'embed4';
// Check if the embedded source is song or album
if(stripos($embed_src, '/song/') !== false) { //it is a song
$embed_src = str_replace('/song/', "/$embed_ver_prefix-large/", $embed_src);
} else { //it is an album
$height = 352;
$embed_src = str_replace('/album/', "/$embed_ver_prefix-album/", $embed_src);
}
$height_str = "height='$height'";
$embed_code = "<iframe src='$embed_src' scrolling='no' width='$width' $height_str scrollbars='no' frameborder='0'></iframe>\n";
$buff .= "<div class='audiomack_player_container'>\n";
$buff .= $embed_code;
$buff .= "</div> <!-- /audiomack_player_container -->\n";
$buff .= "\n<!-- /audiomack | {$plugin_data['name']} | {$plugin_data['url']} -->\n";
return $buff;
}
}
}
这是我用来回应功能的代码:
<?php }
if($audiomack_audio!='') {
$audiomack_audio_code = samk_audio_audiomack($audiomack_audio,'100%',250); ?>
<div class="audio-container sermon-tabs" id="audiomack_audio">
<?php echo $audiomack_audio_code; ?>
</div>
<?php } ?>
我现在面临的挑战是当我echo
函数samk_audio_audiomack($url, $width, $height)
时,没有任何作用。但是,如果我将return
更改为echo
或print
,则会返回$buff
的值。
答案 0 :(得分:1)
return会将值$buff
“返回”到调用函数的位置。
如果在函数内部回显$buff
,它将在PHP
运行脚本时回显它,否则它会将$buff
变量数据提供给调用函数的变量。< / p>
您可以从函数内部回显以获得灵活性,但这会完全破坏函数的用途,因为函数会减少复制并在不同位置的负载中粘贴相同的代码以回显类似(在您的情况下)的输出。
查看类似的问题“PHP echo function return value vs echo inside function”
如果在函数内调用,则立即返回语句 结束当前函数的执行,并将其参数作为 函数调用的值。
http://php.net/manual/en/functions.returning-values.php
<强>示例:强>
$plugin_data['name']
&amp; $plugin_data['url']
将返回undefined,因为我不知道其中是否定义了else。
//$audio_caller now contains what $buff returned
$audio_caller = samk_audio_audiomack('http://www.example.com');
//Use this where you want to echo the function contents
echo $audio_caller;
以下是对以下脚本的测试:https://3v4l.org/CuE0f
//Get the Audiomack URL
if (!function_exists('samk_audio_audiomack')) {
function samk_audio_audiomack($url, $width = '100%', $height = 250) {
if($url!='') {
$buff = '';
$buff .= "\n<!-- audiomack | {$plugin_data['name']} | {$plugin_data['url']} -->\n";
$embed_src = $url;
$embed_ver_prefix = 'embed4';
// Check if the embedded source is song or album
if(stripos($embed_src, '/song/') !== false) { //it is a song
$embed_src = str_replace('/song/', "/$embed_ver_prefix-large/", $embed_src);
}else{ //it is an album
$height = 352;
$embed_src = str_replace('/album/', "/$embed_ver_prefix-album/", $embed_src);
}
$height_str = "height='$height'";
$embed_code = "<iframe src='$embed_src' scrolling='no' width='$width' $height_str scrollbars='no' frameborder='0'></iframe>\n";
$buff .= "<div class='audiomack_player_container'>\n";
$buff .= $embed_code;
$buff .= "</div> <!-- /audiomack_player_container -->\n";
$buff .= "\n<!-- /audiomack | {$plugin_data['name']} | {$plugin_data['url']} -->\n";
return $buff;
}
}
}
$audio_caller = samk_audio_audiomack('http://www.example.com');
echo $audio_caller;
修改强>
在第1行,为什么会有一个紧密的大括号? <?php }
//删除了测试
以下示例适用于版本5.3到7。
<?php
$audiomack_audio = 'www.example.com';
if($audiomack_audio!='') { ?>
<div class="audio-container sermon-tabs" id="audiomack_audio">
<?php $audiomack_audio_code = samk_audio_audiomack($audiomack_audio,'100%',250); ?>
<?php echo $audiomack_audio_code; ?>
</div>
<?php } ?>