Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Guess random number filter_list
Author
Message
Guess random number #1
this is my first try with php i'm having an error i'm not sure why this is happening hope someone might help .

Spoiler:
Code:
<?php
session_start();
if($_SESSION["hasStarted"]!=true){
$_SESSION["number"]=rand(0,20);
$_SESSION["hasStarted"]=true;
$_SESSION["tries"]=0;
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="random.php">
<input type="text" name="number"/>
<input type="submit" value="submit"/>
</form>
<?php
echo $_SESSION["number"];
if($_POST){
    $_SESSION["tries"]++;
if($_POST["number"]==$_SESSION["number"]){
    $_SESSION["hasStarted"]=false;
    echo $_SESSION["tries"];
    
     ?><script>alert(<?php echo $_SESSION["tries"];?>);</script>

<a href="random.php">PLAY AGAIN</a>

<?php
$_SESSION["tries"]=0;
}
else{?>
    <script>alert("false");</script>
    <?php echo $_SESSION["number"];
}}?>
</body>
</html>

The error i'm having is : Notice: Undefined index: hasStarted in C:\xampp\htdocs\test\random.php on line 3


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Guess random number #2
(04-02-2017, 01:27 PM)Vi-Sion Wrote: The error i'm having is :  Notice: Undefined index: hasStarted in C:\xampp\htdocs\test\random.php on line 3

Your problem is written in plain english "Undefined index: hasStarted in C:\xampp\htdocs\test\random.php on line 3"
You are trying to access the variable $_SESSION["hasStarted"] but is is not set.
You can check if a variable is set with the method isset()

Reply

RE: Guess random number #3
well i know but i heard that in php you don't need to assign a value to a variable before using it.
Code:
In PHP the type of the variable does not need to be declared before use it because types are associated with values rather than variables.
this is quoted from w3resource .


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Guess random number #4
(04-02-2017, 01:47 PM)Vi-Sion Wrote: well i know but i heard that in php you don't need to assign a value to a variable before using it.
Yes you do.

(04-02-2017, 01:47 PM)Vi-Sion Wrote:
Code:
In PHP the type of the variable does not need to be declared before use it because types are associated with values rather than variables.
this is quoted from w3resource .
This quote states that you don't have to declare variables, witch is right.
But you do have to set a value to a variable before using it.

Reply

RE: Guess random number #5
i also heard it takes a default value if i didn't give it one so if used as a boolean and didn't give it a value it's default value should be false


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Guess random number #6
(04-02-2017, 01:55 PM)Vi-Sion Wrote: i also heard it takes a default value if i didn't give it one so if used as a boolean and didn't give it a value it's default value should be false

Yes. In php an unset variable will always give you false.
But in your code, you are trying to access a variable index, that will always trow you an error if it is unset.
(This post was last modified: 04-02-2017, 02:10 PM by Pikami.)

Reply

RE: Guess random number #7
Thanks for your help i got it, much appreciated.


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Guess random number #8
(04-02-2017, 02:12 PM)Vi-Sion Wrote: Thanks for your help i got it, much appreciated.

I am glad I helped Blush

Reply

RE: Guess random number #9
Here's my final code i'll be glad to get comments if there's anything i should fix in my code.
Spoiler:
Code:
<?php
session_start();
if(isset($_SESSION["hasStarted"])==false){
$_SESSION["number"]=rand(0,100);
$_SESSION["hasStarted"]=true;
$_SESSION["tries"]=0;
}
?>
<html>
<head>
</head>
<body>
<div><p>Guess the hidden number between 0 and 100</p>
<form method="post" action="random.php">
<input type="text" name="number"/>
<input type="submit" value="Guess"/>
</form></div>
<?php
if($_POST){
$_SESSION["tries"]++;
if($_POST["number"]==$_SESSION["number"]){
$_SESSION["hasStarted"]=false;
?><script>alert("You've guessed the hidden number after "+<?php echo $_SESSION["tries"];?>+" tries");</script>

<div><a href="random.php">PLAY AGAIN</a></div>
<?php unset($_SESSION["hasStarted"])?>

<?php
$_SESSION["tries"]=0;
}
else{?>
<script><?php if($_SESSION["number"]<$_POST["number"]){?>alert("Hidden number is smaller");<?php }?></script>
<script><?php if($_SESSION["number"]>$_POST["number"]){?>alert("Hidden number is bigger");<?php } ?></script>
<?php }} ?>
</body>
</html>


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Guess random number #10
I recommended to using Gaussian Distribution , which implemented as random number generator.

https://en.wikipedia.org/wiki/Normal_distribution
http://mathworld.wolfram.com/NormalDistribution.html
https://natedenlinger.com/php-random-num...ell-curve/

I hope that help , comrade.

Reply







Users browsing this thread: 1 Guest(s)