Randomly producing True or False - how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Randomly producing True or False - how?

Post by simonmlewis »

Hi
I need to be able to randomly have one of two responses that product an SQL query code.

So random result one will give a "no", and random result two will product the query.

How would I do this?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Randomly producing True or False - how?

Post by flying_circus »

Code: Select all

<?php
  $random = rand(0,1);

  if($random == 0) {
    echo 'No';
  } else {
    echo 'Query';
  }
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Randomly producing True or False - how?

Post by Jonah Bron »

Code: Select all

$var = rand(0,1) ? 'No' : 'SELECT ...';
:)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Randomly producing True or False - how?

Post by simonmlewis »

I understand flying_circus's, but not Jonah's.... Jonah can you explain how yours operates?

This is the sql that would need to be placed in there:

Code: Select all

$sqlconn=@mysql_connect("localhost","root","");
$rs=@mysql_select_db("dbname",$sqlconn);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Randomly producing True or False - how?

Post by simonmlewis »

Code: Select all

<?php 

  $random = rand(0,1);

  if($random == 0) {
    echo 'Sorry there is a fault';
  } else {
    $sqlconn=@mysql_connect("localhost","root","");
$rs=@mysql_select_db("db_name",$sqlconn);
  }
?>
This works real nice. But if the other one is meant to be slicker, still interested it......
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Randomly producing True or False - how?

Post by Apollo »

simonmlewis wrote:I understand flying_circus's, but not Jonah's.... Jonah can you explain how yours operates?
See ternary operator.
This is the sql that would need to be placed in there:
That's no SQL, just some php code to create a connection to an SQL server ;)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Randomly producing True or False - how?

Post by pickle »

Jonah Bron wrote:

Code: Select all

$var = rand(0,1) ? 'No' : 'SELECT ...';
:)
Ternary operator is basically:
some variable = (condition) ? (result if condition == TRUE) : (result if condition == FALSE)

rand(0,1) will produce either a 0 or a 1. PHP interprets 1 as equivalent to boolean TRUE, so the first result will be used. PHP interprets 0 as equivalent to boolean FALSE, so the second result will be used.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply