开发者

PHP switch inside user function outputs nothing

First of all, I'm a novice PHP programmer at best. Basically, I have a switch function that I use in two different places. Each one switches a different variable, but all the cases inside the switch are identical for both instances. So I'm trying to create a function that runs the switch, and set the variable just before I call the function so it pulls in the right variable. But I'm getting no output. Example:

    function testfunc() {
    switch ($currentplace) { 
    case 1: 
    echo $thumb1;
    break; 

    case 2: 
    echo $thumb2;
    break; 

    case 3: 
    echo $thumb3;
    break; 

    case 4: 
    echo $thumb4;
    break; 
     }
}

Then, later I call the function like this:

//SET CASE FOR THUMB1开发者_如何转开发 
$currentplace = $place1;
testfunc();

And then later, somewhere else but for the second variable

//SET CASE FOR THUMB2 
$currentplace = $place2;
testfunc();

?>

Anyone tell me what I'm doing wrong?


Change your code like:

function testfunc($currentplace) {
...
}

testfunc($currentplace);

// or testfunc($place1);  

That is how you can pass a variable to a function such that it can use it. Note that the names do not have to match (see the commented line).

Your function cannot access global variables unless you mark them as such. But in this case, there's no need to. (In fact you never really need to use globals like that.)


You need to pass your variable to your function.

For exemple :

function testfunc($currentplace) {
  // your code here
}


testfunc($place1);

testfunc($place2);

And as for $currentplace your $thumb is not define is your function.

You can find more informations about variables scope in the manual.


You are using this portion of code :

$currentplace = $place1;
testfunc();

And, then, your testfunc() function looks like this :

function testfunc() {
    switch ($currentplace) { 
    // ...

But *your $currentplace variable, declared outside of the function, is not visible from inside the function : functions introduce their own variable scope.


You have two possible solutions :

  • either, at the beginning of the function, import your variable, using the global keyword (which is not quite a good solution -- as it makes the function dependent on an external variable.)
  • Or, better, pass your variable to the function, as a parameter.


Using the first, bad, idea, your function would be defined like this :

function testfunc() {
    global $currentplace;  // Make the outside variable visible from inside the function
    switch ($currentplace) { 
    // ...

For more informations on that, take a look at the Variable scope section of the manual.


And, using the second, far better, solution, it would be defined like that :

function testfunc($currentplace) {
    switch ($currentplace) { 

And called this way :

testfunc($place1);
testfunc($place2);

And for more informations on this, take a look at the Function arguments section of the PHP manual.


I guess, it's not matching to any case label. Keep a default statement and try to echo the value of $currentplace.

function testfunc() 
{
    switch ($currentplace) 
    {
       // .....

       default: echo $currentplace ;
                break ;
    }
}


echo $thumb1; will echo a variable that does not exist in the function's scope. I assume that is a global variable, in which case you must declare it via global $thumb1; first (the same goes for the other "thumb" ones).

$currentplace needs to come from somewhere too, you probably forgot to pass it as function parameter?


You have to place global $currentplace; in testfunc(). But, can't you just use an array instead of $thumbs?


Looks like you want to access a global variable from a function.

It is better to pass the variable as argument to the function.


It is a matter of scope. $currentplace, which you set to $place1/2 is not visible inside testfunc. The $currentplace inside testfunc is a different variable which happens to have the same name. (It would be pretty hard to write code otherwise, if we had to worry about what names other people might have used outside.)

The simplest solution would be to get the outside (global) $currentplace into testfunc's scope, in PHP's case using a global declaration, but that is considered bad practice (because we don't want functions unnecessarily modifying outside variables). The most accepted practice would be passing $currentplace as a parameter, which I'm sure numerous other answers have already shown how to do.

While we're at it, it is also a good practice for functions not to make any assumptions about how they're going to be used. I'm referring to your echo statements, which print to the standard output. If you decided later to collect the output in a string variable before outputting it, you'd have to modify testfunc. If testfunc simply returned the string, however, it would be a simple matter of saying echo testfunc($currentplace); or $place = testfunc($currentplace); according to your varying needs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜