willdk Posted December 10, 2008 Share Posted December 10, 2008 if you have $min = "10"; $max = "20"; ... rand($min,$max) How do you add +1 to the value of the rand output? rand($min,$max) +1 Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/ Share on other sites More sharing options...
Mr_J Posted December 10, 2008 Share Posted December 10, 2008 if you have $min = "10"; $max = "20"; ... rand($min,$max) How do you add +1 to the value of the rand output? rand($min,$max) +1 I would use the if statement. Search google... http://www.google.co.za/search?hl=en&q=php+if+statement&btnG=Search&meta= Tzag.com... Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711174 Share on other sites More sharing options...
MatthewJ Posted December 10, 2008 Share Posted December 10, 2008 <?php $min = 10; $max = 20; $num = rand($min, $max); $num++; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711176 Share on other sites More sharing options...
willdk Posted December 10, 2008 Author Share Posted December 10, 2008 I would use the if statement. Search google... http://www.google.co.za/search?hl=en&q=php+if+statement&btnG=Search&meta= Tzag.com... I don't understand why I have to use 'if' ??? I make this smaller... $random = rand($min,$max); Then I have a value between 10 and 20 for $random. So if the value is 15 then I want to have the same value to be increased by 1 ($random + 1). I'm a newbie so don't shoot me if I'm wrong... Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711179 Share on other sites More sharing options...
willdk Posted December 10, 2008 Author Share Posted December 10, 2008 <?php $min = 10; $max = 20; $num = rand($min, $max); $num++; ?> I tried this code before but I get the same value <?php $min = "10"; $max = "20"; $random = rand($min,$max); echo $random.' and '.$random++ ; ?> 10 and 10 16 and 16 ... Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711182 Share on other sites More sharing options...
BjornR_1989 Posted December 10, 2008 Share Posted December 10, 2008 Use ++$var If you echo the var immediately. <?php $var = 1; echo $var++; //returns 1 but after the echo it has INT 2 as value $var = 1; echo ++$var; //returns 2 ?> Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711188 Share on other sites More sharing options...
willdk Posted December 10, 2008 Author Share Posted December 10, 2008 Thanks Bjorn, But it was not what I was looking for I found the solution after trying and trying $random + 1 gr. Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711193 Share on other sites More sharing options...
BjornR_1989 Posted December 10, 2008 Share Posted December 10, 2008 $var + 1; is exactly the same as $var++; Btw, don't use $min = "10"; $max = "20"; for integers, use $min = 10; $max = 20; Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711196 Share on other sites More sharing options...
willdk Posted December 10, 2008 Author Share Posted December 10, 2008 Btw, don't use $min = "10"; $max = "20"; for integers, use $min = 10; $max = 20; Thanks for the tip $var + 1; is exactly the same as $var++; my working code $random = rand($min,$max); $irandom = $random + 1; When I use $random++ the value decreases (-1) Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711204 Share on other sites More sharing options...
balistic Posted December 10, 2008 Share Posted December 10, 2008 why not just use $random = rand($min,$max)+1; Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711265 Share on other sites More sharing options...
willdk Posted December 10, 2008 Author Share Posted December 10, 2008 why not just use $random = rand($min,$max)+1; Works indeed. thx. Then I don't need that extra line '$irandom...'. gr. Quote Link to comment https://forums.phpfreaks.com/topic/136325-solved-how-do-you-add-1-in-this-part/#findComment-711501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.