# Switch function

**URL:** <https://forum.kirupa.com/t/switch-function/260237>\
**Category:** programming\
**Created:** [May 13, 2008, 6:38pm UTC](https://forum.kirupa.com/t/switch-function/260237 "2008-05-13T18:38:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharkb8](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@sharkb8](https://forum.kirupa.com/u/sharkb8)\
**Post date:** [May 13, 2008, 6:38pm UTC](https://forum.kirupa.com/t/switch-function/260237/1 "2008-05-13T18:38:01Z")

</div>

Im having problems getting this switch function to work properly:  
class Convert  
{  
public $var;

function Change($var)  
{  
switch($this-\>var)  
{  
case “grapes”:  
echo $this-\>var=“GRAPES”;  
break;  
case “apple”:  
echo $this-\>var=“APPLE”;  
break;  
case 1:  
echo $this-\>var=2;  
break;  
default:  
echo “Variable does not exist”;  
break;  
}  
}

}

$i=“apple”;  
$test=new Convert();  
$test-\>Change($i);

For some reason Im not passing the argument and I get the default statement. What gives?

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [May 13, 2008, 6:56pm UTC](https://forum.kirupa.com/t/switch-function/260237/2 "2008-05-13T18:56:54Z")

</div>

```auto

switch($var)

```

---

<div class="post-metadata">

**Author:** ![sharkb8](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@sharkb8](https://forum.kirupa.com/u/sharkb8)\
**Post date:** [May 13, 2008, 7:04pm UTC](https://forum.kirupa.com/t/switch-function/260237/3 "2008-05-13T19:04:34Z")

</div>

what am i doing wrong with that variable?

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 13, 2008, 7:37pm UTC](https://forum.kirupa.com/t/switch-function/260237/4 "2008-05-13T19:37:26Z")

</div>

The parameter var you are using in the Change function is a local variable. $test-\>var is not being assigned the value you pass in the $test-\>Change() method call, so when you switch($this-\>var), you haven’t assigned $this-\>var the value of $var yet.

I like to use an underscore in my object properties to avoid confusion.

---

<div class="post-metadata">

**Author:** ![sharkb8](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@sharkb8](https://forum.kirupa.com/u/sharkb8)\
**Post date:** [May 13, 2008, 8:27pm UTC](https://forum.kirupa.com/t/switch-function/260237/5 "2008-05-13T20:27:11Z")

</div>

I thought that you can pass any variable as an argument and in this case $i=“apple”. The $var is a public variable so i dont see why i can access it. Thank you for your input.

---

<div class="post-metadata">

**Author:** ![sharkb8](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@sharkb8](https://forum.kirupa.com/u/sharkb8)\
**Post date:** [May 13, 2008, 8:42pm UTC](https://forum.kirupa.com/t/switch-function/260237/6 "2008-05-13T20:42:45Z")

</div>

OK I got it now. Thank you for taking a look at my coding!

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 13, 2008, 8:46pm UTC](https://forum.kirupa.com/t/switch-function/260237/7 "2008-05-13T20:46:09Z")

</div>

You can pass any variable as an argument. My understanding is, however, that you have two different $var variables. The public property var and the local function attribute.When you call the method as you have described, you are assigning the value of $i to the local variable $var, but there isn’t anyplace where you assign $this-\>var = $var , so to speak.

You can either assign $var to $this-\>var before you call the switch statement, or you can evaluate $var in the switch statement rather than $this-\>var

---

<div class="post-metadata">

**Author:** ![sharkb8](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@sharkb8](https://forum.kirupa.com/u/sharkb8)\
**Post date:** [May 13, 2008, 9:03pm UTC](https://forum.kirupa.com/t/switch-function/260237/8 "2008-05-13T21:03:48Z")

</div>

Makes sense. You have to assign that $i variable to something. I feel such a newb but Im learning.

I thought when you pass a variable in your object, it also is passed through the function’s method. So I guess I have to assign it in the class to get passed through the function.
