-2

In the following code:

$num = Rand (1,2);
switch ($num)
{
case 1:
$var1 = "first value";
break;
case 2:
$var1 = "second value";
break;
}

My next part of coding needs the $var1 to be included inside an array of Jobs. Any idea how I would do this.

Thanks as always, Cameron.

  • 1
    `I need each case to make the variable $var1 change.` -- Why? When? How? Without more details, it's hard to suggest a solution. – Amal Murali Dec 15 '13 at 19:59
  • I apologize Amal, I was still formating the question as I hit enter. – user3103998 Dec 15 '13 at 20:00
  • 2
    So.. why does your code not work? it seems like it works to me. – Ali Dec 15 '13 at 20:02
  • The question is not very clear, you want to it to change regardless of the case? – feddus Dec 15 '13 at 20:02
  • 1
    What you have would work fine in isolation. Do you have more code than this? – Michael Berkowski Dec 15 '13 at 20:02
  • This code will work as you intend for you job decision, good luck! – feddus Dec 15 '13 at 20:03
  • @user3103998: I don't see anything wrong with your code. Also, you still haven't answered those questions :) – Amal Murali Dec 15 '13 at 20:03
  • If for example you did wrap this in a function, you might have variable scope issues. We need to see more code. – Michael Berkowski Dec 15 '13 at 20:03
  • I should have explained better. I need to call that variable in my next part of code inside another string. So it would be like $Job = $var . $var2. (We declared $var above as job, $var2 would be location defined later.) I believe the wording is, with this variable I have from the switch I need to concote (spelling?) it into another string. – user3103998 Dec 15 '13 at 20:04
  • @user3103998 Can you at least edit your question with the information you just supplied and what output are you expecting? – Ali Dec 15 '13 at 20:05
  • Sure Ali. I'm brand new here. Still learning the ropes. – user3103998 Dec 15 '13 at 20:05
  • @user3103998: And? Where exactly are you stuck? What doesn't work? Does it output some error messages? We have no idea what you're trying to accomplish. You really need to give us more details about what you're trying to accomplish if you want accurate answers. – Amal Murali Dec 15 '13 at 20:06

2 Answers2

4

why not this? code example works for php5.5

$var1  = array("first val","second", "and so on ...")[rand(0,2)];


code example works for php<5.5
$vars = array("first val","second", "and so on ...");
$var1 = $vars[array_rand($vars)];
jko
  • 2,068
  • 13
  • 25
  • That would work I would think, are we able to pass strings inside that? Every time I try a curly bracket anywhere I get errors with Unexpected ";" – user3103998 Dec 15 '13 at 20:09
  • 2
    You should probably test your code before posting it as an answer. – Ali Dec 15 '13 at 20:10
  • @AliTrixx That will work fine _in PHP 5.4+_ – Michael Berkowski Dec 15 '13 at 20:11
  • @AliTrixx: What is wrong with this code? It is syntactically correct (on PHP versions => 5.4). – Amal Murali Dec 15 '13 at 20:11
  • since i don't know which version of is running, i will not test code. who knows it is supposed to run on php4 or something? you never know. this is why i only post examples for php - one should be able to learn from examples as well. – jko Dec 15 '13 at 20:12
  • @AmalMurali I never knew that, new information to me. my apologies. – Ali Dec 15 '13 at 20:12
  • @MichaelBerkowski Just tested it on PHP 5.4.7 with syntax error. – user555 Dec 15 '13 at 20:14
  • 1
    @user555: [Works fine here](http://3v4l.org/GfmhD). – Amal Murali Dec 15 '13 at 20:18
  • @AmalMurali Only in PHP >= 5.5.0 and not in PHP 5.4 as claimed. – user555 Dec 15 '13 at 20:23
  • @user555 Dereferencing from a regular function call is 5.4+ like `func()[$n]`, but dereferencing from an `array()` construct as in this case apparently doesn't work prior to 5.5 `array(1,2,3)[$n]` – Michael Berkowski Dec 15 '13 at 20:27
  • @MichaelBerkowski Yes, what they call "literal dereferencing" was added in PHP 5.5. – user555 Dec 15 '13 at 20:29
  • and that's why i only post examples for php - like i said - you never know the required version of php. i know to good, that there are still php4-services out there... bit the latest is 5.5 so i keep sticking with new ;-) – jko Dec 15 '13 at 20:31
  • @floww Since we have established your example works in PHP 5.5+ perhaps you could edit your answer to reflect that. – user555 Dec 15 '13 at 20:34
  • you are right. just did.- – jko Dec 15 '13 at 20:38
0

Both of this versions work for me:

$strings = array('string 1', 'string 2', 'string 3');
$rnd = array_rand($strings);


$string = $strings[$rnd];
echo $string;

switch ($rnd) {
    case 0:
        $string = $strings[0];
        break;

    case 1:
        $string = $strings[1];
        break;

    case 2:
        $string = $strings[2];
        break;
}
echo $string;
jko
  • 2,068
  • 13
  • 25
M K
  • 9,138
  • 7
  • 43
  • 44