开发者

PHP Function parameters - problem with var not being set

开发者 https://www.devze.com 2022-12-23 15:06 出处:网络
So I am obviously not a very good programmer.I have written this small function: function dispAdjuggler($atts) {

So I am obviously not a very good programmer. I have written this small function:

function dispAdjuggler($atts) {
extract(shortcode_atts(array(
    'slot' => ''
), $atts)); 

$adspot = '';
$adtype = '';

// Get blog # we're on
global $blog_id;
switch ($blog_id) {
    case 1:
        // root blog HOME page
        if (is_home()) {
            switch ($slot) {
                case 'top_leaderboard':
                    $adspot = '855525';
                    $adtype = '608934';
       开发者_运维知识库         break;
                case 'right_halfpage':
                    $adspot = '855216';
                    $adtype = '855220';
                break;
                case 'right_med-rectangle':
                    $adspot = '858222';
                    $adtype = '613526';
                break;
                default:
                    throw new Exception("Ad slot is not defined");
                break;
            }

When I reference the function on a page like so:

<?php dispAdjuggler("top_leaderboard"); ?>

The switch is throwing the default exception. What am I doing wrong here?

Thanks!!


Without know what the shortcode_atts() function does, it looks like you're passing in array to set default values ('slot' = empty string), which extract() then converts into $short = ''

extract(shortcode_atts(array(
    'slot' => ''
), $atts)); 

right? Now that $slot is the empty string, it won't match any of the cases in your switch, and therefore the default case gets triggered, which throws the "Ad slot is not defined" exception.


Aren't you missing the extract type parameter?

http://us.php.net/manual/en/function.extract.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号