Jump to content


random number


9 replies to this topic

#1 baggyn

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Location:Top secret
  • Interests:COMPUTER

Posted 28 September 2005 - 01:47 PM

hi

im trying to code a page so that a random number is selected, if one number is selected it displays one thing, if the other number is selected it displays something else.

I wrote this but it doesnt seem to work can anyone help?

<?php
$arg = rand(1,2);

if ($arg='1')
echo "lo";

if ($arg='2')
echo "hi";
?>


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 September 2005 - 05:35 PM

Vary simple problem...
if($arg==1){
  echo "lo";
}elseif($arg==2){
  echo "hi";
}
When doing an if, you want to see if something equals "==" another thing. Not set it equal "=" to another.

p.s. I did change the structure of your code to how i prefer it, you can change it to the way you had it (if you know how) later :P

#3 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 29 September 2005 - 01:11 AM

<?php
$arg = rand(1, 2);
$output = array(
'lo',
'hi');
echo $output[$arg-1];
?>
This is a little shorter, and does the same thing

#4 MillerTime

    Young Padawan

  • Members
  • Pip
  • 69 posts

Posted 29 September 2005 - 05:25 PM

rc69, on Sep 28 2005, 10:35 PM, said:

Vary simple problem...
if($arg==1){
  echo "lo";
}elseif($arg==2){
  echo "hi";
}
When doing an if, you want to see if something equals "==" another thing. Not set it equal "=" to another.

p.s. I did change the structure of your code to how i prefer it, you can change it to the way you had it (if you know how) later :P
actually, "==" is not called "equal" it is called "identical". So when using ifs and elses do not use the equal sign ("=") because that would just cause an infinite loop

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 September 2005 - 06:08 PM

Well, if you're going to be just echoing a string, yes, halopro's code would probably be better, although it may use more memory (not even a noticeable amount). If you're going to preform an actual function or something, you'd use something like mine

MillerTime, on Sep 29 2005, 03:25 PM, said:

rc69, on Sep 28 2005, 10:35 PM, said:

Vary simple problem...
if($arg==1){
  echo "lo";
}elseif($arg==2){
  echo "hi";
}
When doing an if, you want to see if something equals "==" another thing.  Not set it equal "=" to another.

p.s.  I did change the structure of your code to how i prefer it, you can change it to the way you had it (if you know how) later :P
actually, "==" is not called "equal" it is called "identical". So when using ifs and elses do not use the equal sign ("=") because that would just cause an infinite loop
http://www.php.net/manual/en/language.oper....comparison.php
"===" is 'identical'. Trust me, i've been doing this for a while, and they told me i was right ^ :)

#6 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 07 October 2005 - 04:54 PM

HaloprO, on Sep 29 2005, 06:11 AM, said:

<?php
$arg = rand(1, 2);
$output = array(
'lo',
'hi');
echo $output[$arg-1];
?>
This is a little shorter, and does the same thing
should actually be

$arg = rand(0, 1);

right?

because array[0] = "lo", array[1]="hi" and array[2]=""

Edited by SpaceGhost, 07 October 2005 - 04:54 PM.


#7 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 07 October 2005 - 07:29 PM

Yes it should SpaceGhost, that's why in mine I used -1 at the end, and for you guys discussing == and ===, == is for stuff likw equal to, with exceptions (ie: gHoSt = GHOST) and for === it would have to be GHOST = GHOST, case sensitive really :/

#8 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 09 October 2005 - 10:27 PM

:tiphat:

well, if that's the difference then why not just do?

strtolower("GHOST") == strtolower("gHoST");

:D

#9 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 09 October 2005 - 11:36 PM

Because that is like 15 chars longer

#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 October 2005 - 03:30 PM

http://www.php.net/m...comparisons.php The actual reference from php.net
If you want to know more, or argue who's right, go there...

I'm closing this topic since it's already been solved. Or at least is presumed solved.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users